├── 135-O ├── 115-O ├── 125-O ├── AUTHORS ├── 5-binary_tree_is_root.c ├── 4-binary_tree_is_leaf.c ├── 3-binary_tree_delete.c ├── 10-binary_tree_depth.c ├── 7-binary_tree_inorder.c ├── 6-binary_tree_preorder.c ├── 8-binary_tree_postorder.c ├── 11-binary_tree_size.c ├── 113-bst_search.c ├── 132-array_to_heap.c ├── 17-binary_tree_sibling.c ├── 9-binary_tree_height.c ├── 112-array_to_bst.c ├── 15-binary_tree_is_full.c ├── 0-binary_tree_node.c ├── 103-binary_tree_rotate_left.c ├── 104-binary_tree_rotate_right.c ├── 122-array_to_avl.c ├── 1-binary_tree_insert_left.c ├── 12-binary_tree_leaves.c ├── 111-bst_insert.c ├── 2-binary_tree_insert_right.c ├── 18-binary_tree_uncle.c ├── 14-binary_tree_balance.c ├── LICENSE ├── 124-sorted_array_to_avl.c ├── 134-heap_to_sorted_array.c ├── 13-binary_tree_nodes.c ├── 100-binary_trees_ancestor.c ├── 110-binary_tree_is_bst.c ├── 101-binary_tree_levelorder.c ├── 120-binary_tree_is_avl.c ├── 131-heap_insert.c ├── 16-binary_tree_is_perfect.c ├── 121-avl_insert.c ├── binary_tree_print.c ├── 130-binary_tree_is_heap.c ├── 114-bst_remove.c ├── 133-heap_extract.c ├── 102-binary_tree_is_complete.c ├── 123-avl_remove.c ├── binary_trees.h └── README.md /135-O: -------------------------------------------------------------------------------- 1 | O(n) 2 | O(n) 3 | O(n) 4 | -------------------------------------------------------------------------------- /115-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | O(log(n)) 3 | O(log(n)) 4 | -------------------------------------------------------------------------------- /125-O: -------------------------------------------------------------------------------- 1 | O(log(n)) 2 | O(log(n)) 3 | O(log(n)) 4 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | INNOCENT UDO 2 | 3 | Pius Owolabi 4 | -------------------------------------------------------------------------------- /5-binary_tree_is_root.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_root - checks if a node is a root 5 | * @node: node to check 6 | * 7 | * Return: 1 if node is a root 8 | * 0 if not a root 9 | * 0 if node is NULL 10 | */ 11 | int binary_tree_is_root(const binary_tree_t *node) 12 | { 13 | return ((!node || node->parent) ? 0 : 1); 14 | } 15 | -------------------------------------------------------------------------------- /4-binary_tree_is_leaf.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_leaf - checks if a node is a leaf 5 | * @node: node to check 6 | * 7 | * Return: 1 if node is a leaf 8 | * 0 if not a leaf 9 | * 0 if node is NULL 10 | */ 11 | int binary_tree_is_leaf(const binary_tree_t *node) 12 | { 13 | return ((!node || node->left || node->right) ? 0 : 1); 14 | } 15 | -------------------------------------------------------------------------------- /3-binary_tree_delete.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_delete - deletes an entire binary tree 5 | * @tree: a pointer to the root node of the tree to delete 6 | * Description: If tree is NULL, do nothing 7 | */ 8 | 9 | void binary_tree_delete(binary_tree_t *tree) 10 | { 11 | if (!tree) 12 | return; 13 | binary_tree_delete(tree->right); 14 | binary_tree_delete(tree->left); 15 | free(tree); 16 | } 17 | -------------------------------------------------------------------------------- /10-binary_tree_depth.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_depth - measures the depth of a node in a binary tree. 5 | *@tree: pointer to the node to measure the depth. 6 | * Return: if tree is NULL, function must return 0. 7 | */ 8 | size_t binary_tree_depth(const binary_tree_t *tree) 9 | { 10 | if (tree == NULL || tree->parent == NULL) 11 | return (0); 12 | else 13 | return (1 + binary_tree_depth(tree->parent)); 14 | } 15 | -------------------------------------------------------------------------------- /7-binary_tree_inorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_inorder - goes through a binary tree using in-order traversal 5 | * @tree: tree to traverse 6 | * @func: pointer to a function to call for each node 7 | */ 8 | void binary_tree_inorder(const binary_tree_t *tree, void (*func)(int)) 9 | { 10 | if (!tree || !func) 11 | return; 12 | 13 | binary_tree_inorder(tree->left, func); 14 | func(tree->n); 15 | binary_tree_inorder(tree->right, func); 16 | } 17 | -------------------------------------------------------------------------------- /6-binary_tree_preorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_preorder - goes through a binary tree using pre-order traversal 5 | * @tree: tree to traverse 6 | * @func: pointer to a function to call for each node 7 | */ 8 | void binary_tree_preorder(const binary_tree_t *tree, void (*func)(int)) 9 | { 10 | if (!tree || !func) 11 | return; 12 | 13 | func(tree->n); 14 | binary_tree_preorder(tree->left, func); 15 | binary_tree_preorder(tree->right, func); 16 | } 17 | -------------------------------------------------------------------------------- /8-binary_tree_postorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_postorder - goes through a binary tree using post-order traverse 5 | * @tree: tree to traverse 6 | * @func: pointer to a function to call for each node 7 | */ 8 | void binary_tree_postorder(const binary_tree_t *tree, void (*func)(int)) 9 | { 10 | if (!tree || !func) 11 | return; 12 | 13 | binary_tree_postorder(tree->left, func); 14 | binary_tree_postorder(tree->right, func); 15 | func(tree->n); 16 | } 17 | -------------------------------------------------------------------------------- /11-binary_tree_size.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_size - measures the size of a binary tree. 5 | *@tree: pointer to the root node of the tree to measure the size. 6 | * Return: if tree is NULL, must return 0. 7 | */ 8 | size_t binary_tree_size(const binary_tree_t *tree) 9 | { 10 | if (tree == NULL) 11 | return (0); 12 | { 13 | size_t n_left, n_right; 14 | 15 | n_left = binary_tree_size(tree->left); 16 | n_right = binary_tree_size(tree->right); 17 | return (1 + n_left + n_right); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /113-bst_search.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * bst_search -function find node in a tree 4 | * @tree: root of the tre to evaluate 5 | * @value: node to find 6 | * Return: 1 if exits 0 if no 7 | */ 8 | bst_t *bst_search(const bst_t *tree, int value) 9 | { 10 | 11 | if (tree == NULL) 12 | return (NULL); 13 | if (value == tree->n) 14 | return ((bst_t *)tree); 15 | if (value < tree->n) 16 | return (bst_search(tree->left, value)); 17 | else 18 | return (bst_search(tree->right, value)); 19 | return (NULL); 20 | } 21 | -------------------------------------------------------------------------------- /132-array_to_heap.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * array_to_heap - builds a Max Binary Heap tree from an array 5 | * @array: a pointer to the first element of the array to be converted 6 | * @size: the number of element in the array 7 | * 8 | * Return: a pointer to the root node of the created Binary Heap 9 | * NULL on failure 10 | */ 11 | heap_t *array_to_heap(int *array, size_t size) 12 | { 13 | unsigned int i; 14 | heap_t *root = NULL; 15 | 16 | for (i = 0; i < size; i++) 17 | heap_insert(&root, array[i]); 18 | 19 | return (root); 20 | } 21 | -------------------------------------------------------------------------------- /17-binary_tree_sibling.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_sibling - finds the sibling of a node 5 | * @node: pointer to the node to find the sibling 6 | * 7 | * Return: pointer to the sibling node 8 | * NULL if node is NULL 9 | * NULL if the parent is NULL 10 | * NULL if the node has no siblings 11 | */ 12 | binary_tree_t *binary_tree_sibling(binary_tree_t *node) 13 | { 14 | if (!node || !node->parent) 15 | return (NULL); 16 | 17 | if (node == node->parent->left) 18 | return (node->parent->right); 19 | return (node->parent->left); 20 | } 21 | -------------------------------------------------------------------------------- /9-binary_tree_height.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_height - measures the height of a binary tree 5 | * @tree: tree to measure the height of 6 | * 7 | * Return: height of the tree 8 | * 0 if tree is NULL 9 | */ 10 | size_t binary_tree_height(const binary_tree_t *tree) 11 | { 12 | size_t height_l = 0; 13 | size_t height_r = 0; 14 | 15 | if (!tree) 16 | return (0); 17 | 18 | height_l = tree->left ? 1 + binary_tree_height(tree->left) : 0; 19 | height_r = tree->right ? 1 + binary_tree_height(tree->right) : 0; 20 | return (height_l > height_r ? height_l : height_r); 21 | } 22 | -------------------------------------------------------------------------------- /112-array_to_bst.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * array_to_bst - builds a Binary Search Tree from an array. 5 | * @array: pointer to the first element of the array to be converted. 6 | * @size: number of element in the array. 7 | * Return: pointer to the root node of the created BST, or NULL on failure. 8 | */ 9 | 10 | bst_t *array_to_bst(int *array, size_t size) 11 | { 12 | bst_t *tree; 13 | size_t index; 14 | 15 | if (array == NULL) 16 | return (NULL); 17 | tree = NULL; 18 | for (index = 0; index < size; index++) 19 | { 20 | bst_insert(&tree, array[index]); 21 | } 22 | return (tree); 23 | } 24 | -------------------------------------------------------------------------------- /15-binary_tree_is_full.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_full - checks if a binary tree is full 5 | * @tree: a pointer to the root node of the tree to check 6 | * 7 | * Return: 1 if the tree is full 8 | * 0 if the tree is not full 9 | * 0 if tree is NULL 10 | */ 11 | int binary_tree_is_full(const binary_tree_t *tree) 12 | { 13 | if (!tree) 14 | return (0); 15 | 16 | if (!tree->right && !tree->left) 17 | return (1); 18 | 19 | if (tree->right && tree->left) 20 | return (binary_tree_is_full(tree->left) && 21 | binary_tree_is_full(tree->right)); 22 | 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /0-binary_tree_node.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_node - function that creates a binary tree node. 5 | *@parent: is a pointer to the parent node of the node to create. 6 | *@value: is the value to put in the new node. 7 | * Return:Your function must return a pointer to the new node or NULL if no. 8 | */ 9 | binary_tree_t *binary_tree_node(binary_tree_t *parent, int value) 10 | { 11 | binary_tree_t *node = malloc(sizeof(binary_tree_t)); 12 | 13 | if (node == NULL) 14 | return (NULL); 15 | node->n = value; 16 | node->parent = parent; 17 | node->left = NULL; 18 | node->right = NULL; 19 | return (node); 20 | } 21 | -------------------------------------------------------------------------------- /103-binary_tree_rotate_left.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_rotate_left - performs a left-rotation on a binary tree 5 | * @tree: pointer to binare_tree_t 6 | * Return: pointer to the new root node of the tree once rotated; 7 | */ 8 | binary_tree_t *binary_tree_rotate_left(binary_tree_t *tree) 9 | { 10 | binary_tree_t *aux, *temp; 11 | 12 | if (tree == NULL) 13 | return (NULL); 14 | if (tree->right) 15 | { 16 | temp = tree->right->left; 17 | aux = tree->right; 18 | aux->parent = tree->parent; 19 | aux->left = tree; 20 | tree->parent = aux; 21 | tree->right = temp; 22 | if (temp) 23 | temp->parent = tree; 24 | return (aux); 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /104-binary_tree_rotate_right.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_rotate_right - performs a rigth-rotation on a binary tree 5 | * @tree: pointer to binare_tree_t 6 | * Return: pointer to the new root node of the tree once rotated; 7 | */ 8 | binary_tree_t *binary_tree_rotate_right(binary_tree_t *tree) 9 | { 10 | binary_tree_t *aux, *temp; 11 | 12 | if (tree == NULL) 13 | return (NULL); 14 | if (tree->left) 15 | { 16 | temp = tree->left->right; 17 | aux = tree->left; 18 | aux->parent = tree->parent; 19 | aux->right = tree; 20 | tree->parent = aux; 21 | tree->left = temp; 22 | if (temp) 23 | temp->parent = tree; 24 | return (aux); 25 | } 26 | return (NULL); 27 | } 28 | -------------------------------------------------------------------------------- /122-array_to_avl.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * array_to_avl - Builds an AVL tree from an array. 5 | * @array: A pointer to the first element of the array to be converted. 6 | * @size: The number of elements in @array. 7 | * 8 | * Return: A pointer to the root node of the created AVL, or NULL upon failure. 9 | */ 10 | avl_t *array_to_avl(int *array, size_t size) 11 | { 12 | avl_t *tree = NULL; 13 | size_t i, j; 14 | 15 | if (array == NULL) 16 | return (NULL); 17 | 18 | for (i = 0; i < size; i++) 19 | { 20 | for (j = 0; j < i; j++) 21 | { 22 | if (array[j] == array[i]) 23 | break; 24 | } 25 | if (j == i) 26 | { 27 | if (avl_insert(&tree, array[i]) == NULL) 28 | return (NULL); 29 | } 30 | } 31 | 32 | return (tree); 33 | } 34 | -------------------------------------------------------------------------------- /1-binary_tree_insert_left.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_insert_left - a function that inserts a node as the left-child 5 | *@parent: a pointer to the node to insert the left-child in. 6 | *@value: the value to store in the new node. 7 | * Return: pointer to created node, or NULL on failure or if parent is NULL. 8 | */ 9 | binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value) 10 | { 11 | binary_tree_t *node = NULL; 12 | 13 | if (parent == NULL) 14 | return (NULL); 15 | node = malloc(sizeof(binary_tree_t)); 16 | if (node == NULL) 17 | return (NULL); 18 | node->n = value; 19 | node->parent = parent; 20 | node->left = NULL; 21 | node->right = NULL; 22 | if (parent->left == NULL) 23 | parent->left = node; 24 | else 25 | { 26 | node->left = parent->left; 27 | parent->left = node; 28 | node->left->parent = node; 29 | } 30 | return (node); 31 | } 32 | -------------------------------------------------------------------------------- /12-binary_tree_leaves.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_leaf - a function that checks if a node is a leaf. 5 | *@node: is a pointer to the node to check. 6 | * Return: return 1 if node is a leaf, otherwise 0. 7 | */ 8 | int binary_tree_is_leaf(const binary_tree_t *node) 9 | { 10 | if (node == NULL) 11 | return (0); 12 | 13 | if (node->right == NULL && node->left == NULL) 14 | return (1); 15 | else 16 | return (0); 17 | } 18 | 19 | /** 20 | * binary_tree_leaves - number of leaves 21 | *@tree: pointer to the root node of the tree to count the number of leaves. 22 | * Return: If tree is NULL, the function must return 0. 23 | */ 24 | size_t binary_tree_leaves(const binary_tree_t *tree) 25 | { 26 | if (tree == NULL) 27 | return (0); 28 | else 29 | return (binary_tree_is_leaf(tree) + 30 | binary_tree_leaves(tree->right) + 31 | binary_tree_leaves(tree->left)); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /111-bst_insert.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * bst_insert - inserts a value in a Binary Search Tree. 4 | * @tree: double pointer to the root node of the BST to insert the value. 5 | * @value: value to store in the node to be inserted. 6 | * Return: Always 0 (Success) 7 | */ 8 | bst_t *bst_insert(bst_t **tree, int value) 9 | { 10 | bst_t *aux = NULL; 11 | 12 | if (tree == NULL) 13 | return (NULL); 14 | if (*tree == NULL) 15 | { 16 | *tree = binary_tree_node(NULL, value); 17 | return (*tree); 18 | } 19 | aux = *tree; 20 | if (value < aux->n) 21 | { 22 | if (aux->left == NULL) 23 | { 24 | aux->left = binary_tree_node(aux, value); 25 | return (aux->left); 26 | } 27 | return (bst_insert(&(aux->left), value)); 28 | } 29 | if (value > aux->n) 30 | { 31 | if (aux->right == NULL) 32 | { 33 | aux->right = binary_tree_node(aux, value); 34 | return (aux->right); 35 | } 36 | return (bst_insert(&(aux->right), value)); 37 | } 38 | return (NULL); 39 | } 40 | -------------------------------------------------------------------------------- /2-binary_tree_insert_right.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_insert_right - inserts a node as the 5 | * right-child of another node 6 | * 7 | * @parent: is a pointer to the node to insert the right-child in 8 | * @value: is the value to store in the new node 9 | * 10 | * Description: If parent already has a right-child, the new node 11 | * must take its place, and the old right-child must 12 | * be set as the right-child of the new node. 13 | * 14 | * Return: a pointer to the created node, or 15 | * NULL on failure or 16 | * if parent is NULL 17 | */ 18 | 19 | binary_tree_t *binary_tree_insert_right(binary_tree_t *parent, int value) 20 | { 21 | binary_tree_t *new; 22 | 23 | if (!parent) 24 | return (NULL); 25 | 26 | new = malloc(sizeof(binary_tree_t)); 27 | if (!new) 28 | return (NULL); 29 | 30 | new->n = value; 31 | new->parent = parent; 32 | new->left = NULL; 33 | new->right = parent->right; 34 | parent->right = new; 35 | if (new->right) 36 | new->right->parent = new; 37 | return (new); 38 | } 39 | -------------------------------------------------------------------------------- /18-binary_tree_uncle.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_uncle - finds the uncle of a node 5 | * @node: a pointer to the node to find the uncle 6 | * 7 | * Return: pointer to the uncle node 8 | * NULL if node is NULL 9 | * NULL if the parent is NULL 10 | * NULL if the node has no uncle 11 | */ 12 | binary_tree_t *binary_tree_uncle(binary_tree_t *node) 13 | { 14 | if (!node || !node->parent) 15 | return (NULL); 16 | 17 | return (binary_tree_sibling(node->parent)); 18 | } 19 | 20 | /** 21 | * binary_tree_sibling - finds the sibling of a node 22 | * @node: pointer to the node to find the sibling 23 | * 24 | * Return: pointer to the sibling node 25 | * NULL if node is NULL 26 | * NULL if the parent is NULL 27 | * NULL if the node has no siblings 28 | */ 29 | binary_tree_t *binary_tree_sibling(binary_tree_t *node) 30 | { 31 | if (!node || !node->parent) 32 | return (NULL); 33 | 34 | if (node == node->parent->left) 35 | return (node->parent->right); 36 | return (node->parent->left); 37 | } 38 | -------------------------------------------------------------------------------- /14-binary_tree_balance.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_height_b - Measures height of a binary tree for a bal tree 5 | * @tree: tree to go through 6 | * Return: the height 7 | */ 8 | 9 | size_t binary_tree_height_b(const binary_tree_t *tree) 10 | { 11 | size_t l = 0; 12 | size_t r = 0; 13 | 14 | if (tree == NULL) 15 | { 16 | return (0); 17 | } 18 | else 19 | { 20 | if (tree) 21 | { 22 | l = tree->left ? 1 + binary_tree_height_b(tree->left) : 1; 23 | r = tree->right ? 1 + binary_tree_height_b(tree->right) : 1; 24 | } 25 | return ((l > r) ? l : r); 26 | } 27 | } 28 | 29 | /** 30 | * binary_tree_balance - Measures balance factor of a binary tree 31 | * @tree: tree to go through 32 | * Return: balanced factor 33 | */ 34 | 35 | int binary_tree_balance(const binary_tree_t *tree) 36 | { 37 | int right = 0, left = 0, total = 0; 38 | 39 | if (tree) 40 | { 41 | left = ((int)binary_tree_height_b(tree->left)); 42 | right = ((int)binary_tree_height_b(tree->right)); 43 | total = left - right; 44 | } 45 | return (total); 46 | } 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 INNOCENT CHARLES UDO 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /124-sorted_array_to_avl.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * aux_sort - create the tree using the half element of the array 4 | * @parent: parent of the node to create 5 | * @array: sorted array 6 | * @begin: position where the array starts 7 | * @last: position where the array ends 8 | * Return: tree created 9 | */ 10 | avl_t *aux_sort(avl_t *parent, int *array, int begin, int last) 11 | { 12 | avl_t *root; 13 | binary_tree_t *aux; 14 | int mid = 0; 15 | 16 | if (begin <= last) 17 | { 18 | mid = (begin + last) / 2; 19 | aux = binary_tree_node((binary_tree_t *)parent, array[mid]); 20 | if (aux == NULL) 21 | return (NULL); 22 | root = (avl_t *)aux; 23 | root->left = aux_sort(root, array, begin, mid - 1); 24 | root->right = aux_sort(root, array, mid + 1, last); 25 | return (root); 26 | } 27 | return (NULL); 28 | } 29 | /** 30 | * sorted_array_to_avl - create a alv tree from sorted array 31 | * @array: sorted array 32 | * @size: size of the sorted array 33 | * Return: alv tree form sorted array 34 | */ 35 | avl_t *sorted_array_to_avl(int *array, size_t size) 36 | { 37 | if (array == NULL || size == 0) 38 | return (NULL); 39 | return (aux_sort(NULL, array, 0, ((int)(size)) - 1)); 40 | } 41 | -------------------------------------------------------------------------------- /134-heap_to_sorted_array.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * tree_size - measures the sum of heights of a binary tree 5 | * @tree: pointer to the root node of the tree to measure the height 6 | * 7 | * Return: Height or 0 if tree is NULL 8 | */ 9 | size_t tree_size(const binary_tree_t *tree) 10 | { 11 | size_t height_l = 0; 12 | size_t height_r = 0; 13 | 14 | if (!tree) 15 | return (0); 16 | 17 | if (tree->left) 18 | height_l = 1 + tree_size(tree->left); 19 | 20 | if (tree->right) 21 | height_r = 1 + tree_size(tree->right); 22 | 23 | return (height_l + height_r); 24 | } 25 | 26 | /** 27 | * heap_to_sorted_array - converts a Binary Max Heap 28 | * to a sorted array of integers 29 | * @heap: a pointer to the root node of the heap to convert 30 | * @size: an address to store the size of the array 31 | * 32 | * Return: the generated array 33 | * NULL on failure 34 | */ 35 | int *heap_to_sorted_array(heap_t *heap, size_t *size) 36 | { 37 | int i, *a = NULL; 38 | 39 | if (!heap || !size) 40 | return (NULL); 41 | 42 | *size = tree_size(heap) + 1; 43 | 44 | a = malloc(sizeof(int) * (*size)); 45 | 46 | if (!a) 47 | return (NULL); 48 | 49 | for (i = 0; heap; i++) 50 | a[i] = heap_extract(&heap); 51 | 52 | return (a); 53 | } 54 | -------------------------------------------------------------------------------- /13-binary_tree_nodes.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_nodes - counts the nodes with at least 1 child in a binary tree 5 | * @tree: tree to count the nodes from 6 | * 7 | * Return: number of nodes counted 8 | * 0 if tree is NULL 9 | */ 10 | size_t binary_tree_nodes(const binary_tree_t *tree) 11 | { 12 | if (!tree || (!tree->left && !tree->right)) 13 | return (0); 14 | 15 | return (binary_tree_size(tree) - binary_tree_leaves(tree)); 16 | } 17 | 18 | /** 19 | * binary_tree_size - measures the size of a binary tree 20 | * @tree: tree to measure the size of 21 | * 22 | * Return: size of the tree 23 | * 0 if tree is NULL 24 | */ 25 | size_t binary_tree_size(const binary_tree_t *tree) 26 | { 27 | if (!tree) 28 | return (0); 29 | 30 | return (binary_tree_size(tree->left) + binary_tree_size(tree->right) + 1); 31 | } 32 | 33 | /** 34 | * binary_tree_leaves - counts the leaves in a binary tree 35 | * @tree: tree to count the leaves from 36 | * 37 | * Return: number of leaves 38 | * 0 if tree is NULL 39 | */ 40 | size_t binary_tree_leaves(const binary_tree_t *tree) 41 | { 42 | if (!tree) 43 | return (0); 44 | 45 | if (!tree->left && !tree->right) 46 | return (1); 47 | 48 | return (binary_tree_leaves(tree->left) + binary_tree_leaves(tree->right)); 49 | } 50 | -------------------------------------------------------------------------------- /100-binary_trees_ancestor.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_depth - measures the depth of a node in a binary tree. 5 | *@tree: pointer to the node to measure the depth. 6 | * Return: if tree is NULL, function must return 0. 7 | */ 8 | size_t binary_tree_depth(const binary_tree_t *tree) 9 | { 10 | if (tree == NULL) 11 | return (0); 12 | else 13 | return (1 + binary_tree_depth(tree->parent)); 14 | } 15 | /** 16 | * binary_trees_ancestor - finds the lowest common ancestor of two nodes 17 | * @first: first node 18 | * @second: second node 19 | * Return: lowest common ancestor 20 | */ 21 | binary_tree_t *binary_trees_ancestor(const binary_tree_t *first, 22 | const binary_tree_t *second) 23 | { 24 | size_t height_f = 0, height_s = 0; 25 | const binary_tree_t *aux = NULL; 26 | 27 | height_f = binary_tree_depth(first); 28 | height_s = binary_tree_depth(second); 29 | if (height_f && height_s) 30 | { 31 | if (height_f > height_s) 32 | { 33 | aux = first; 34 | first = second; 35 | second = aux; 36 | } 37 | while (first) 38 | { 39 | aux = second; 40 | while (aux) 41 | { 42 | if (first == aux) 43 | return ((binary_tree_t *)first); 44 | aux = aux->parent; 45 | } 46 | first = first->parent; 47 | } 48 | } 49 | else 50 | { 51 | return (NULL); 52 | } 53 | return (NULL); 54 | } 55 | -------------------------------------------------------------------------------- /110-binary_tree_is_bst.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * find_node -function find node in a tree 4 | * @root: root of the tre to evaluate 5 | * @node: node to find 6 | * Return: 1 if exits 0 if no 7 | */ 8 | int find_node(binary_tree_t *root, binary_tree_t *node) 9 | { 10 | 11 | if (root == NULL) 12 | return (0); 13 | if (node == root) 14 | return (1); 15 | if (node->n < root->n) 16 | return (find_node(root->left, node)); 17 | if (node->n > root->n) 18 | return (find_node(root->right, node)); 19 | return (0); 20 | } 21 | /** 22 | * croos_tree - cross the tree checking if each node exist correctly 23 | * @root: root node of the tree 24 | * @node: current node to evaluate 25 | * Return: 1 if is BST0 if no 26 | */ 27 | int croos_tree(binary_tree_t *root, binary_tree_t *node) 28 | { 29 | if (root && node) 30 | { 31 | int aux = 0; 32 | 33 | aux = find_node(root, node); 34 | if (node->left) 35 | aux &= croos_tree(root, node->left); 36 | if (node->right) 37 | aux &= croos_tree(root, node->right); 38 | return (aux); 39 | } 40 | return (0); 41 | } 42 | /** 43 | * binary_tree_is_bst - check if ist a correctly bst tree 44 | * @tree: tre to check 45 | * Return: 1 if is bst 0 if not 46 | */ 47 | int binary_tree_is_bst(const binary_tree_t *tree) 48 | { 49 | if (tree == NULL) 50 | return (0); 51 | return (croos_tree((binary_tree_t *)tree, (binary_tree_t *)tree)); 52 | } 53 | -------------------------------------------------------------------------------- /101-binary_tree_levelorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_height - measures the height of a binary tree. 4 | *@tree: pointer to the root node of the tree to measure the height. 5 | * Return: if tree is NULL, your function must return 0. 6 | */ 7 | size_t binary_tree_height(const binary_tree_t *tree) 8 | { 9 | if (tree) 10 | { 11 | int left = 0, right = 0; 12 | 13 | if (tree->right) 14 | right = 1 + binary_tree_height(tree->right); 15 | if (tree->left) 16 | left = 1 + binary_tree_height(tree->left); 17 | if (left > right) 18 | return (left); 19 | else 20 | return (right); 21 | } 22 | else 23 | return (0); 24 | } 25 | /** 26 | * print_at_level - print node, especific level 27 | * @tree: pointer to the root node of the tree to traverse 28 | * @func: pointer to a function to call for each node. 29 | * @level: level to print 30 | */ 31 | void print_at_level(const binary_tree_t *tree, void (*func)(int), int level) 32 | { 33 | if (tree && func) 34 | { 35 | if (level == 1) 36 | func(tree->n); 37 | else 38 | { 39 | print_at_level(tree->left, func, level - 1); 40 | print_at_level(tree->right, func, level - 1); 41 | } 42 | } 43 | 44 | } 45 | 46 | /** 47 | * binary_tree_levelorder - goes through a binary tree in level-order traversal 48 | * @tree: pointer to the root node of the tree to traverse 49 | * @func: pointer to a function to call for each node. 50 | */ 51 | void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int)) 52 | { 53 | size_t h = 0, i = 1; 54 | 55 | if (tree && func) 56 | { 57 | h = binary_tree_height(tree); 58 | while (i <= h + 1) 59 | { 60 | print_at_level(tree, func, i); 61 | i++; 62 | } 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /120-binary_tree_is_avl.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | #include "limits.h" 3 | 4 | size_t height(const binary_tree_t *tree); 5 | int is_avl_helper(const binary_tree_t *tree, int lo, int hi); 6 | int binary_tree_is_avl(const binary_tree_t *tree); 7 | 8 | /** 9 | * height - Measures the height of a binary tree. 10 | * @tree: A pointer to the root node of the tree to measure the height. 11 | * 12 | * Return: If tree is NULL, your function must return 0, else return height. 13 | */ 14 | size_t height(const binary_tree_t *tree) 15 | { 16 | if (tree) 17 | { 18 | size_t l = 0, r = 0; 19 | 20 | l = tree->left ? 1 + height(tree->left) : 1; 21 | r = tree->right ? 1 + height(tree->right) : 1; 22 | return ((l > r) ? l : r); 23 | } 24 | return (0); 25 | } 26 | 27 | /** 28 | * is_avl_helper - Checks if a binary tree is a valid AVL tree. 29 | * @tree: A pointer to the root node of the tree to check. 30 | * @lo: The value of the smallest node visited thus far. 31 | * @hi: The value of the largest node visited this far. 32 | * 33 | * Return: If the tree is a valid AVL tree, 1, otherwise, 0. 34 | */ 35 | int is_avl_helper(const binary_tree_t *tree, int lo, int hi) 36 | { 37 | size_t lhgt, rhgt, diff; 38 | 39 | if (tree != NULL) 40 | { 41 | if (tree->n < lo || tree->n > hi) 42 | return (0); 43 | lhgt = height(tree->left); 44 | rhgt = height(tree->right); 45 | diff = lhgt > rhgt ? lhgt - rhgt : rhgt - lhgt; 46 | if (diff > 1) 47 | return (0); 48 | return (is_avl_helper(tree->left, lo, tree->n - 1) && 49 | is_avl_helper(tree->right, tree->n + 1, hi)); 50 | } 51 | return (1); 52 | } 53 | 54 | /** 55 | * binary_tree_is_avl - Checks if a binary tree is a valid AVL tree. 56 | * @tree: A pointer to the root node of the tree to check. 57 | * 58 | * Return: 1 if tree is a valid AVL tree, and 0 otherwise 59 | */ 60 | int binary_tree_is_avl(const binary_tree_t *tree) 61 | { 62 | if (tree == NULL) 63 | return (0); 64 | return (is_avl_helper(tree, INT_MIN, INT_MAX)); 65 | } 66 | -------------------------------------------------------------------------------- /131-heap_insert.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * heap_insert - inserts a value in Max Binary Heap 5 | * @root: a double pointer to the root node of the Heap to insert the value 6 | * @value: the value to store in the node to be inserted 7 | * 8 | * Return: a pointer to the created node 9 | * NULL on failure 10 | */ 11 | heap_t *heap_insert(heap_t **root, int value) 12 | { 13 | heap_t *tree, *new, *flip; 14 | int size, leaves, sub, bit, level, tmp; 15 | 16 | if (!root) 17 | return (NULL); 18 | if (!(*root)) 19 | return (*root = binary_tree_node(NULL, value)); 20 | tree = *root; 21 | size = binary_tree_size(tree); 22 | leaves = size; 23 | for (level = 0, sub = 1; leaves >= sub; sub *= 2, level++) 24 | leaves -= sub; 25 | /* subtract all nodes except for bottom-most level */ 26 | 27 | for (bit = 1 << (level - 1); bit != 1; bit >>= 1) 28 | tree = leaves & bit ? tree->right : tree->left; 29 | /* 30 | * Traverse tree to first empty slot based on the binary 31 | * representation of the number of leaves. 32 | * Example - 33 | * If there are 12 nodes in a complete tree, there are 5 leaves on 34 | * the 4th tier of the tree. 5 is 101 in binary. 1 corresponds to 35 | * right, 0 to left. 36 | * The first empty node is 101 == RLR, *root->right->left->right 37 | */ 38 | 39 | new = binary_tree_node(tree, value); 40 | leaves & 1 ? (tree->right = new) : (tree->left = new); 41 | 42 | flip = new; 43 | for (; flip->parent && (flip->n > flip->parent->n); flip = flip->parent) 44 | { 45 | tmp = flip->n; 46 | flip->n = flip->parent->n; 47 | flip->parent->n = tmp; 48 | new = new->parent; 49 | } 50 | /* Flip values with parent until parent value exceeds new value */ 51 | 52 | return (new); 53 | } 54 | 55 | /** 56 | * binary_tree_size - measures the size of a binary tree 57 | * @tree: tree to measure the size of 58 | * 59 | * Return: size of the tree 60 | * 0 if tree is NULL 61 | */ 62 | size_t binary_tree_size(const binary_tree_t *tree) 63 | { 64 | if (!tree) 65 | return (0); 66 | 67 | return (binary_tree_size(tree->left) + binary_tree_size(tree->right) + 1); 68 | } 69 | -------------------------------------------------------------------------------- /16-binary_tree_is_perfect.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "binary_trees.h" 3 | 4 | /** 5 | * binary_tree_height - measures the height of a binary tree 6 | * @tree: tree to measure the height of 7 | * 8 | * Return: height of the tree 9 | * 0 if tree is NULL 10 | */ 11 | size_t binary_tree_height(const binary_tree_t *tree) 12 | { 13 | size_t height_l = 0; 14 | size_t height_r = 0; 15 | 16 | if (!tree) 17 | return (0); 18 | 19 | height_l = tree->left ? 1 + binary_tree_height(tree->left) : 0; 20 | height_r = tree->right ? 1 + binary_tree_height(tree->right) : 0; 21 | return (height_l > height_r ? height_l : height_r); 22 | } 23 | 24 | /** 25 | * binary_tree_size - measures the size of a binary tree 26 | * @tree: tree to measure the size of 27 | * 28 | * Return: size of the tree 29 | * 0 if tree is NULL 30 | */ 31 | size_t binary_tree_size(const binary_tree_t *tree) 32 | { 33 | if (!tree) 34 | return (0); 35 | 36 | return (binary_tree_size(tree->left) + binary_tree_size(tree->right) + 1); 37 | } 38 | 39 | /** 40 | *_pow_recursion - returns the value of x raised to the power of y 41 | *@x: the value to exponentiate 42 | *@y: the power to raise x to 43 | *Return: x to the power of y, or -1 if y is negative 44 | */ 45 | 46 | int _pow_recursion(int x, int y) 47 | { 48 | if (y < 0) 49 | return (-1); 50 | if (y == 0) 51 | return (1); 52 | else 53 | return (x * _pow_recursion(x, y - 1)); 54 | } 55 | 56 | /** 57 | * binary_tree_is_perfect - checks if a binary tree is perfect 58 | * @tree: a pointer to the root node of the tree to check 59 | * 60 | * Return: 1 if the tree is perfect 61 | * 0 if the tree is not perfect 62 | * 0 if tree is NULL 63 | */ 64 | int binary_tree_is_perfect(const binary_tree_t *tree) 65 | { 66 | size_t height = 0; 67 | size_t nodes = 0; 68 | size_t power = 0; 69 | 70 | if (!tree) 71 | return (0); 72 | 73 | if (!tree->right && !tree->left) 74 | return (1); 75 | 76 | height = binary_tree_height(tree); 77 | nodes = binary_tree_size(tree); 78 | 79 | power = (size_t)_pow_recursion(2, height + 1); 80 | return (power - 1 == nodes); 81 | } 82 | 83 | -------------------------------------------------------------------------------- /121-avl_insert.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * r_insert_node - node value insertion in an AVL tree. 5 | * @tree: double pointer to the root node of the AVL tree struct. 6 | * @parent: parent node of struct AVL. 7 | * @new: double pointer to left or right insertion. 8 | * @nval: insertion value of the AVL. 9 | * 10 | * Return: pointer to the new root after insertion, otherwise NULL. 11 | */ 12 | avl_t *r_insert_node(avl_t **tree, avl_t *parent, avl_t **new, int nval) 13 | { 14 | int bval; 15 | 16 | if (*tree == NULL) 17 | return (*new = binary_tree_node(parent, nval)); 18 | 19 | if ((*tree)->n > nval) 20 | { 21 | (*tree)->left = r_insert_node(&(*tree)->left, *tree, new, nval); 22 | if ((*tree)->left == NULL) 23 | return (NULL); 24 | } 25 | else if ((*tree)->n < nval) 26 | { 27 | (*tree)->right = r_insert_node(&(*tree)->right, *tree, new, nval); 28 | if ((*tree)->right == NULL) 29 | return (NULL); 30 | } 31 | else 32 | { 33 | return (*tree); 34 | } 35 | 36 | bval = binary_tree_balance(*tree); 37 | 38 | if (bval > 1 && (*tree)->left->n > nval) 39 | *tree = binary_tree_rotate_right(*tree); 40 | else if (bval > 1 && (*tree)->left->n < nval) 41 | { 42 | (*tree)->left = binary_tree_rotate_left((*tree)->left); 43 | *tree = binary_tree_rotate_right(*tree); 44 | } 45 | else if (bval < -1 && (*tree)->right->n < nval) 46 | *tree = binary_tree_rotate_left(*tree); 47 | else if (bval < -1 && (*tree)->right->n > nval) 48 | { 49 | (*tree)->right = binary_tree_rotate_right((*tree)->right); 50 | *tree = binary_tree_rotate_left(*tree); 51 | } 52 | 53 | return (*tree); 54 | } 55 | 56 | /** 57 | * avl_insert - inserts a value into an AVL tree. 58 | * @tree: double pointer to the root node of the AVL tree to insert into. 59 | * @value: value to store in the node to be inserted. 60 | * 61 | * Return: pointer to the inserted node, or NULL if fails. 62 | */ 63 | avl_t *avl_insert(avl_t **tree, int value) 64 | { 65 | avl_t *new = NULL; 66 | 67 | if (*tree == NULL) 68 | { 69 | *tree = binary_tree_node(NULL, value); 70 | return (*tree); 71 | } 72 | 73 | r_insert_node(tree, *tree, &new, value); 74 | return (new); 75 | } 76 | -------------------------------------------------------------------------------- /binary_tree_print.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "binary_trees.h" 5 | 6 | /* Original code from http://stackoverflow.com/a/13755911/5184480 */ 7 | 8 | /** 9 | * print_t - Stores recursively each level in an array of strings 10 | * 11 | * @tree: Pointer to the node to print 12 | * @offset: Offset to print 13 | * @depth: Depth of the node 14 | * @s: Buffer 15 | * 16 | * Return: length of printed tree after process 17 | */ 18 | static int print_t(const binary_tree_t *tree, int offset, int depth, char **s) 19 | { 20 | char b[6]; 21 | int width, left, right, is_left, i; 22 | 23 | if (!tree) 24 | return (0); 25 | is_left = (tree->parent && tree->parent->left == tree); 26 | width = sprintf(b, "(%03d)", tree->n); 27 | left = print_t(tree->left, offset, depth + 1, s); 28 | right = print_t(tree->right, offset + left + width, depth + 1, s); 29 | for (i = 0; i < width; i++) 30 | s[depth][offset + left + i] = b[i]; 31 | if (depth && is_left) 32 | { 33 | for (i = 0; i < width + right; i++) 34 | s[depth - 1][offset + left + width / 2 + i] = '-'; 35 | s[depth - 1][offset + left + width / 2] = '.'; 36 | } 37 | else if (depth && !is_left) 38 | { 39 | for (i = 0; i < left + width; i++) 40 | s[depth - 1][offset - width / 2 + i] = '-'; 41 | s[depth - 1][offset + left + width / 2] = '.'; 42 | } 43 | return (left + width + right); 44 | } 45 | 46 | /** 47 | * _height - Measures the height of a binary tree 48 | * 49 | * @tree: Pointer to the node to measures the height 50 | * 51 | * Return: The height of the tree starting at @node 52 | */ 53 | static size_t _height(const binary_tree_t *tree) 54 | { 55 | size_t height_l; 56 | size_t height_r; 57 | 58 | height_l = tree->left ? 1 + _height(tree->left) : 0; 59 | height_r = tree->right ? 1 + _height(tree->right) : 0; 60 | return (height_l > height_r ? height_l : height_r); 61 | } 62 | 63 | /** 64 | * binary_tree_print - Prints a binary tree 65 | * 66 | * @tree: Pointer to the root node of the tree to print 67 | */ 68 | void binary_tree_print(const binary_tree_t *tree) 69 | { 70 | char **s; 71 | size_t height, i, j; 72 | 73 | if (!tree) 74 | return; 75 | height = _height(tree); 76 | s = malloc(sizeof(*s) * (height + 1)); 77 | if (!s) 78 | return; 79 | for (i = 0; i < height + 1; i++) 80 | { 81 | s[i] = malloc(sizeof(**s) * 255); 82 | if (!s[i]) 83 | return; 84 | memset(s[i], 32, 255); 85 | } 86 | print_t(tree, 0, 0, s); 87 | for (i = 0; i < height + 1; i++) 88 | { 89 | for (j = 254; j > 1; --j) 90 | { 91 | if (s[i][j] != ' ') 92 | break; 93 | s[i][j] = '\0'; 94 | } 95 | printf("%s\n", s[i]); 96 | free(s[i]); 97 | } 98 | free(s); 99 | } 100 | -------------------------------------------------------------------------------- /130-binary_tree_is_heap.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "binary_trees.h" 3 | 4 | int btih_helper(const binary_tree_t *tree); 5 | int binary_tree_is_complete(const binary_tree_t *tree); 6 | int btic_helper(const binary_tree_t *tree, size_t index, size_t size); 7 | size_t binary_tree_size(const binary_tree_t *tree); 8 | 9 | /** 10 | * binary_tree_is_heap - checks if a binary tree is a valid Max Binary Heap 11 | * @tree: a pointer to the root node of the tree to check 12 | * 13 | * Return: 1 if tree is a valid Max Binary Heap 14 | * 0 if tree is NULL 15 | * 0 otherwise 16 | */ 17 | int binary_tree_is_heap(const binary_tree_t *tree) 18 | { 19 | if (!tree) 20 | return (0); 21 | return (btih_helper(tree)); 22 | } 23 | 24 | /** 25 | * btih_helper - checks if a binary tree is a valid Max Binary Heap 26 | * @tree: a pointer to the root node of the tree to check 27 | * 28 | * Return: 1 if tree is a valid Max Binary Heap 29 | * 1 if tree is NULL 30 | * 0 otherwise 31 | */ 32 | int btih_helper(const binary_tree_t *tree) 33 | { 34 | if (!tree) 35 | return (1); 36 | 37 | if (!binary_tree_is_complete(tree)) 38 | return (0); 39 | 40 | if (tree->left) 41 | if (tree->left->n > tree->n) 42 | return (0); 43 | if (tree->right) 44 | if (tree->right->n > tree->n) 45 | return (0); 46 | 47 | return (btih_helper(tree->left) && 48 | btih_helper(tree->right)); 49 | } 50 | 51 | /** 52 | * binary_tree_is_complete - checks if a binary tree is complete 53 | * @tree: a pointer to the root node of the tree to check 54 | * 55 | * Return: 1 if the tree is complete 56 | * 0 if the tree is not complete 57 | * 0 if tree is NULL 58 | */ 59 | int binary_tree_is_complete(const binary_tree_t *tree) 60 | { 61 | size_t size; 62 | 63 | if (!tree) 64 | return (0); 65 | size = binary_tree_size(tree); 66 | 67 | return (btic_helper(tree, 0, size)); 68 | } 69 | 70 | /** 71 | * btic_helper - checks if a binary tree is complete 72 | * @tree: a pointer to the root node of the tree to check 73 | * @index: node index to check 74 | * @size: number of nodes in the tree 75 | * 76 | * Return: 1 if the tree is complete 77 | * 0 if the tree is not complete 78 | * 0 if tree is NULL 79 | */ 80 | int btic_helper(const binary_tree_t *tree, size_t index, size_t size) 81 | { 82 | if (!tree) 83 | return (1); 84 | 85 | if (index >= size) 86 | return (0); 87 | 88 | return (btic_helper(tree->left, 2 * index + 1, size) && 89 | btic_helper(tree->right, 2 * index + 2, size)); 90 | } 91 | 92 | /** 93 | * binary_tree_size - measures the size of a binary tree 94 | * @tree: tree to measure the size of 95 | * 96 | * Return: size of the tree 97 | * 0 if tree is NULL 98 | */ 99 | size_t binary_tree_size(const binary_tree_t *tree) 100 | { 101 | if (!tree) 102 | return (0); 103 | 104 | return (binary_tree_size(tree->left) + 105 | binary_tree_size(tree->right) + 1); 106 | } 107 | -------------------------------------------------------------------------------- /114-bst_remove.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | bst_t *inorder_successor(bst_t *root); 4 | bst_t *bst_delete(bst_t *root, bst_t *node); 5 | bst_t *bst_remove_recursive(bst_t *root, bst_t *node, int value); 6 | bst_t *bst_remove(bst_t *root, int value); 7 | 8 | /** 9 | * inorder_successor - Returns the minimum value of a binary search tree. 10 | * @root: A pointer to the root node of the BST to search. 11 | * 12 | * Return: The minimum value in @tree. 13 | */ 14 | bst_t *inorder_successor(bst_t *root) 15 | { 16 | while (root->left != NULL) 17 | root = root->left; 18 | return (root); 19 | } 20 | 21 | /** 22 | * bst_delete - Deletes a node from a binary search tree. 23 | * @root: A pointer to the root node of the BST. 24 | * @node: A pointer to the node to delete from the BST. 25 | * 26 | * Return: A pointer to the new root node after deletion. 27 | */ 28 | bst_t *bst_delete(bst_t *root, bst_t *node) 29 | { 30 | bst_t *parent = node->parent, *successor = NULL; 31 | 32 | /* No children or right-child only */ 33 | if (node->left == NULL) 34 | { 35 | if (parent != NULL && parent->left == node) 36 | parent->left = node->right; 37 | else if (parent != NULL) 38 | parent->right = node->right; 39 | if (node->right != NULL) 40 | node->right->parent = parent; 41 | free(node); 42 | return (parent == NULL ? node->right : root); 43 | } 44 | 45 | /* Left-child only */ 46 | if (node->right == NULL) 47 | { 48 | if (parent != NULL && parent->left == node) 49 | parent->left = node->left; 50 | else if (parent != NULL) 51 | parent->right = node->left; 52 | if (node->left != NULL) 53 | node->left->parent = parent; 54 | free(node); 55 | return (parent == NULL ? node->left : root); 56 | } 57 | 58 | /* Two children */ 59 | successor = inorder_successor(node->right); 60 | node->n = successor->n; 61 | 62 | return (bst_delete(root, successor)); 63 | } 64 | 65 | /** 66 | * bst_remove_recursive - Removes a node from a binary search tree recursively. 67 | * @root: A pointer to the root node of the BST to remove a node from. 68 | * @node: A pointer to the current node in the BST. 69 | * @value: The value to remove from the BST. 70 | * 71 | * Return: A pointer to the root node after deletion. 72 | */ 73 | bst_t *bst_remove_recursive(bst_t *root, bst_t *node, int value) 74 | { 75 | if (node != NULL) 76 | { 77 | if (node->n == value) 78 | return (bst_delete(root, node)); 79 | if (node->n > value) 80 | return (bst_remove_recursive(root, node->left, value)); 81 | return (bst_remove_recursive(root, node->right, value)); 82 | } 83 | return (NULL); 84 | } 85 | 86 | /** 87 | * bst_remove - Removes a node from a binary search tree. 88 | * @root: A pointer to the root node of the BST to remove a node from. 89 | * @value: The value to remove in the BST. 90 | * 91 | * Return: A pointer to the new root node after deletion. 92 | * 93 | * Description: If the node to be deleted has two children, it 94 | * is replaced with its first in-order successor. 95 | */ 96 | bst_t *bst_remove(bst_t *root, int value) 97 | { 98 | return (bst_remove_recursive(root, root, value)); 99 | } 100 | -------------------------------------------------------------------------------- /133-heap_extract.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * tree_height - measures the height of a binary tree 5 | * @tree: pointer to the root node of the tree to measure the height 6 | * 7 | * Return: Height or 0 if tree is NULL 8 | */ 9 | size_t tree_height(const heap_t *tree) 10 | { 11 | size_t height_l = 0; 12 | size_t height_r = 0; 13 | 14 | if (!tree) 15 | return (0); 16 | 17 | if (tree->left) 18 | height_l = 1 + tree_height(tree->left); 19 | 20 | if (tree->right) 21 | height_r = 1 + tree_height(tree->right); 22 | 23 | if (height_l > height_r) 24 | return (height_l); 25 | return (height_r); 26 | } 27 | /** 28 | * tree_size_h - measures the sum of heights of a binary tree 29 | * @tree: pointer to the root node of the tree to measure the height 30 | * 31 | * Return: Height or 0 if tree is NULL 32 | */ 33 | size_t tree_size_h(const binary_tree_t *tree) 34 | { 35 | size_t height_l = 0; 36 | size_t height_r = 0; 37 | 38 | if (!tree) 39 | return (0); 40 | 41 | if (tree->left) 42 | height_l = 1 + tree_size_h(tree->left); 43 | 44 | if (tree->right) 45 | height_r = 1 + tree_size_h(tree->right); 46 | 47 | return (height_l + height_r); 48 | } 49 | 50 | /** 51 | * _preorder - goes through a binary tree using pre-order traversal 52 | * @tree: pointer to the root node of the tree to traverse 53 | * @node: will be last note in traverse 54 | * @height: height of tree 55 | * 56 | * Return: No Return 57 | */ 58 | void _preorder(heap_t *tree, heap_t **node, size_t height) 59 | { 60 | if (!tree) 61 | return; 62 | 63 | if (!height) 64 | *node = tree; 65 | height--; 66 | 67 | _preorder(tree->left, node, height); 68 | _preorder(tree->right, node, height); 69 | } 70 | 71 | /** 72 | * heapify - heapifies max binary heap 73 | * @root: pointer to binary heap 74 | */ 75 | void heapify(heap_t *root) 76 | { 77 | int value; 78 | heap_t *tmp1, *tmp2; 79 | 80 | if (!root) 81 | return; 82 | 83 | tmp1 = root; 84 | 85 | while (1) 86 | { 87 | if (!tmp1->left) 88 | break; 89 | if (!tmp1->right) 90 | tmp2 = tmp1->left; 91 | else 92 | { 93 | if (tmp1->left->n > tmp1->right->n) 94 | tmp2 = tmp1->left; 95 | else 96 | tmp2 = tmp1->right; 97 | } 98 | if (tmp1->n > tmp2->n) 99 | break; 100 | value = tmp1->n; 101 | tmp1->n = tmp2->n; 102 | tmp2->n = value; 103 | tmp1 = tmp2; 104 | } 105 | } 106 | 107 | /** 108 | * heap_extract - extracts the root node of a Max Binary Heap 109 | * @root: a double pointer to the root node of heap 110 | * 111 | * Return: the value stored in the root node 112 | * 0 on failure 113 | */ 114 | int heap_extract(heap_t **root) 115 | { 116 | int value; 117 | heap_t *heap_r, *node; 118 | 119 | if (!root || !*root) 120 | return (0); 121 | heap_r = *root; 122 | value = heap_r->n; 123 | if (!heap_r->left && !heap_r->right) 124 | { 125 | *root = NULL; 126 | free(heap_r); 127 | return (value); 128 | } 129 | 130 | _preorder(heap_r, &node, tree_height(heap_r)); 131 | 132 | heap_r->n = node->n; 133 | if (node->parent->right) 134 | node->parent->right = NULL; 135 | else 136 | node->parent->left = NULL; 137 | free(node); 138 | heapify(heap_r); 139 | *root = heap_r; 140 | return (value); 141 | } 142 | -------------------------------------------------------------------------------- /102-binary_tree_is_complete.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | levelorder_queue_t *create_node(binary_tree_t *node); 4 | void free_queue(levelorder_queue_t *head); 5 | void push(binary_tree_t *node, levelorder_queue_t *head, 6 | levelorder_queue_t **tail); 7 | void pop(levelorder_queue_t **head); 8 | int binary_tree_is_complete(const binary_tree_t *tree); 9 | 10 | /** 11 | * create_node - Creates a new levelorder_queue_t node. 12 | * @node: The binary tree node for the new node to contain. 13 | * 14 | * Return: If an error occurs, NULL. 15 | * Otherwise, a pointer to the new node. 16 | */ 17 | levelorder_queue_t *create_node(binary_tree_t *node) 18 | { 19 | levelorder_queue_t *new; 20 | 21 | new = malloc(sizeof(levelorder_queue_t)); 22 | if (new == NULL) 23 | return (NULL); 24 | 25 | new->node = node; 26 | new->next = NULL; 27 | 28 | return (new); 29 | } 30 | 31 | /** 32 | * free_queue - Frees a levelorder_queue_t queue. 33 | * @head: A pointer to the head of the queue. 34 | */ 35 | void free_queue(levelorder_queue_t *head) 36 | { 37 | levelorder_queue_t *tmp; 38 | 39 | while (head != NULL) 40 | { 41 | tmp = head->next; 42 | free(head); 43 | head = tmp; 44 | } 45 | } 46 | 47 | /** 48 | * push - Pushes a node to the back of a levelorder_queue_t queue. 49 | * @node: The binary tree node to print and push. 50 | * @head: A double pointer to the head of the queue. 51 | * @tail: A double pointer to the tail of the queue. 52 | * 53 | * Description: Upon malloc failure, exits with a status code of 1. 54 | */ 55 | void push(binary_tree_t *node, levelorder_queue_t *head, 56 | levelorder_queue_t **tail) 57 | { 58 | levelorder_queue_t *new; 59 | 60 | new = create_node(node); 61 | if (new == NULL) 62 | { 63 | free_queue(head); 64 | exit(1); 65 | } 66 | (*tail)->next = new; 67 | *tail = new; 68 | } 69 | 70 | /** 71 | * pop - Pops the head of a levelorder_queue_t queue. 72 | * @head: A double pointer to the head of the queue. 73 | */ 74 | void pop(levelorder_queue_t **head) 75 | { 76 | levelorder_queue_t *tmp; 77 | 78 | tmp = (*head)->next; 79 | free(*head); 80 | *head = tmp; 81 | } 82 | 83 | /** 84 | * binary_tree_is_complete - Checks if a binary tree is complete. 85 | * @tree: A pointer to the root node of the tree to traverse. 86 | * 87 | * Return: If the tree is NULL or not complete, 0. 88 | * Otherwise, 1. 89 | * 90 | * Description: Upon malloc failure, exits with a status code of 1. 91 | */ 92 | int binary_tree_is_complete(const binary_tree_t *tree) 93 | { 94 | levelorder_queue_t *head, *tail; 95 | unsigned char flag = 0; 96 | 97 | if (tree == NULL) 98 | return (0); 99 | 100 | head = tail = create_node((binary_tree_t *)tree); 101 | if (head == NULL) 102 | exit(1); 103 | 104 | while (head != NULL) 105 | { 106 | if (head->node->left != NULL) 107 | { 108 | if (flag == 1) 109 | { 110 | free_queue(head); 111 | return (0); 112 | } 113 | push(head->node->left, head, &tail); 114 | } 115 | else 116 | flag = 1; 117 | if (head->node->right != NULL) 118 | { 119 | if (flag == 1) 120 | { 121 | free_queue(head); 122 | return (0); 123 | } 124 | push(head->node->right, head, &tail); 125 | } 126 | else 127 | flag = 1; 128 | pop(&head); 129 | } 130 | return (1); 131 | } 132 | -------------------------------------------------------------------------------- /123-avl_remove.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * bal - Measures balance factor of a AVL 5 | * @tree: tree to go through 6 | * Return: balanced factor 7 | */ 8 | void bal(avl_t **tree) 9 | { 10 | int bval; 11 | 12 | if (tree == NULL || *tree == NULL) 13 | return; 14 | if ((*tree)->left == NULL && (*tree)->right == NULL) 15 | return; 16 | bal(&(*tree)->left); 17 | bal(&(*tree)->right); 18 | bval = binary_tree_balance((const binary_tree_t *)*tree); 19 | if (bval > 1) 20 | *tree = binary_tree_rotate_right((binary_tree_t *)*tree); 21 | else if (bval < -1) 22 | *tree = binary_tree_rotate_left((binary_tree_t *)*tree); 23 | } 24 | /** 25 | * successor - get the next successor i mean the min node in the right subtree 26 | * @node: tree to check 27 | * Return: the min value of this tree 28 | */ 29 | int successor(bst_t *node) 30 | { 31 | int left = 0; 32 | 33 | if (node == NULL) 34 | { 35 | return (0); 36 | } 37 | else 38 | { 39 | left = successor(node->left); 40 | if (left == 0) 41 | { 42 | return (node->n); 43 | } 44 | return (left); 45 | } 46 | 47 | } 48 | /** 49 | *remove_type - function that removes a node depending of its children 50 | *@root: node to remove 51 | *Return: 0 if it has no children or other value if it has 52 | */ 53 | int remove_type(bst_t *root) 54 | { 55 | int new_value = 0; 56 | 57 | if (!root->left && !root->right) 58 | { 59 | if (root->parent->right == root) 60 | root->parent->right = NULL; 61 | else 62 | root->parent->left = NULL; 63 | free(root); 64 | return (0); 65 | } 66 | else if ((!root->left && root->right) || (!root->right && root->left)) 67 | { 68 | if (!root->left) 69 | { 70 | if (root->parent->right == root) 71 | root->parent->right = root->right; 72 | else 73 | root->parent->left = root->right; 74 | root->right->parent = root->parent; 75 | } 76 | if (!root->right) 77 | { 78 | if (root->parent->right == root) 79 | root->parent->right = root->left; 80 | else 81 | root->parent->left = root->left; 82 | root->left->parent = root->parent; 83 | } 84 | free(root); 85 | return (0); 86 | } 87 | else 88 | { 89 | new_value = successor(root->right); 90 | root->n = new_value; 91 | return (new_value); 92 | } 93 | } 94 | /** 95 | * bst_remove - remove a node from a BST tree 96 | * @root: root of the tree 97 | * @value: node with this value to remove 98 | * Return: the tree changed 99 | */ 100 | bst_t *bst_remove(bst_t *root, int value) 101 | { 102 | int type = 0; 103 | 104 | if (root == NULL) 105 | return (NULL); 106 | if (value < root->n) 107 | bst_remove(root->left, value); 108 | else if (value > root->n) 109 | bst_remove(root->right, value); 110 | else if (value == root->n) 111 | { 112 | type = remove_type(root); 113 | if (type != 0) 114 | bst_remove(root->right, type); 115 | } 116 | else 117 | return (NULL); 118 | return (root); 119 | } 120 | 121 | /** 122 | * avl_remove - remove a node from a AVL tree 123 | * @root: root of the tree 124 | * @value: node with this value to remove 125 | * Return: the tree changed 126 | */ 127 | avl_t *avl_remove(avl_t *root, int value) 128 | { 129 | avl_t *root_a = (avl_t *) bst_remove((bst_t *) root, value); 130 | 131 | if (root_a == NULL) 132 | return (NULL); 133 | bal(&root_a); 134 | return (root_a); 135 | } 136 | -------------------------------------------------------------------------------- /binary_trees.h: -------------------------------------------------------------------------------- 1 | #ifndef BINARY_TREES_H 2 | #define BINARY_TREES_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | /** 14 | * struct binary_tree_s - Binary tree node 15 | * 16 | * @n: Integer stored in the node 17 | * @parent: Pointer to the parent node 18 | * @left: Pointer to the left child node 19 | * @right: Pointer to the right child node 20 | */ 21 | struct binary_tree_s 22 | { 23 | int n; 24 | struct binary_tree_s *parent; 25 | struct binary_tree_s *left; 26 | struct binary_tree_s *right; 27 | }; 28 | typedef struct binary_tree_s binary_tree_t; 29 | typedef struct binary_tree_s bst_t; 30 | typedef struct binary_tree_s avl_t; 31 | typedef struct binary_tree_s heap_t; 32 | 33 | /* binary_tree_print.c */ 34 | void binary_tree_print(const binary_tree_t *); 35 | 36 | /* tasks */ 37 | binary_tree_t *binary_tree_node(binary_tree_t *parent, int value); 38 | binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value); 39 | binary_tree_t *binary_tree_insert_right(binary_tree_t *parent, int value); 40 | void binary_tree_delete(binary_tree_t *tree); 41 | int binary_tree_is_leaf(const binary_tree_t *node); 42 | int binary_tree_is_root(const binary_tree_t *node); 43 | void binary_tree_preorder(const binary_tree_t *tree, void (*func)(int)); 44 | void binary_tree_inorder(const binary_tree_t *tree, void (*func)(int)); 45 | void binary_tree_postorder(const binary_tree_t *tree, void (*func)(int)); 46 | size_t binary_tree_height(const binary_tree_t *tree); 47 | size_t binary_tree_depth(const binary_tree_t *tree); 48 | size_t binary_tree_size(const binary_tree_t *tree); 49 | size_t binary_tree_leaves(const binary_tree_t *tree); 50 | size_t binary_tree_nodes(const binary_tree_t *tree); 51 | int binary_tree_balance(const binary_tree_t *tree); 52 | int binary_tree_is_full(const binary_tree_t *tree); 53 | int binary_tree_is_perfect(const binary_tree_t *tree); 54 | binary_tree_t *binary_tree_sibling(binary_tree_t *node); 55 | binary_tree_t *binary_tree_uncle(binary_tree_t *node); 56 | 57 | /* Advanced tasks */ 58 | binary_tree_t *binary_trees_ancestor(const binary_tree_t *first, 59 | const binary_tree_t *second); 60 | void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int)); 61 | int binary_tree_is_complete(const binary_tree_t *tree); 62 | binary_tree_t *binary_tree_rotate_left(binary_tree_t *tree); 63 | binary_tree_t *binary_tree_rotate_right(binary_tree_t *tree); 64 | int binary_tree_is_bst(const binary_tree_t *tree); 65 | bst_t *bst_insert(bst_t **tree, int value); 66 | bst_t *array_to_bst(int *array, size_t size); 67 | bst_t *bst_search(const bst_t *tree, int value); 68 | bst_t *bst_remove(bst_t *root, int value); 69 | int binary_tree_is_avl(const binary_tree_t *tree); 70 | avl_t *avl_insert(avl_t **tree, int value); 71 | avl_t *array_to_avl(int *array, size_t size); 72 | avl_t *avl_remove(avl_t *root, int value); 73 | avl_t *sorted_array_to_avl(int *array, size_t size); 74 | int binary_tree_is_heap(const binary_tree_t *tree); 75 | heap_t *heap_insert(heap_t **root, int value); 76 | heap_t *array_to_heap(int *array, size_t size); 77 | int heap_extract(heap_t **root); 78 | int *heap_to_sorted_array(heap_t *heap, size_t *size); 79 | 80 | /* helper functions */ 81 | int _pow_recursion(int x, int y); 82 | binary_tree_t *bta_helper(binary_tree_t *root, const binary_tree_t *first, 83 | const binary_tree_t *second); 84 | void btlo_helper(const binary_tree_t *tree, void (*func)(int), size_t level); 85 | int btic_helper(const binary_tree_t *tree, size_t index, size_t size); 86 | int btib_helper(const binary_tree_t *tree, int low, int hi); 87 | bst_t *bst_min_val(bst_t *root); 88 | int btia_helper(const binary_tree_t *tree, int low, int hi); 89 | int btih_helper(const binary_tree_t *tree); 90 | void sata_helper(avl_t **root, int *array, size_t lo, size_t hi); 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C - Binary trees 2 | 3 | 4 | 5 | A binary tree is a rooted tree that is also an ordered tree (a.k.a. plane tree) in which every node has at most two children. A rooted tree naturally imparts a notion of levels (distance from the root), thus for every node a notion of children may be defined as the nodes connected to it a level below. 6 | 7 | ## Tests :heavy_check_mark: 8 | 9 | * [Tester](./Main_tester): Folder of test files for all tasks. Provided by ALX. 10 | 11 | ## Helper File :raised_hands: 12 | 13 | 14 | 15 | ## Header File :file_folder: 16 | 17 | * [binary_trees.h](./binary_trees.h): Header file containing definitions and 18 | prototypes for all types and functions written for the project. 19 | 20 | Data Structures 21 | ``` 22 | struct binary_tree_s 23 | { 24 | int n; 25 | struct binary_tree_s *parent; 26 | struct binary_tree_s *left; 27 | struct binary_tree_s *right; 28 | }; 29 | 30 | typedef struct binary_tree_s binary_tree_t; 31 | typedef struct binary_tree_s bst_t; 32 | typedef struct binary_tree_s avl_t; 33 | typedef struct binary_tree_s heap_t; 34 | ``` 35 | 36 | Function Prototypes 37 | 38 | | File | Prototype | 39 | | -------------------------------- | ------------------------------------------------------------------------------------------------ | 40 | | `binary_tree_print.c` | `void binary_tree_print(const binary_tree_t *tree)` | 41 | | `0-binary_tree_node.c` | `binary_tree_t *binary_tree_node(binary_tree_t *parent, int value);` | 42 | | `1-binary_tree_insert_left.c` | `binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value);` | 43 | | `2-binary_tree_insert_right.c` | `binary_tree_t *binary_tree_insert_right(binary_tree_t *parent, int value);` | 44 | | `3-binary_tree_delete.c` | `void binary_tree_delete(binary_tree_t *tree);` | 45 | | `4-binary_tree_is_leaf.c` | `int binary_tree_is_leaf(const binary_tree_t *node);` | 46 | | `5-binary_tree_is_root.c` | `int binary_tree_is_root(const binary_tree_t *node);` 47 | | `6-binary_tree_preorder.c` | `void binary_tree_preorder(const binary_tree_t *tree, void (*func)(int));` | 48 | | `7-binary_tree_inorder.c` | `void binary_tree_inorder(const binary_tree_t *tree, void (*func)(int));` | 49 | | `8-binary_tree_postorder.c` | `void binary_tree_postorder(const binary_tree_t *tree, void (*func)(int));` | 50 | | `9-binary_tree_height.c` | `size_t binary_tree_height(const binary_tree_t *tree);` | 51 | | `10-binary_tree_depth.c` | `size_t binary_tree_depth(const binary_tree_t *tree);` | 52 | | `11-binary_tree_size.c` | `size_t binary_tree_size(const binary_tree_t *tree);` | 53 | | `12-binary_tree_leaves.c` | `size_t binary_tree_leaves(const binary_tree_t *tree);` | 54 | | `13-binary_tree_nodes.c` | `size_t binary_tree_nodes(const binary_tree_t *tree);` | 55 | | `14-binary_tree_balance.c` | `int binary_tree_balance(const binary_tree_t *tree);` | 56 | | `15-binary_tree_is_full.c` | `int binary_tree_is_full(const binary_tree_t *tree);` | 57 | | `16-binary_tree_is_perfect.c` | `int binary_tree_is_perfect(const binary_tree_t *tree);` | 58 | | `17-binary_tree_sibling.c` | `binary_tree_t *binary_tree_sibling(binary_tree_t *node);` | 59 | | `18-binary_tree_uncle.c` | `binary_tree_t *binary_tree_uncle(binary_tree_t *node);` | 60 | | `100-binary_trees_ancestor.c` | `binary_tree_t *binary_trees_ancestor(const binary_tree_t *first, const binary_tree_t *second);` | 61 | | `101-binary_tree_levelorder.c` | `void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int));` | 62 | | `102-binary_tree_is_complete.c` | `int binary_tree_is_complete(const binary_tree_t *tree);` | 63 | | `103-binary_tree_rotate_left.c` | `binary_tree_t *binary_tree_rotate_left(binary_tree_t *tree);` | 64 | | `104-binary_tree_rotate_right.c` | `binary_tree_t *binary_tree_rotate_right(binary_tree_t *tree);` | 65 | | `110-binary_tree_is_bst.c` | `int binary_tree_is_bst(const binary_tree_t *tree);` | 66 | | `111-bst_insert.c` | `bst_t *bst_insert(bst_t **tree, int value);` | 67 | | `112-array_to_bst.c` | `bst_t *array_to_bst(int *array, size_t size);` | 68 | | `113-bst_search.c` | `bst_t *bst_search(const bst_t *tree, int value);` | 69 | | `114-bst_remove.c` | `bst_t *bst_remove(bst_t *root, int value);` | 70 | | `120-binary_tree_is_avl.c` | `int binary_tree_is_avl(const binary_tree_t *tree);` | 71 | | `121-avl_insert.c` | `avl_t *avl_insert(avl_t **tree, int value);` | 72 | | `122-array_to_avl.c` | `avl_t *array_to_avl(int *array, size_t size);` | 73 | 74 | ## Tasks :page_with_curl: 75 | 76 | * **0. New node** 77 | * [0-binary_tree_node.c](./0-binary_tree_node.c): C function that creates a 78 | binary tree node with a given parent and value. 79 | * Returns a pointer to the new node, or `NULL` on failure. 80 | 81 | * **1. Insert left** 82 | * [1-binary_tree_insert](./1-binary_tree_insert): C function that inserts a 83 | node as the left-child of another. 84 | * Returns a pointer to the new node, or `NULL` on failure. 85 | * If the given `parent` already contains a left node, the new node takes its 86 | place and the old left-child becomes the left-child of the new node. 87 | 88 | * **2. Insert right** 89 | * [2-binary_tree_insert_right.c](./2-binary_tree_insert_right.c): C function that 90 | inserts a node as the right-child of another. 91 | * Returns a pointer to the new node, or `NULL` on failure. 92 | * If the given `parent` already contains a right node, the new node takes its 93 | place and the old right-child becomes the right-child of the new node. 94 | 95 | * **3. Delete** 96 | * [3-binary_tree_delete.c](./3-binary_tree_delete.c): C function that deletes 97 | an entire binary tree. 98 | 99 | * **4. Is leaf** 100 | * [4-binary_tree_is_leaf.c](./4-binary_tree_is_leaf.c): C function that checks 101 | if a given node is a leaf. 102 | * Returns `1` if the node is a leaf, `0` otherwise. 103 | 104 | * **5. Is root** 105 | * [5-binary_tree_is_root.c](./5-binary_tree_is_root.c): C function that checks 106 | if a given node is a root. 107 | * Returns `1` if the node is a root, `0` otherwise. 108 | 109 | * **6. Pre-order traversal** 110 | * [6-binary_tree_preorder.c](./6-binary_tree_preorder.c): C function that 111 | traverses a tree using pre-order traversal. 112 | 113 | * **7. In-order traversal** 114 | * [7-binary_tree_inorder.c](./7-binary_tree_inorder.c): C function that 115 | traverses a tree using in-order traversal. 116 | 117 | * **8. Post-order traversal** 118 | * [8-binary_tree_postorder.c](./8-binary_tree_postorder.c): C function that 119 | traverses a tree using post-order traversal. 120 | 121 | * **9. Height** 122 | * [9-binary_tree_height.c](./9-binary_tree_height.c): C function that returns 123 | the height of a binary tree. 124 | 125 | * **10. Depth** 126 | * [10-binary_tree_depth.c](./10-binary_tree_depth.c): C function that returns 127 | the depth of a given node in a binary tree. 128 | 129 | * **11. Size** 130 | * [11-binary_tree_size.c](./11-binary_tree_size.c): C function that returns 131 | the size of a binary tree. 132 | 133 | * **12. Leaves** 134 | * [12-binary_tree_leaves.c](./12-binary_tree_leaves.c): C function that returns 135 | the number of leaves in a binary tree. 136 | 137 | * **13. Nodes** 138 | * [13-binary_tree_nodes.c](./13-binary_tree_nodes.c): C function that returns 139 | the number of nodes in a binary tree with at least one child. 140 | 141 | * **14. Balance factor** 142 | * [14-binary_tree_balance.c](./14-binary_tree_balance.c): C function that 143 | returns the balance factor of a binary tree. 144 | 145 | * **15. Is full** 146 | * [15-binary_tree_is_full.c](./15-binary_tree_is_full.c): C function that 147 | checks if a binary tree is full. 148 | * Returns `1` if a tree is full, `0` otherwise. 149 | 150 | * **16. Is perfect** 151 | * [16-binary_tree_is_perfect.c](./16-binary_tree_is_perfect.c): C function 152 | that checks if a binary tree is perfect. 153 | * Returns `1` if a tree is perfect, `0` otherwise. 154 | 155 | * **17. Sibling** 156 | * [17-binary_tree_sibling.c](./17-binary_tree_sibling.c): C function that 157 | returns a pointer to the sibling of a given node in a binary tree. 158 | * Returns `NULL` if no sibling is found. 159 | 160 | * **18. Uncle** 161 | * [18-binary_tree_uncle.c](./18-binary_tree_uncle.c): C function that 162 | returns a pointer to the uncle of a given node in a binary tree. 163 | * Returns `NULL` if no uncle is found. 164 | 165 | * **19. Lowest common ancestor** 166 | * [100-binary_trees_ancestor.c](./100-binary_trees_ancestor.c): C function 167 | that returns a pointer to the lowest common ancestor node of two given nodes 168 | in a binary tree. 169 | * Returns `NULL` if no common ancestor is found. 170 | 171 | * **20. Level-order traversal** 172 | * [101-binary_tree_levelorder.c](./101-binary_tree_levelorder.c): C function 173 | that traverses a binary tree using level-order traversal. 174 | 175 | * **21. Is complete** 176 | * [102-binary_tree_is_complete.c](./102-binary_tree_is_complete.c): C function 177 | that checks if a binary tree is complete. 178 | * Returns `1` if the tree is complete, `0` otherwise. 179 | 180 | * **22. Rotate left** 181 | * [103-binary_tree_rotate_left.c](./103-binary_tree_rotate_left.c): C function 182 | that performs a left-rotation on a binary tree. 183 | * Returns a pointer to the new root node of the tree after rotation. 184 | 185 | * **23. Rotate right** 186 | * [104-binary_tree_rotate_right.c](./104-binary_tree_rotate_right.c): C function 187 | that performs a right-rotation on a binary tree. 188 | * Returns a pointer to the new root node of the tree after rotation. 189 | 190 | * **24. Is BST** 191 | * [110-binary_tree_is_bst.c](./110-binary_tree_is_bst.c): C function that 192 | checks if a binary tree is a valid binary search tree. 193 | * Returns `1` if the tree is a valid BST, `0` otherwise. 194 | 195 | * **25. BST - Insert** 196 | * [111-bst_insert.c](./111-bst_insert.c): C function that inserts a value into 197 | a binary search tree. 198 | * Returns a pointer to the new node, or `NULL` on failure. 199 | * If the tree is `NULL`, the value becomes the root node. 200 | * The value is ignored if it is already present in the tree. 201 | 202 | * **26. BST - Array to BST** 203 | * [112-array_to_bst.c](./112-array_to_bst.c): C function that builds a binary 204 | search tree from an array. 205 | * Returns a pointer to the root node of the created tree, or `NULL` on failure. 206 | 207 | * **27. BST - Search** 208 | * [113-bst_search.c](./113-bst_search.c): C function that searches for a value 209 | in a binary search tree. 210 | * If the value is matched in the BST, returns a pointer to the matched node. 211 | * Otherwise, returns `NULL`. 212 | 213 | * **28. BST - Remove** 214 | * [114-bst_remove.c](./114-bst_remove.c): C function that removes a node from 215 | a binary search tree. 216 | * Returns a pointer to the new root node of the tree after deletion. 217 | * If the node to be deleted has two children, it is replaced with its first 218 | in-order successor. 219 | 220 | * **29. Big O #BST** 221 | * [115-O](./115-O): Text file containing the average time complexities of 222 | binary search tree operations (one answer per line): 223 | * Inserting the value `n`. 224 | * Removing the node with the value `n`. 225 | * Searching for a node in a BST of size `n`. 226 | 227 | * **30. Is AVL** 228 | * [120-binary_tree_is_avl.c](./120-binary_tree_is_avl.c): C function that checks if 229 | a binary tree is a valid AVL tree. 230 | * If the tree is a valid AVL tree, returns `1`. 231 | * Otherwise, returns `0`. 232 | 233 | * **31. AVL - Insert** 234 | * [121-avl_insert.c](./121-avl_insert.c): C function that inserts a value in an AVL tree. 235 | * Returns a value to the inserted node, or `NULL` on failure. 236 | 237 | * **32. AVL - Array to AVL** 238 | * [122-array_to_avl.c](./122-array_to_avl.c): C function that builds an AVL tree 239 | from an array. 240 | * Returns a pointer to the root node of the created AVL tree, or `NULL` on failure. 241 | * Ignores duplicate values. 242 | 243 | * **35. Big O #AVL Tree** 244 | * [125-O](./125-O): Text file containing the average time complexities of AVL tree 245 | opeartions (one answer per line): 246 | * Inserting the value `n`. 247 | * Removing the node with the value `n`. 248 | * Searching for a node in an AVL tree of size `n`. 249 | 250 | * **41. Big O #Binary Heap** 251 | * [135-O](./135-O): Text file containing the average time complexities of 252 | binary heap opeartions (one answer per line): 253 | * Inserting the value `n`. 254 | * Extracting the root node. 255 | * Searching for a node in a binary heap of size `n`. 256 | 257 | ## Author 258 | - [UDO INNOCENT CHARLES](https://github.com/Innocentsax) 259 | - [PIUS OWOLABI](https://github.com/opius2017) 260 | --------------------------------------------------------------------------------