Skip to content

Commit

Permalink
function to find the uncle of a node in a tree
Browse files Browse the repository at this point in the history
  • Loading branch information
whalay committed Mar 29, 2023
1 parent 8db23c9 commit 328f6d5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 18-binary_tree_uncle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "binary_trees.h"

/**
* binary_tree_uncle - Finds the uncle of a node
* in a binary tree.
* @node: A pointer to the node to find the uncle of.
*
* Return: If node is NULL or has no uncle, NULL.
* Otherwise, a pointer to the uncle node.
*/
binary_tree_t *binary_tree_uncle(binary_tree_t *node)
{
if (node == NULL ||
node->parent == NULL ||
node->parent->parent == NULL)
return (NULL);
if (node->parent->parent->left == node->parent)
return (node->parent->parent->right);
return (node->parent->parent->left);
}
16 changes: 16 additions & 0 deletions binary_trees.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,21 @@ void binary_tree_print(const binary_tree_t *);
binary_tree_t *binary_tree_node(binary_tree_t *parent, int value);
binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value);
binary_tree_t *binary_tree_insert_right(binary_tree_t *parent, int value);
void binary_tree_delete(binary_tree_t *tree);
int binary_tree_is_leaf(const binary_tree_t *node);
int binary_tree_is_root(const binary_tree_t *node);
void binary_tree_preorder(const binary_tree_t *tree, void (*func)(int));
void binary_tree_inorder(const binary_tree_t *tree, void (*func)(int));
void binary_tree_postorder(const binary_tree_t *tree, void (*func)(int));
size_t binary_tree_height(const binary_tree_t *tree);
size_t binary_tree_depth(const binary_tree_t *tree);
size_t binary_tree_size(const binary_tree_t *tree);
size_t binary_tree_leaves(const binary_tree_t *tree);
size_t binary_tree_nodes(const binary_tree_t *tree);
int binary_tree_balance(const binary_tree_t *tree);
int binary_tree_is_full(const binary_tree_t *tree);
int binary_tree_is_perfect(const binary_tree_t *tree);
binary_tree_t *binary_tree_sibling(binary_tree_t *node);
binary_tree_t *binary_tree_uncle(binary_tree_t *node);

#endif /* BINARY_TREES_H */

0 comments on commit 328f6d5

Please sign in to comment.