├── 135-O ├── 115-O ├── 125-O ├── 133-heap_extract.c ├── 131-heap_insert.c ├── 132-array_to_heap.c ├── 134-heap_to_sorted_array.c ├── 10-binary_tree_depth.c ├── 5-binary_tree_is_root.c ├── 17-binary_tree_sibling.c ├── 4-binary_tree_is_leaf.c ├── 11-binary_tree_size.c ├── main_files ├── 112-main.c ├── 122-main.c ├── 3-main.c ├── 2-main.c ├── 1-main.c ├── 0-main.c ├── 113-main.c ├── 11-main.c ├── 4-main.c ├── 10-main.c ├── 12-main.c ├── 114-main.c ├── 104-main.c ├── 7-main.c ├── 15-main.c ├── 5-main.c ├── 6-main.c ├── 8-main.c ├── 124-main.c ├── 13-main.c ├── 9-main.c ├── 101-main.c ├── 103-main.c ├── 110-main.c ├── 111-main.c ├── 14-main.c ├── 16-main.c ├── 123-main.c ├── 18-main.c ├── 121-main.c ├── 17-main.c ├── 130-main.c ├── 100-main.c ├── 102-main.c └── 120-main.c ├── 7-binary_tree_inorder.c ├── 18-binary_tree_uncle.c ├── 3-binary_tree_delete.c ├── 6-binary_tree_preorder.c ├── 8-binary_tree_postorder.c ├── 12-binary_tree_leaves.c ├── 13-binary_tree_nodes.c ├── 0-binary_tree_node.c ├── 112-array_to_bst.c ├── 9-binary_tree_height.c ├── 103-binary_tree_rotate_left.c ├── 104-binary_tree_rotate_right.c ├── 122-array_to_avl.c ├── 113-bst_search.c ├── 1-binary_tree_insert_left.c ├── 15-binary_tree_is_full.c ├── 2-binary_tree_insert_right.c ├── 100-binary_trees_ancestor.c ├── 14-binary_tree_balance.c ├── 111-bst_insert.c ├── 124-sorted_array_to_avl.c ├── 16-binary_tree_is_perfect.c ├── 120-binary_tree_is_avl.c ├── 121-avl_insert.c ├── 102-binary_tree_is_complete.c ├── 110-binary_tree_is_bst.c ├── 114-bst_remove.c ├── binary_tree_print.c ├── 101-binary_tree_levelorder.c ├── 130-binary_tree_is_heap.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 | -------------------------------------------------------------------------------- /133-heap_extract.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * heap_extract - extract node 4 | * @root: root of the tre to evaluate 5 | * Return: value 6 | */ 7 | int heap_extract(heap_t **root) 8 | { 9 | return (0); 10 | } 11 | -------------------------------------------------------------------------------- /131-heap_insert.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * heap_insert -function insert node in a heap tree 4 | * @root: root of the tre to evaluate 5 | * @value: value to insert 6 | * Return: pointer 7 | */ 8 | heap_t *heap_insert(heap_t **root, int value) 9 | { 10 | return (NULL); 11 | } 12 | -------------------------------------------------------------------------------- /132-array_to_heap.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * array_to_heap -function create a heap tree from array 4 | * @array: array reference 5 | * @size: size of array 6 | * Return: pointer to tree 7 | */ 8 | heap_t *array_to_heap(int *array, size_t size) 9 | { 10 | return (NULL); 11 | } 12 | -------------------------------------------------------------------------------- /134-heap_to_sorted_array.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * heap_to_sorted_array - converts a Binary Max Heap to a sorted array of int 4 | * @heap: root of the tree 5 | * @size: size from array 6 | * Return: pointer 7 | */ 8 | int *heap_to_sorted_array(heap_t *heap, size_t *size) 9 | { 10 | return (NULL); 11 | } 12 | -------------------------------------------------------------------------------- /10-binary_tree_depth.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_depth - depth of specified node from root 4 | * @tree: node to check the depth 5 | * Return: 0 is it is the root or number of depth 6 | */ 7 | size_t binary_tree_depth(const binary_tree_t *tree) 8 | { 9 | return ((tree && tree->parent) ? 1 + binary_tree_depth(tree->parent) : 0); 10 | } 11 | -------------------------------------------------------------------------------- /5-binary_tree_is_root.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_is_root - Function that checks if a node is a root 5 | * @node: Node to check 6 | * Return: 0 if is not a root 1 if it is 7 | */ 8 | 9 | int binary_tree_is_root(const binary_tree_t *node) 10 | { 11 | if (node == NULL || node->parent != NULL) 12 | return (0); 13 | return (1); 14 | } 15 | -------------------------------------------------------------------------------- /17-binary_tree_sibling.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_sibling - function that finds if a node is sibling 5 | * @node: node to check 6 | * Return: The sibling node 7 | */ 8 | 9 | binary_tree_t *binary_tree_sibling(binary_tree_t *node) 10 | { 11 | if (node == NULL || node->parent == NULL) 12 | { 13 | return (NULL); 14 | } 15 | if (node->parent->left == node) 16 | return (node->parent->right); 17 | return (node->parent->left); 18 | } 19 | -------------------------------------------------------------------------------- /4-binary_tree_is_leaf.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_is_leaf - know if a node is a leaf, that means that the node 4 | * does not have any child neither left nor right 5 | * @node: node to study 6 | * Return: 1 if it is a leaf or 0 if it is not 7 | */ 8 | int binary_tree_is_leaf(const binary_tree_t *node) 9 | { 10 | if (node == NULL) 11 | return (0); 12 | if (node->left == NULL && node->right == NULL) 13 | return (1); 14 | return (0); 15 | } 16 | -------------------------------------------------------------------------------- /11-binary_tree_size.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_size - function that return the size of a tree 4 | * @tree: tree to check 5 | * Return: size of tree 6 | */ 7 | size_t binary_tree_size(const binary_tree_t *tree) 8 | { 9 | size_t size = 0, r = 0, l = 0; 10 | 11 | if (tree == NULL) 12 | { 13 | return (0); 14 | } 15 | else 16 | { 17 | l = binary_tree_size(tree->left); 18 | r = binary_tree_size(tree->right); 19 | size = r + l + 1; 20 | } 21 | return (size); 22 | } 23 | -------------------------------------------------------------------------------- /main_files/112-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "binary_trees.h" 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: 0 on success, error code on failure 8 | */ 9 | int main(void) 10 | { 11 | bst_t *tree; 12 | int array[] = { 13 | 79, 47, 68, 87, 84, 91, 21, 32, 34, 2, 14 | 20, 22, 98, 1, 62, 95 15 | }; 16 | size_t n = sizeof(array) / sizeof(array[0]); 17 | 18 | tree = array_to_bst(array, n); 19 | if (!tree) 20 | return (1); 21 | binary_tree_print(tree); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /7-binary_tree_inorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_inorder - print elements of tree using in-order traversal 4 | * @tree: tree to go through 5 | * @func: function to use 6 | * Return: Nothing 7 | */ 8 | void binary_tree_inorder(const binary_tree_t *tree, void (*func)(int)) 9 | { 10 | if (tree == NULL || func == NULL) 11 | { 12 | return; 13 | } 14 | else 15 | { 16 | binary_tree_inorder(tree->left, func); 17 | func(tree->n); 18 | binary_tree_inorder(tree->right, func); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /18-binary_tree_uncle.c: -------------------------------------------------------------------------------- 1 | nclude "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_uncle - function that finds if a node is sibling 5 | * @node: node to check 6 | * Return: The uncle node 7 | */ 8 | 9 | binary_tree_t *binary_tree_uncle(binary_tree_t *node) 10 | { 11 | if (node == NULL || node->parent == NULL || node->parent->parent == NULL) 12 | { 13 | return (NULL); 14 | } 15 | if (node->parent->parent->left == node->parent) 16 | return (node->parent->parent->right); 17 | return (node->parent->parent->left); 18 | } 19 | -------------------------------------------------------------------------------- /3-binary_tree_delete.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_delete - free an entire tree using recursion it free the node 4 | * when the node left and right are NULL 5 | * @tree: tree to free 6 | * Return: Nothing 7 | */ 8 | void binary_tree_delete(binary_tree_t *tree) 9 | { 10 | if (tree == NULL) 11 | { 12 | return; 13 | } 14 | else 15 | { 16 | if (tree != NULL) 17 | { 18 | binary_tree_delete(tree->left); 19 | binary_tree_delete(tree->right); 20 | } 21 | free(tree); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /6-binary_tree_preorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_preorder - print elements of tree using pre-order traversal 4 | * @tree: tree to go through 5 | * @func: function to use 6 | * Return: Nothing 7 | */ 8 | void binary_tree_preorder(const binary_tree_t *tree, void (*func)(int)) 9 | { 10 | if (tree == NULL || func == NULL) 11 | { 12 | return; 13 | } 14 | else 15 | { 16 | func(tree->n); 17 | binary_tree_preorder(tree->left, func); 18 | binary_tree_preorder(tree->right, func); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /8-binary_tree_postorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_postorder - print elements of tree using post-order traversal 4 | * @tree: tree to go through 5 | * @func: function to use 6 | * Return: Nothing 7 | */ 8 | void binary_tree_postorder(const binary_tree_t *tree, void (*func)(int)) 9 | { 10 | if (tree == NULL || func == NULL) 11 | { 12 | return; 13 | } 14 | else 15 | { 16 | binary_tree_postorder(tree->left, func); 17 | binary_tree_postorder(tree->right, func); 18 | } 19 | func(tree->n); 20 | } 21 | -------------------------------------------------------------------------------- /main_files/122-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "binary_trees.h" 3 | 4 | /** 5 | * main - Entry point 6 | * 7 | * Return: 0 on success, error code on failure 8 | */ 9 | int main(void) 10 | { 11 | avl_t *tree; 12 | int array[] = { 13 | 79, 47, 68, 87, 84, 91, 21, 32, 34, 2, 14 | 20, 22, 98, 1, 62, 95 15 | }; 16 | size_t n = sizeof(array) / sizeof(array[0]); 17 | 18 | tree = array_to_avl(array, n); 19 | if (!tree) 20 | return (1); 21 | binary_tree_print(tree); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /main_files/3-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | 14 | root = binary_tree_node(NULL, 98); 15 | root->left = binary_tree_node(root, 12); 16 | root->right = binary_tree_node(root, 402); 17 | binary_tree_insert_right(root->left, 54); 18 | binary_tree_insert_right(root, 128); 19 | binary_tree_print(root); 20 | binary_tree_delete(root); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /12-binary_tree_leaves.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_leaves - function that returns the number of leaves in a tree 4 | * @tree: tree to check 5 | * Return: number of leaves 6 | */ 7 | size_t binary_tree_leaves(const binary_tree_t *tree) 8 | { 9 | size_t leaf = 0, l = 0, r = 0; 10 | 11 | if (tree == NULL) 12 | { 13 | return (0); 14 | } 15 | else 16 | { 17 | l = binary_tree_leaves(tree->left); 18 | r = binary_tree_leaves(tree->right); 19 | leaf = l + r; 20 | return ((!l && !r) ? leaf + 1 : leaf + 0); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /main_files/2-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | 14 | root = binary_tree_node(NULL, 98); 15 | root->left = binary_tree_node(root, 12); 16 | root->right = binary_tree_node(root, 402); 17 | binary_tree_print(root); 18 | printf("\n"); 19 | binary_tree_insert_right(root->left, 54); 20 | binary_tree_insert_right(root, 128); 21 | binary_tree_print(root); 22 | return (0); 23 | } 24 | -------------------------------------------------------------------------------- /13-binary_tree_nodes.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_nodes - function that returns # of nodes in a tree with children 4 | * @tree: tree to check 5 | * Return: number of nodes with children 6 | */ 7 | size_t binary_tree_nodes(const binary_tree_t *tree) 8 | { 9 | 10 | size_t node = 0; 11 | 12 | if (tree == NULL) 13 | { 14 | return (0); 15 | } 16 | else 17 | { 18 | node += ((tree->left || tree->right) ? 1 : 0); 19 | node += binary_tree_nodes(tree->left); 20 | node += binary_tree_nodes(tree->right); 21 | return (node); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /0-binary_tree_node.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_node - Create a binary node 5 | * @parent: node parent 6 | * @value: Value of the node 7 | * Return: the new node 8 | */ 9 | 10 | binary_tree_t *binary_tree_node(binary_tree_t *parent, int value) 11 | { 12 | binary_tree_t *new_node; 13 | 14 | new_node = malloc(sizeof(binary_tree_t)); 15 | if (new_node == NULL) 16 | { 17 | return (NULL); 18 | } 19 | new_node->n = value; 20 | new_node->parent = parent; 21 | new_node->left = NULL; 22 | new_node->right = NULL; 23 | return (new_node); 24 | } 25 | -------------------------------------------------------------------------------- /main_files/1-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point testing comment 2 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | 11 | int main(void) 12 | { 13 | binary_tree_t *root; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_print(root); 19 | printf("\n"); 20 | binary_tree_insert_left(root->right, 128); 21 | binary_tree_insert_left(root, 54); 22 | binary_tree_print(root); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /112-array_to_bst.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * array_to_bst - turns an array to a BST tree 4 | * @array: array to turns to BST tree 5 | * @size: size of array 6 | * Return: BST tree from array 7 | */ 8 | bst_t *array_to_bst(int *array, size_t size) 9 | { 10 | size_t i = 0; 11 | bst_t *root; 12 | 13 | root = NULL; 14 | if (size == 0) 15 | { 16 | return (NULL); 17 | } 18 | for (; i < size; i++) 19 | { 20 | if (i == 0) 21 | { 22 | bst_insert(&root, array[i]); 23 | } 24 | else 25 | { 26 | bst_insert(&root, array[i]); 27 | } 28 | } 29 | return (root); 30 | } 31 | -------------------------------------------------------------------------------- /9-binary_tree_height.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_height - Function that measures the height of a binary tree 5 | * @tree: tree to go through 6 | * Return: the height 7 | */ 8 | 9 | size_t binary_tree_height(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(tree->left) : 0; 23 | r = tree->right ? 1 + binary_tree_height(tree->right) : 0; 24 | } 25 | return ((l > r) ? l : r); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /103-binary_tree_rotate_left.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_rotate_left - Function that rotates left the binary tree. 5 | * @tree: The root node of the three 6 | * Return: Pointer node of the new node 7 | */ 8 | binary_tree_t *binary_tree_rotate_left(binary_tree_t *tree) 9 | { 10 | binary_tree_t *pivot; 11 | 12 | if (tree == NULL || tree->right == NULL) 13 | { 14 | return (NULL); 15 | } 16 | pivot = tree->right; 17 | tree->right = pivot->left; 18 | if (pivot->left != NULL) 19 | { 20 | pivot->left->parent = tree; 21 | } 22 | pivot->left = tree; 23 | pivot->parent = tree->parent; 24 | tree->parent = pivot; 25 | return (pivot); 26 | } 27 | -------------------------------------------------------------------------------- /main_files/0-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "binary_trees.h" 3 | 4 | /** 5 | * main - Entry point testing comment 6 | * 7 | * Return: Always 0 (Success) 8 | */ 9 | int main(void) 10 | { 11 | binary_tree_t *root; 12 | 13 | root = binary_tree_node(NULL, 98); 14 | 15 | root->left = binary_tree_node(root, 12); 16 | root->left->left = binary_tree_node(root->left, 6); 17 | root->left->right = binary_tree_node(root->left, 16); 18 | 19 | root->right = binary_tree_node(root, 402); 20 | root->right->left = binary_tree_node(root->right, 256); 21 | root->right->right = binary_tree_node(root->right, 512); 22 | 23 | binary_tree_print(root); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /104-binary_tree_rotate_right.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_rotate_right - Function that rotates right the binary tree. 5 | * @tree: The root node of the three 6 | * Return: Pointer node of the new node 7 | */ 8 | binary_tree_t *binary_tree_rotate_right(binary_tree_t *tree) 9 | { 10 | binary_tree_t *pivot; 11 | 12 | if (tree == NULL || tree->left == NULL) 13 | { 14 | return (NULL); 15 | } 16 | pivot = tree->left; 17 | tree->left = pivot->right; 18 | if (pivot->right != NULL) 19 | { 20 | pivot->right->parent = tree; 21 | } 22 | pivot->right = tree; 23 | pivot->parent = tree->parent; 24 | tree->parent = pivot; 25 | return (pivot); 26 | } 27 | -------------------------------------------------------------------------------- /122-array_to_avl.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * array_to_avl - turns an array to a avl tree 5 | * @array: array to turns to AVL tree 6 | * @size: size of array 7 | * Return: AVL tree from array 8 | */ 9 | avl_t *array_to_avl(int *array, size_t size) 10 | { 11 | size_t i, j = 0; 12 | avl_t *root; 13 | 14 | root = NULL; 15 | if (size == 0) 16 | { 17 | return (NULL); 18 | } 19 | for (i = 0; i < size; i++) 20 | { 21 | for (j = 0; j < i; j++) 22 | { 23 | if (array[j] == array[i]) 24 | break; 25 | } 26 | if (j == i) 27 | { 28 | if (avl_insert(&root, array[i]) == NULL) 29 | { 30 | return (NULL); 31 | } 32 | } 33 | } 34 | return (root); 35 | } 36 | -------------------------------------------------------------------------------- /113-bst_search.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * bst_search - search for a special value in the tree and return the node 4 | * @tree: tree to go through 5 | * @value: value to search 6 | * Return: the node with the value or NULL if the value is not in the tree 7 | */ 8 | bst_t *bst_search(const bst_t *tree, int value) 9 | { 10 | bst_t *found; 11 | 12 | if (tree == NULL) 13 | return (NULL); 14 | 15 | if (value < tree->n) 16 | { 17 | found = bst_search(tree->left, value); 18 | } 19 | else if (value > tree->n) 20 | { 21 | found = bst_search(tree->right, value); 22 | } 23 | else if (value == tree->n) 24 | return ((bst_t *)tree); 25 | else 26 | return (NULL); 27 | return (found); 28 | } 29 | -------------------------------------------------------------------------------- /main_files/113-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: 0 on success, error code on failure 9 | */ 10 | int main(void) 11 | { 12 | bst_t *tree; 13 | int array[] = { 14 | 79, 47, 68, 87, 84, 91, 21, 32, 34, 2, 15 | 20, 22, 98, 1, 62, 95 16 | }; 17 | size_t n = sizeof(array) / sizeof(array[0]); 18 | bst_t *node; 19 | 20 | tree = array_to_bst(array, n); 21 | if (!tree) 22 | return (1); 23 | binary_tree_print(tree); 24 | node = bst_search(tree, 32); 25 | printf("Found: %d\n", node->n); 26 | binary_tree_print(node); 27 | node = bst_search(tree, 512); 28 | printf("Node should be nil -> %p\n", (void *)node); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /1-binary_tree_insert_left.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_insert_left - add a node in the left of the parent 4 | * if it exists it move down one level and add the new node first 5 | * @parent: parent of the specified node 6 | * @value: value of the node 7 | * Return: NULL if it fails or the new node 8 | */ 9 | binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value) 10 | { 11 | binary_tree_t *new_node; 12 | 13 | if (parent == NULL) 14 | { 15 | return (NULL); 16 | } 17 | 18 | new_node = binary_tree_node(parent, value); 19 | if (new_node == NULL) 20 | { 21 | return (NULL); 22 | } 23 | if (parent->left != NULL) 24 | { 25 | new_node->left = parent->left; 26 | parent->left->parent = new_node; 27 | } 28 | parent->left = new_node; 29 | return (new_node); 30 | } 31 | -------------------------------------------------------------------------------- /15-binary_tree_is_full.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_is_full - function that says if a tree is full or not 4 | * a tree is full if it has tow or none children 5 | * @tree: tree to check 6 | * Return: 1 if it is a full 0 if not 7 | */ 8 | int binary_tree_is_full(const binary_tree_t *tree) 9 | { 10 | int left = 0, right = 0; 11 | 12 | if (tree == NULL) 13 | { 14 | return (0); 15 | } 16 | else 17 | { 18 | if (tree->left && tree->right) 19 | { 20 | left = binary_tree_is_full(tree->left); 21 | right = binary_tree_is_full(tree->right); 22 | if (left == 0 || right == 0) 23 | { 24 | return (0); 25 | } 26 | return (1); 27 | } 28 | else if (!tree->left && !tree->right) 29 | { 30 | return (1); 31 | } 32 | else 33 | { 34 | return (0); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /2-binary_tree_insert_right.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_insert_right - add a node in the right of the parent 4 | * if it exists it move down one level and add the new node first 5 | * @parent: parent of the specified node 6 | * @value: value of the node 7 | * Return: NULL if it fails or the new node 8 | */ 9 | 10 | binary_tree_t *binary_tree_insert_right(binary_tree_t *parent, int value) 11 | { 12 | binary_tree_t *new_node; 13 | 14 | if (parent == NULL) 15 | { 16 | return (NULL); 17 | } 18 | 19 | new_node = binary_tree_node(parent, value); 20 | if (new_node == NULL) 21 | { 22 | return (NULL); 23 | } 24 | if (parent->right != NULL) 25 | { 26 | new_node->right = parent->right; 27 | parent->right->parent = new_node; 28 | } 29 | parent->right = new_node; 30 | return (new_node); 31 | } 32 | -------------------------------------------------------------------------------- /main_files/11-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | size_t size; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | size = binary_tree_size(root); 23 | printf("Size of %d: %lu\n", root->n, size); 24 | size = binary_tree_size(root->right); 25 | printf("Size of %d: %lu\n", root->right->n, size); 26 | size = binary_tree_size(root->left->right); 27 | printf("Size of %d: %lu\n", root->left->right->n, size); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /main_files/4-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int ret; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | ret = binary_tree_is_leaf(root); 23 | printf("Is %d a leaf: %d\n", root->n, ret); 24 | ret = binary_tree_is_leaf(root->right); 25 | printf("Is %d a leaf: %d\n", root->right->n, ret); 26 | ret = binary_tree_is_leaf(root->right->right); 27 | printf("Is %d a leaf: %d\n", root->right->right->n, ret); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /main_files/10-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | size_t depth; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | depth = binary_tree_depth(root); 23 | printf("Depth of %d: %lu\n", root->n, depth); 24 | depth = binary_tree_depth(root->right); 25 | printf("Depth of %d: %lu\n", root->right->n, depth); 26 | depth = binary_tree_depth(root->left->right); 27 | printf("Depth of %d: %lu\n", root->left->right->n, depth); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /main_files/12-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | size_t leaves; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | leaves = binary_tree_leaves(root); 23 | printf("Leaves in %d: %lu\n", root->n, leaves); 24 | leaves = binary_tree_leaves(root->right); 25 | printf("Leaves in %d: %lu\n", root->right->n, leaves); 26 | leaves = binary_tree_leaves(root->left->right); 27 | printf("Leaves in %d: %lu\n", root->left->right->n, leaves); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /main_files/114-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: 0 on success, error code on failure 9 | */ 10 | int main(void) 11 | { 12 | bst_t *tree; 13 | int array[] = { 14 | 79, 47, 68, 87, 84, 91, 21, 32, 34, 2, 15 | 20, 22, 98, 1, 62, 95 16 | }; 17 | size_t n = sizeof(array) / sizeof(array[0]); 18 | 19 | tree = array_to_bst(array, n); 20 | if (!tree) 21 | return (1); 22 | binary_tree_print(tree); 23 | 24 | tree = bst_remove(tree, 79); 25 | printf("Removed 79...\n"); 26 | binary_tree_print(tree); 27 | 28 | tree = bst_remove(tree, 21); 29 | printf("Removed 21...\n"); 30 | binary_tree_print(tree); 31 | 32 | tree = bst_remove(tree, 68); 33 | printf("Removed 68...\n"); 34 | binary_tree_print(tree); 35 | binary_tree_delete(tree); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /main_files/104-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: 0 on success, error code on failure 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | 14 | root = binary_tree_node(NULL, 98); 15 | root->left = binary_tree_node(root, 64); 16 | root->left->left = binary_tree_node(root->left, 32); 17 | binary_tree_print(root); 18 | printf("Rotate-right %d\n", root->n); 19 | root = binary_tree_rotate_right(root); 20 | binary_tree_print(root); 21 | printf("\n"); 22 | 23 | root->left->left = binary_tree_node(root->left, 20); 24 | root->left->right = binary_tree_node(root->left, 56); 25 | binary_tree_print(root); 26 | printf("Rotate-right %d\n", root->n); 27 | root = binary_tree_rotate_right(root); 28 | binary_tree_print(root); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /main_files/7-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * print_num - Prints a number 7 | * 8 | * @n: Number to be printed 9 | */ 10 | void print_num(int n) 11 | { 12 | printf("%d\n", n); 13 | } 14 | 15 | /** 16 | * main - Entry point 17 | * 18 | * Return: Always 0 (Success) 19 | */ 20 | int main(void) 21 | { 22 | binary_tree_t *root; 23 | 24 | root = binary_tree_node(NULL, 98); 25 | root->left = binary_tree_node(root, 12); 26 | root->right = binary_tree_node(root, 402); 27 | root->left->left = binary_tree_node(root->left, 6); 28 | root->left->right = binary_tree_node(root->left, 56); 29 | root->right->left = binary_tree_node(root->right, 256); 30 | root->right->right = binary_tree_node(root->right, 512); 31 | 32 | binary_tree_print(root); 33 | binary_tree_inorder(root, &print_num); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /main_files/15-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int full; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | root->left->left = binary_tree_node(root->left, 10); 21 | binary_tree_print(root); 22 | 23 | full = binary_tree_is_full(root); 24 | printf("Is %d full: %d\n", root->n, full); 25 | full = binary_tree_is_full(root->left); 26 | printf("Is %d full: %d\n", root->left->n, full); 27 | full = binary_tree_is_full(root->right); 28 | printf("Is %d full: %d\n", root->right->n, full); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /main_files/5-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int ret; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | ret = binary_tree_is_root(root); 23 | printf("Is %d a root: %d\n", root->n, ret); 24 | ret = binary_tree_is_root(root->right); 25 | printf("Is %d a root: %d\n", root->right->n, ret); 26 | ret = binary_tree_is_root(root->right->right); 27 | printf("Is %d a root: %d\n", root->right->right->n, ret); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /main_files/6-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * print_num - Prints a number 7 | * 8 | * @n: Number to be printed 9 | */ 10 | void print_num(int n) 11 | { 12 | printf("%d\n", n); 13 | } 14 | 15 | /** 16 | * main - Entry point 17 | * 18 | * Return: Always 0 (Success) 19 | */ 20 | int main(void) 21 | { 22 | binary_tree_t *root; 23 | 24 | root = binary_tree_node(NULL, 98); 25 | root->left = binary_tree_node(root, 12); 26 | root->right = binary_tree_node(root, 402); 27 | root->left->left = binary_tree_node(root->left, 6); 28 | root->left->right = binary_tree_node(root->left, 56); 29 | root->right->left = binary_tree_node(root->right, 256); 30 | root->right->right = binary_tree_node(root->right, 512); 31 | 32 | binary_tree_print(root); 33 | binary_tree_preorder(root, &print_num); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /main_files/8-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * print_num - Prints a number 7 | * 8 | * @n: Number to be printed 9 | */ 10 | void print_num(int n) 11 | { 12 | printf("%d\n", n); 13 | } 14 | 15 | /** 16 | * main - Entry point 17 | * 18 | * Return: Always 0 (Success) 19 | */ 20 | int main(void) 21 | { 22 | binary_tree_t *root; 23 | 24 | root = binary_tree_node(NULL, 98); 25 | root->left = binary_tree_node(root, 12); 26 | root->right = binary_tree_node(root, 402); 27 | root->left->left = binary_tree_node(root->left, 6); 28 | root->left->right = binary_tree_node(root->left, 56); 29 | root->right->left = binary_tree_node(root->right, 256); 30 | root->right->right = binary_tree_node(root->right, 512); 31 | 32 | binary_tree_print(root); 33 | binary_tree_postorder(root, &print_num); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /main_files/124-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * print_array - Prints an array of integers 7 | * 8 | * @array: The array to be printed 9 | * @size: Size of the array 10 | */ 11 | void print_array(const int *array, size_t size) 12 | { 13 | size_t i; 14 | 15 | for (i = 0; i < size; ++i) 16 | printf("(%03d)", array[i]); 17 | printf("\n"); 18 | } 19 | 20 | /** 21 | * main - Entry point 22 | * 23 | * Return: 0 on success, error code on failure 24 | */ 25 | int main(void) 26 | { 27 | avl_t *tree; 28 | int array[] = { 29 | 1, 2, 20, 21, 22, 32, 34, 47, 62, 68, 30 | 79, 84, 87, 91, 95, 98 31 | }; 32 | size_t n = sizeof(array) / sizeof(array[0]); 33 | 34 | tree = sorted_array_to_avl(array, n); 35 | if (!tree) 36 | return (1); 37 | print_array(array, n); 38 | binary_tree_print(tree); 39 | return (0); 40 | } 41 | -------------------------------------------------------------------------------- /main_files/13-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | size_t nodes; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | nodes = binary_tree_nodes(root); 23 | printf("Nodes in %d: %lu\n", root->n, nodes); 24 | nodes = binary_tree_nodes(root->right); 25 | printf("Nodes in %d: %lu\n", root->right->n, nodes); 26 | nodes = binary_tree_nodes(root->left->right); 27 | printf("Nodes in %d: %lu\n", root->left->right->n, nodes); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /100-binary_trees_ancestor.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_trees_ancestor - function that checks an ancestor 5 | * @first: First node 6 | * @second: Second node 7 | * Return: the node of the ancestor 8 | */ 9 | 10 | binary_tree_t *binary_trees_ancestor(const binary_tree_t *first, 11 | const binary_tree_t *second) 12 | { 13 | binary_tree_t *p, *q; 14 | 15 | if (first == NULL || second == NULL) 16 | { 17 | return (NULL); 18 | } 19 | if (first == second) 20 | { 21 | return ((binary_tree_t *)first); 22 | } 23 | 24 | p = first->parent; 25 | q = second->parent; 26 | if (p == NULL || first == q || (!p->parent && q)) 27 | { 28 | return (binary_trees_ancestor(first, q)); 29 | } 30 | else if (q == NULL || p == second || (!q->parent && p)) 31 | { 32 | return (binary_trees_ancestor(p, second)); 33 | } 34 | return (binary_trees_ancestor(p, q)); 35 | } 36 | -------------------------------------------------------------------------------- /main_files/9-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | size_t height; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_print(root); 21 | 22 | height = binary_tree_height(root); 23 | printf("Height from %d: %lu\n", root->n, height); 24 | height = binary_tree_height(root->right); 25 | printf("Height from %d: %lu\n", root->right->n, height); 26 | height = binary_tree_height(root->left->right); 27 | printf("Height from %d: %lu\n", root->left->right->n, height); 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /main_files/101-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * print_num - Prints a number 7 | * 8 | * @n: Number to be printed 9 | */ 10 | void print_num(int n) 11 | { 12 | printf("%d\n", n); 13 | } 14 | 15 | /** 16 | * main - Entry point 17 | * 18 | * Return: Always 0 (Success) 19 | */ 20 | int main(void) 21 | { 22 | binary_tree_t *root; 23 | 24 | root = binary_tree_node(NULL, 98); 25 | root->left = binary_tree_node(root, 12); 26 | root->right = binary_tree_node(root, 402); 27 | root->left->left = binary_tree_node(root->left, 6); 28 | root->left->right = binary_tree_node(root->left, 56); 29 | root->right->left = binary_tree_node(root->right, 256); 30 | root->right->right = binary_tree_node(root->right, 512); 31 | 32 | binary_tree_print(root); 33 | binary_tree_levelorder(root, &print_num); 34 | binary_tree_delete(root); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /main_files/103-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: 0 on success, error code on failure 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | 14 | root = binary_tree_node(NULL, 98); 15 | root->right = binary_tree_node(root, 128); 16 | root->right->right = binary_tree_node(root->right, 402); 17 | binary_tree_print(root); 18 | printf("Rotate-left %d\n", root->n); 19 | root = binary_tree_rotate_left(root); 20 | binary_tree_print(root); 21 | printf("\n"); 22 | 23 | root->right->right = binary_tree_node(root->right, 450); 24 | root->right->left = binary_tree_node(root->right, 420); 25 | binary_tree_print(root); 26 | printf("Rotate-left %d\n", root->n); 27 | root = binary_tree_rotate_left(root); 28 | binary_tree_print(root); 29 | return (0); 30 | } 31 | -------------------------------------------------------------------------------- /main_files/110-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int bst; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 128); 18 | root->left->right = binary_tree_node(root->left, 54); 19 | root->right->right = binary_tree_node(root, 402); 20 | root->left->left = binary_tree_node(root->left, 10); 21 | 22 | binary_tree_print(root); 23 | bst = binary_tree_is_bst(root); 24 | printf("Is %d bst: %d\n", root->n, bst); 25 | bst = binary_tree_is_bst(root->left); 26 | printf("Is %d bst: %d\n", root->left->n, bst); 27 | 28 | root->right->left = binary_tree_node(root->right, 97); 29 | binary_tree_print(root); 30 | bst = binary_tree_is_bst(root); 31 | printf("Is %d bst: %d\n", root->n, bst); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /main_files/111-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | bst_t *root; 13 | bst_t *node; 14 | 15 | root = NULL; 16 | node = bst_insert(&root, 98); 17 | printf("Inserted: %d\n", node->n); 18 | node = bst_insert(&root, 402); 19 | printf("Inserted: %d\n", node->n); 20 | node = bst_insert(&root, 12); 21 | printf("Inserted: %d\n", node->n); 22 | node = bst_insert(&root, 46); 23 | printf("Inserted: %d\n", node->n); 24 | node = bst_insert(&root, 128); 25 | printf("Inserted: %d\n", node->n); 26 | node = bst_insert(&root, 256); 27 | printf("Inserted: %d\n", node->n); 28 | node = bst_insert(&root, 512); 29 | printf("Inserted: %d\n", node->n); 30 | node = bst_insert(&root, 1); 31 | printf("Inserted: %d\n", node->n); 32 | node = bst_insert(&root, 128); 33 | printf("Node should be nil -> %p\n", (void *)node); 34 | binary_tree_print(root); 35 | return (0); 36 | } 37 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /main_files/14-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int balance; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | binary_tree_insert_left(root, 45); 21 | binary_tree_insert_right(root->left, 50); 22 | binary_tree_insert_left(root->left->left, 10); 23 | binary_tree_insert_left(root->left->left->left, 8); 24 | binary_tree_print(root); 25 | 26 | balance = binary_tree_balance(root); 27 | printf("Balance of %d: %+d\n", root->n, balance); 28 | balance = binary_tree_balance(root->right); 29 | printf("Balance of %d: %+d\n", root->right->n, balance); 30 | balance = binary_tree_balance(root->left->left->right); 31 | printf("Balance of %d: %+d\n", root->left->left->right->n, balance); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /main_files/16-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int perfect; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 402); 18 | binary_tree_insert_right(root->left, 54); 19 | binary_tree_insert_right(root, 128); 20 | root->left->left = binary_tree_node(root->left, 10); 21 | root->right->left = binary_tree_node(root->right, 10); 22 | 23 | binary_tree_print(root); 24 | perfect = binary_tree_is_perfect(root); 25 | printf("Perfect: %d\n\n", perfect); 26 | 27 | root->right->right->left = binary_tree_node(root->right->right, 10); 28 | binary_tree_print(root); 29 | perfect = binary_tree_is_perfect(root); 30 | printf("Perfect: %d\n\n", perfect); 31 | 32 | root->right->right->right = binary_tree_node(root->right->right, 10); 33 | binary_tree_print(root); 34 | perfect = binary_tree_is_perfect(root); 35 | printf("Perfect: %d\n", perfect); 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /111-bst_insert.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * bst_insert - insert nodes in order to create a BST tree 4 | * @tree: tree to create with type BST 5 | * @value: value of node to insert 6 | * Return: BST tree 7 | */ 8 | bst_t *bst_insert(bst_t **tree, int value) 9 | { 10 | bst_t *new, *temp; 11 | binary_tree_t *aux; 12 | 13 | if (tree == NULL) 14 | return (NULL); 15 | 16 | if (*tree == NULL) 17 | { 18 | aux = binary_tree_node((binary_tree_t *)(*tree), value); 19 | new = (bst_t *)aux; 20 | *tree = new; 21 | } 22 | else 23 | { 24 | temp = *tree; 25 | if (value < temp->n) 26 | { 27 | if (temp->left) 28 | new = bst_insert(&temp->left, value); 29 | else 30 | { 31 | aux = binary_tree_node((binary_tree_t *)temp, value); 32 | new = temp->left = (bst_t *)aux; 33 | } 34 | } 35 | else if (value > temp->n) 36 | { 37 | if (temp->right) 38 | new = bst_insert(&temp->right, value); 39 | else 40 | { 41 | aux = binary_tree_node((binary_tree_t *)temp, value); 42 | new = temp->right = aux; 43 | } 44 | } 45 | else 46 | return (NULL); 47 | } 48 | return (new); 49 | } 50 | -------------------------------------------------------------------------------- /main_files/123-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | 6 | /** 7 | * main - Entry point 8 | * 9 | * Return: 0 on success, error code on failure 10 | */ 11 | int main(void) 12 | { 13 | avl_t *tree; 14 | int array[] = { 15 | 79, 47, 68, 87, 84, 91, 21, 32, 34, 2, 16 | 20, 22, 98, 1, 62, 95 17 | }; 18 | size_t n = sizeof(array) / sizeof(array[0]); 19 | 20 | tree = array_to_avl(array, n); 21 | if (!tree) 22 | return (1); 23 | binary_tree_print(tree); 24 | 25 | tree = avl_remove(tree, 47); 26 | printf("Removed 47...\n"); 27 | binary_tree_print(tree); 28 | 29 | tree = avl_remove(tree, 79); 30 | printf("Removed 79...\n"); 31 | binary_tree_print(tree); 32 | 33 | tree = avl_remove(tree, 32); 34 | printf("Removed 32...\n"); 35 | binary_tree_print(tree); 36 | 37 | tree = avl_remove(tree, 34); 38 | printf("Removed 34...\n"); 39 | binary_tree_print(tree); 40 | 41 | tree = avl_remove(tree, 22); 42 | printf("Removed 22...\n"); 43 | binary_tree_print(tree); 44 | binary_tree_delete(tree); 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /16-binary_tree_is_perfect.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * tree_is_perfect - function that says if a tree is perfect or not 4 | * it has to be the same quantity of levels in left as right, and also 5 | * each node has to have 2 nodes or none 6 | * @tree: tree to check 7 | * Return: 0 if is not a perfect or other number that is the level of height 8 | */ 9 | int tree_is_perfect(const binary_tree_t *tree) 10 | { 11 | int l = 0, r = 0; 12 | 13 | if (tree->left && tree->right) 14 | { 15 | l = 1 + tree_is_perfect(tree->left); 16 | r = 1 + tree_is_perfect(tree->right); 17 | if (r == l && r != 0 && l != 0) 18 | return (r); 19 | return (0); 20 | } 21 | else if (!tree->left && !tree->right) 22 | { 23 | return (1); 24 | } 25 | else 26 | { 27 | return (0); 28 | } 29 | } 30 | /** 31 | * binary_tree_is_perfect - perfect or not a tree 32 | * @tree: tree to check 33 | * Return: 1 is it is or 0 if not 34 | */ 35 | int binary_tree_is_perfect(const binary_tree_t *tree) 36 | { 37 | int result = 0; 38 | 39 | if (tree == NULL) 40 | { 41 | return (0); 42 | } 43 | else 44 | { 45 | result = tree_is_perfect(tree); 46 | if (result != 0) 47 | { 48 | return (1); 49 | } 50 | return (0); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /main_files/18-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | binary_tree_t *uncle; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 128); 18 | root->left->right = binary_tree_node(root->left, 54); 19 | root->right->right = binary_tree_node(root->right, 402); 20 | root->left->left = binary_tree_node(root->left, 10); 21 | root->right->left = binary_tree_node(root->right, 110); 22 | root->right->right->left = binary_tree_node(root->right->right, 200); 23 | root->right->right->right = binary_tree_node(root->right->right, 512); 24 | 25 | binary_tree_print(root); 26 | uncle = binary_tree_uncle(root->right->left); 27 | printf("Uncle of %d: %d\n", root->right->left->n, uncle->n); 28 | uncle = binary_tree_uncle(root->left->right); 29 | printf("Uncle of %d: %d\n", root->left->right->n, uncle->n); 30 | uncle = binary_tree_uncle(root->left); 31 | printf("Uncle of %d: %p\n", root->left->n, (void *)uncle); 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /main_files/121-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: 0 on success, error code on failure 9 | */ 10 | int main(void) 11 | { 12 | avl_t *root; 13 | avl_t *node; 14 | 15 | root = NULL; 16 | node = avl_insert(&root, 98); 17 | printf("Inserted: %d\n", node->n); 18 | binary_tree_print(root); 19 | node = avl_insert(&root, 402); 20 | printf("\nInserted: %d\n", node->n); 21 | binary_tree_print(root); 22 | node = avl_insert(&root, 12); 23 | printf("\nInserted: %d\n", node->n); 24 | binary_tree_print(root); 25 | node = avl_insert(&root, 46); 26 | printf("\nInserted: %d\n", node->n); 27 | binary_tree_print(root); 28 | node = avl_insert(&root, 128); 29 | printf("\nInserted: %d\n", node->n); 30 | binary_tree_print(root); 31 | node = avl_insert(&root, 256); 32 | printf("\nInserted: %d\n", node->n); 33 | binary_tree_print(root); 34 | node = avl_insert(&root, 512); 35 | printf("\nInserted: %d\n", node->n); 36 | binary_tree_print(root); 37 | node = avl_insert(&root, 50); 38 | printf("\nInserted: %d\n", node->n); 39 | binary_tree_print(root); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /main_files/17-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | binary_tree_t *sibling; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 128); 18 | root->left->right = binary_tree_node(root->left, 54); 19 | root->right->right = binary_tree_node(root->right, 402); 20 | root->left->left = binary_tree_node(root->left, 10); 21 | root->right->left = binary_tree_node(root->right, 110); 22 | root->right->right->left = binary_tree_node(root->right->right, 200); 23 | root->right->right->right = binary_tree_node(root->right->right, 512); 24 | 25 | binary_tree_print(root); 26 | sibling = binary_tree_sibling(root->left); 27 | printf("Sibling of %d: %d\n", root->left->n, sibling->n); 28 | sibling = binary_tree_sibling(root->right->left); 29 | printf("Sibling of %d: %d\n", root->right->left->n, sibling->n); 30 | sibling = binary_tree_sibling(root->left->right); 31 | printf("Sibling of %d: %d\n", root->left->right->n, sibling->n); 32 | sibling = binary_tree_sibling(root); 33 | printf("Sibling of %d: %p\n", root->n, (void *)sibling); 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /main_files/130-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * basic_tree - Build a basic binary tree 7 | * 8 | * Return: A pointer to the created tree 9 | */ 10 | binary_tree_t *basic_tree(void) 11 | { 12 | binary_tree_t *root; 13 | 14 | root = binary_tree_node(NULL, 98); 15 | root->left = binary_tree_node(root, 90); 16 | root->right = binary_tree_node(root, 85); 17 | root->left->right = binary_tree_node(root->left, 80); 18 | root->left->left = binary_tree_node(root->left, 79); 19 | return (root); 20 | } 21 | 22 | /** 23 | * main - Entry point 24 | * 25 | * Return: Always 0 (Success) 26 | */ 27 | int main(void) 28 | { 29 | binary_tree_t *root; 30 | int heap; 31 | 32 | root = basic_tree(); 33 | 34 | binary_tree_print(root); 35 | heap = binary_tree_is_heap(root); 36 | printf("Is %d heap: %d\n", root->n, heap); 37 | heap = binary_tree_is_heap(root->left); 38 | printf("Is %d heap: %d\n", root->left->n, heap); 39 | 40 | root->right->left = binary_tree_node(root->right, 97); 41 | binary_tree_print(root); 42 | heap = binary_tree_is_heap(root); 43 | printf("Is %d heap: %d\n", root->n, heap); 44 | 45 | root = basic_tree(); 46 | root->right->right = binary_tree_node(root->right, 79); 47 | binary_tree_print(root); 48 | heap = binary_tree_is_heap(root); 49 | printf("Is %d heap: %d\n", root->n, heap); 50 | return (0); 51 | } 52 | -------------------------------------------------------------------------------- /main_files/100-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * launch_test - Test ancestor function and print informations 7 | * 8 | * @n1: First node 9 | * @n2: Second node 10 | */ 11 | void launch_test(binary_tree_t *n1, binary_tree_t *n2) 12 | { 13 | binary_tree_t *ancestor; 14 | 15 | ancestor = binary_trees_ancestor(n1, n2); 16 | printf("Ancestor of [%d] & [%d]: ", n1->n, n2->n); 17 | if (!ancestor) 18 | printf("(nil)\n"); 19 | else 20 | printf("%d\n", ancestor->n); 21 | } 22 | 23 | /** 24 | * main - Entry point 25 | * 26 | * Return: Always 0 (Success) 27 | */ 28 | int main(void) 29 | { 30 | binary_tree_t *root; 31 | 32 | root = binary_tree_node(NULL, 98); 33 | root->left = binary_tree_node(root, 12); 34 | root->right = binary_tree_node(root, 402); 35 | root->left->right = binary_tree_node(root->left, 54); 36 | root->right->right = binary_tree_node(root->right, 128); 37 | root->left->left = binary_tree_node(root->left, 10); 38 | root->right->left = binary_tree_node(root->right, 45); 39 | root->right->right->left = binary_tree_node(root->right->right, 92); 40 | root->right->right->right = binary_tree_node(root->right->right, 65); 41 | binary_tree_print(root); 42 | 43 | launch_test(root->left, root->right); 44 | launch_test(root->right->left, root->right->right->right); 45 | launch_test(root->right->right, root->right->right->right); 46 | return (0); 47 | } 48 | -------------------------------------------------------------------------------- /main_files/102-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * main - Entry point 7 | * 8 | * Return: Always 0 (Success) 9 | */ 10 | int main(void) 11 | { 12 | binary_tree_t *root; 13 | int complete; 14 | 15 | root = binary_tree_node(NULL, 98); 16 | root->left = binary_tree_node(root, 12); 17 | root->right = binary_tree_node(root, 128); 18 | root->left->right = binary_tree_node(root->left, 54); 19 | root->right->right = binary_tree_node(root, 402); 20 | root->left->left = binary_tree_node(root->left, 10); 21 | root->right->left = binary_tree_node(root->right, 45); 22 | 23 | binary_tree_print(root); 24 | complete = binary_tree_is_complete(root); 25 | printf("Is %d complete: %d\n", root->n, complete); 26 | complete = binary_tree_is_complete(root->left); 27 | printf("Is %d complete: %d\n", root->left->n, complete); 28 | 29 | root->right->left = binary_tree_node(root->right, 112); 30 | binary_tree_print(root); 31 | complete = binary_tree_is_complete(root); 32 | printf("Is %d complete: %d\n", root->n, complete); 33 | 34 | root->left->left->left = binary_tree_node(root->left->left, 8); 35 | binary_tree_print(root); 36 | complete = binary_tree_is_complete(root); 37 | printf("Is %d complete: %d\n", root->n, complete); 38 | 39 | root->left->right->left = binary_tree_node(root->left->right, 23); 40 | binary_tree_print(root); 41 | complete = binary_tree_is_complete(root); 42 | printf("Is %d complete: %d\n", root->n, complete); 43 | 44 | binary_tree_delete(root); 45 | return (0); 46 | } 47 | -------------------------------------------------------------------------------- /main_files/120-main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary_trees.h" 4 | 5 | /** 6 | * basic_tree - Build a basic binary tree 7 | * 8 | * Return: A pointer to the created tree 9 | */ 10 | binary_tree_t *basic_tree(void) 11 | { 12 | binary_tree_t *root; 13 | 14 | root = binary_tree_node(NULL, 98); 15 | root->left = binary_tree_node(root, 12); 16 | root->right = binary_tree_node(root, 128); 17 | root->left->right = binary_tree_node(root->left, 54); 18 | root->right->right = binary_tree_node(root, 402); 19 | root->left->left = binary_tree_node(root->left, 10); 20 | return (root); 21 | } 22 | 23 | /** 24 | * main - Entry point 25 | * 26 | * Return: Always 0 (Success) 27 | */ 28 | int main(void) 29 | { 30 | binary_tree_t *root; 31 | int avl; 32 | 33 | root = basic_tree(); 34 | 35 | binary_tree_print(root); 36 | avl = binary_tree_is_avl(root); 37 | printf("Is %d avl: %d\n", root->n, avl); 38 | avl = binary_tree_is_avl(root->left); 39 | printf("Is %d avl: %d\n", root->left->n, avl); 40 | 41 | root->right->left = binary_tree_node(root->right, 97); 42 | binary_tree_print(root); 43 | avl = binary_tree_is_avl(root); 44 | printf("Is %d avl: %d\n", root->n, avl); 45 | 46 | root = basic_tree(); 47 | root->right->right->right = binary_tree_node(root->right->right, 430); 48 | binary_tree_print(root); 49 | avl = binary_tree_is_avl(root); 50 | printf("Is %d avl: %d\n", root->n, avl); 51 | 52 | root->right->right->right->left = binary_tree_node(root->right->right->right, 420); 53 | binary_tree_print(root); 54 | avl = binary_tree_is_avl(root); 55 | printf("Is %d avl: %d\n", root->n, avl); 56 | return (0); 57 | } 58 | -------------------------------------------------------------------------------- /120-binary_tree_is_avl.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_height - Function that measures the height of a binary tree 5 | * @tree: tree to go through 6 | * Return: the height 7 | */ 8 | 9 | size_t binary_tree_height(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(tree->left) : 1; 23 | r = tree->right ? 1 + binary_tree_height(tree->right) : 1; 24 | } 25 | return ((l > r) ? l : r); 26 | } 27 | } 28 | 29 | /** 30 | * bal_avl - Auxiliar function to compare each subtree if its AVL. 31 | * @tree: node that point to the tree to check. 32 | * @high: node that point to the higher node selected 33 | * @lower: node that point to the lower node selected. 34 | * Return: 1 if tree is AVL, 0 if not. 35 | */ 36 | int bal_avl(const binary_tree_t *tree, int lower, int high) 37 | { 38 | size_t height_l, height_r, balancer; 39 | 40 | if (tree != NULL) 41 | { 42 | if (tree->n > high || tree->n < lower) 43 | { 44 | return (0); 45 | } 46 | height_l = binary_tree_height(tree->left); 47 | height_r = binary_tree_height(tree->right); 48 | balancer = height_l > height_r ? height_l - height_r : height_r - height_l; 49 | if (balancer > 1) 50 | { 51 | return (0); 52 | } 53 | return (bal_avl(tree->left, lower, tree->n - 1) && 54 | bal_avl(tree->right, tree->n + 1, high)); 55 | } 56 | return (1); 57 | } 58 | 59 | /** 60 | * binary_tree_is_avl - checks if a binary tree is a valid AVL tree. 61 | * @tree: node that point to the tree to check. 62 | * Return: 1 if tree is AVL, 0 if not. 63 | */ 64 | int binary_tree_is_avl(const binary_tree_t *tree) 65 | { 66 | if (tree == NULL) 67 | { 68 | return (0); 69 | } 70 | return (bal_avl(tree, INT_MIN, INT_MAX)); 71 | } 72 | -------------------------------------------------------------------------------- /121-avl_insert.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * r_insert_node - node value instertion in a AVL. 5 | * @tree: type **pointer of root node of the AVL tree struct. 6 | * @parent: parent node of struct AVL. 7 | * @new: type**pointer left or right insertion. 8 | * @nval: insertion value of the AVL. 9 | * Return: pointer to the new root after insertion otherwise NULL 10 | */ 11 | avl_t *r_insert_node(avl_t **tree, avl_t *parent, avl_t **new, int nval) 12 | { 13 | int bval; 14 | 15 | if (*tree == NULL) 16 | return (*new = binary_tree_node(parent, nval)); 17 | if ((*tree)->n > nval) 18 | { 19 | (*tree)->left = r_insert_node(&(*tree)->left, *tree, new, nval); 20 | if ((*tree)->left == NULL) 21 | return (NULL); 22 | } 23 | else if ((*tree)->n < nval) 24 | { 25 | (*tree)->right = r_insert_node(&(*tree)->right, *tree, new, nval); 26 | if ((*tree)->right == NULL) 27 | return (NULL); 28 | } 29 | else 30 | { 31 | return (*tree); 32 | } 33 | bval = binary_tree_balance(*tree); 34 | if (bval > 1 && (*tree)->left->n > nval) 35 | { 36 | *tree = binary_tree_rotate_right(*tree); 37 | } 38 | else if (bval > 1 && (*tree)->left->n < nval) 39 | { 40 | (*tree)->left = binary_tree_rotate_left((*tree)->left); 41 | *tree = binary_tree_rotate_right(*tree); 42 | } 43 | else if (bval < -1 && (*tree)->right->n < nval) 44 | { 45 | *tree = binary_tree_rotate_left(*tree); 46 | } 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 | return (*tree); 53 | } 54 | /** 55 | * avl_insert - inserts a value into an AVL tree. 56 | * @tree: type **pointer to the root node of the AVL tree to insert into. 57 | * @value: value to store in the node to be inserted 58 | * Return: inserted node, or NULL if fails. 59 | */ 60 | avl_t *avl_insert(avl_t **tree, int value) 61 | { 62 | avl_t *new = NULL; 63 | 64 | if (*tree == NULL) 65 | { 66 | *tree = binary_tree_node(NULL, value); 67 | return (*tree); 68 | } 69 | r_insert_node(tree, *tree, &new, value); 70 | return (new); 71 | } 72 | -------------------------------------------------------------------------------- /102-binary_tree_is_complete.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * new_node - Function that creates a new_node in a linked_list 4 | * @node: Type pointer of node to be created 5 | * Return: the node created 6 | */ 7 | link_t *new_node(binary_tree_t *node) 8 | { 9 | link_t *new; 10 | 11 | new = malloc(sizeof(link_t)); 12 | if (new == NULL) 13 | { 14 | return (NULL); 15 | } 16 | new->node = node; 17 | new->next = NULL; 18 | 19 | return (new); 20 | } 21 | /** 22 | * free_q - Function that free the nodes at the linked list 23 | * @head: Node of the linked_list 24 | */ 25 | void free_q(link_t *head) 26 | { 27 | link_t *temp_node; 28 | 29 | while (head) 30 | { 31 | temp_node = head->next; 32 | free(head); 33 | head = temp_node; 34 | } 35 | } 36 | /** 37 | * _push - Function that pushes a node into the stack 38 | * @node: Type pointer of node of the tree 39 | * @head: Type head node of in the stack 40 | * @tail: Type tail node of in the stack 41 | */ 42 | void _push(binary_tree_t *node, link_t *head, link_t **tail) 43 | { 44 | link_t *new; 45 | 46 | new = new_node(node); 47 | if (new == NULL) 48 | { 49 | free_q(head); 50 | exit(1); 51 | } 52 | (*tail)->next = new; 53 | *tail = new; 54 | } 55 | /** 56 | * _pop - Function that pops a node into the stack 57 | * @head: Type head node of in the stack 58 | */ 59 | void _pop(link_t **head) 60 | { 61 | link_t *temp_node; 62 | 63 | temp_node = (*head)->next; 64 | free(*head); 65 | *head = temp_node; 66 | } 67 | /** 68 | * binary_tree_is_complete - Function that checks if a binary tree is complete 69 | * @tree: Type pointer of node of the tree 70 | * Return: 1 if is complete 0 if it is not 71 | */ 72 | int binary_tree_is_complete(const binary_tree_t *tree) 73 | { 74 | link_t *head, *tail; 75 | int flag = 0; 76 | 77 | if (tree == NULL) 78 | { 79 | return (0); 80 | } 81 | head = tail = new_node((binary_tree_t *)tree); 82 | if (head == NULL) 83 | { 84 | exit(1); 85 | } 86 | while (head != NULL) 87 | { 88 | if (head->node->left != NULL) 89 | { 90 | if (flag == 1) 91 | { 92 | free_q(head); 93 | return (0); 94 | } 95 | _push(head->node->left, head, &tail); 96 | } 97 | else 98 | flag = 1; 99 | if (head->node->right != NULL) 100 | { 101 | if (flag == 1) 102 | { 103 | free_q(head); 104 | return (0); 105 | } 106 | _push(head->node->right, head, &tail); 107 | } 108 | else 109 | flag = 1; 110 | _pop(&head); 111 | } 112 | return (1); 113 | } 114 | -------------------------------------------------------------------------------- /110-binary_tree_is_bst.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * check_sub_tree_Left - check if all nodes are smaller than 4 | * the root specified 5 | * @node: node in the tree to verify condition 6 | * @max: value to compare 7 | * Return: 1 if all nodes are smaller or equal or 0 if not 8 | */ 9 | int check_sub_tree_Left(const binary_tree_t *node, int max) 10 | { 11 | int left = 0, right = 0; 12 | 13 | if (node == NULL) 14 | { 15 | return (1); 16 | } 17 | else 18 | { 19 | if (node->n >= max) 20 | return (0); 21 | left = check_sub_tree_Left(node->left, max); 22 | right = check_sub_tree_Left(node->right, max); 23 | if (left == right && left == 1) 24 | return (1); 25 | return (0); 26 | } 27 | } 28 | /** 29 | * check_sub_tree_Right - check if all the nodes are bigger than the 30 | * root specified 31 | * @node: node in the tree to verify condition 32 | * @min: value to compare 33 | * Return: 1 if all is bigger or equal or 0 if not 34 | */ 35 | int check_sub_tree_Right(const binary_tree_t *node, int min) 36 | { 37 | int left = 0, right = 0; 38 | 39 | if (node == NULL) 40 | { 41 | return (1); 42 | } 43 | else 44 | { 45 | if (node->n <= min) 46 | return (0); 47 | left = check_sub_tree_Right(node->left, min); 48 | right = check_sub_tree_Right(node->right, min); 49 | if (left == right && left == 1) 50 | return (1); 51 | return (0); 52 | } 53 | } 54 | /** 55 | * binary_tree_is_bst - says if a tree is a bst or not 56 | * the process here is first verify that the left node be smaller than the root 57 | * then verify if the right node is bigger than th root. 58 | * after that verify if the left subtree has nodes smaller than root 59 | * and the right subtree has bigger nodes than root 60 | * @tree: node that point to the tree to check 61 | * Return: 1 if it is a BST or 0 if not 62 | */ 63 | int binary_tree_is_bst(const binary_tree_t *tree) 64 | { 65 | int var = 0, left = 2, right = 2; 66 | 67 | if (tree == NULL) 68 | return (0); 69 | if (tree->left && tree->left->n > tree->n) 70 | return (0); 71 | if (tree->right && tree->right->n < tree->n) 72 | return (0); 73 | if (tree->left && tree->left->n < tree->n) 74 | { 75 | var = check_sub_tree_Left(tree->left, tree->n); 76 | if (var == 0) 77 | return (0); 78 | left = binary_tree_is_bst(tree->left); 79 | } 80 | if (tree->right && tree->right->n > tree->n) 81 | { 82 | var = check_sub_tree_Right(tree->right, tree->n); 83 | if (var == 0) 84 | return (0); 85 | right = binary_tree_is_bst(tree->right); 86 | } 87 | if (left != 2 || right != 2) 88 | { 89 | if (left == 0 || right == 0) 90 | return (0); 91 | } 92 | return (1); 93 | } 94 | -------------------------------------------------------------------------------- /114-bst_remove.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * successor - get the next successor i mean the min node in the right subtree 4 | * @node: tree to check 5 | * Return: the min value of this tree 6 | */ 7 | int successor(bst_t *node) 8 | { 9 | int left = 0; 10 | 11 | if (node == NULL) 12 | { 13 | return (0); 14 | } 15 | else 16 | { 17 | left = successor(node->left); 18 | if (left == 0) 19 | { 20 | return (node->n); 21 | } 22 | return (left); 23 | } 24 | 25 | } 26 | /** 27 | * two_children - function that gets the next successor using the min 28 | * value in the right subtree, and then replace the node value for 29 | * this successor 30 | * @root: node tat has two children 31 | * Return: the value found 32 | */ 33 | int two_children(bst_t *root) 34 | { 35 | int new_value = 0; 36 | 37 | new_value = successor(root->right); 38 | root->n = new_value; 39 | return (new_value); 40 | } 41 | /** 42 | *remove_type - function that removes a node depending of its children 43 | *@root: node to remove 44 | *Return: 0 if it has no children or other value if it has 45 | */ 46 | int remove_type(bst_t *root) 47 | { 48 | if (!root->left && !root->right) 49 | { 50 | if (root->parent->right == root) 51 | root->parent->right = NULL; 52 | else 53 | root->parent->left = NULL; 54 | free(root); 55 | return (0); 56 | } 57 | else if ((!root->left && root->right) || (!root->right && root->left)) 58 | { 59 | if (!root->left) 60 | { 61 | if (root->parent->right == root) 62 | root->parent->right = root->right; 63 | else 64 | root->parent->left = root->right; 65 | root->right->parent = root->parent; 66 | } 67 | if (!root->right) 68 | { 69 | if (root->parent->right == root) 70 | root->parent->right = root->left; 71 | else 72 | root->parent->left = root->left; 73 | root->left->parent = root->parent; 74 | } 75 | free(root); 76 | return (0); 77 | } 78 | else 79 | return (two_children(root)); 80 | } 81 | /** 82 | * bst_remove - remove a node from a BST tree 83 | * @root: root of the tree 84 | * @value: node with this value to remove 85 | * Return: the tree changed 86 | */ 87 | bst_t *bst_remove(bst_t *root, int value) 88 | { 89 | int type = 0; 90 | 91 | if (root == NULL) 92 | return (NULL); 93 | if (value < root->n) 94 | bst_remove(root->left, value); 95 | else if (value > root->n) 96 | bst_remove(root->right, value); 97 | else if (value == root->n) 98 | { 99 | type = remove_type(root); 100 | if (type != 0) 101 | bst_remove(root->right, type); 102 | } 103 | else 104 | return (NULL); 105 | return (root); 106 | } 107 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /101-binary_tree_levelorder.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | /** 3 | * binary_tree_height - Function that measures the height of a binary tree 4 | * @tree: tree to go through 5 | * Return: the height 6 | */ 7 | size_t binary_tree_height(const binary_tree_t *tree) 8 | { 9 | size_t l = 0; 10 | size_t r = 0; 11 | 12 | if (tree == NULL) 13 | { 14 | return (0); 15 | } 16 | else 17 | { 18 | if (tree) 19 | { 20 | l = tree->left ? 1 + binary_tree_height(tree->left) : 0; 21 | r = tree->right ? 1 + binary_tree_height(tree->right) : 0; 22 | } 23 | return ((l > r) ? l : r); 24 | } 25 | } 26 | /** 27 | * binary_tree_depth - depth of specified node from root 28 | * @tree: node to check the depth 29 | * Return: 0 is it is the root or number of depth 30 | */ 31 | size_t binary_tree_depth(const binary_tree_t *tree) 32 | { 33 | return ((tree && tree->parent) ? 1 + binary_tree_depth(tree->parent) : 0); 34 | } 35 | /** 36 | * linked_node - this function makes a linked list from depth level and node 37 | * @head: pointer to head of linked list 38 | * @tree: node to store 39 | * @level: depth of node to store 40 | * Return: Nothing 41 | */ 42 | void linked_node(link_t **head, const binary_tree_t *tree, size_t level) 43 | { 44 | link_t *new, *aux; 45 | 46 | new = malloc(sizeof(link_t)); 47 | if (new == NULL) 48 | { 49 | return; 50 | } 51 | new->n = level; 52 | new->node = tree; 53 | if (*head == NULL) 54 | { 55 | new->next = NULL; 56 | *head = new; 57 | } 58 | else 59 | { 60 | aux = *head; 61 | while (aux->next != NULL) 62 | { 63 | aux = aux->next; 64 | } 65 | new->next = NULL; 66 | aux->next = new; 67 | } 68 | } 69 | /** 70 | * recursion - goes through the complete tree and each stores each node 71 | * in linked_node function 72 | * @head: pointer to head of linked list 73 | * @tree: node to check 74 | * Return: Nothing by default it affects the pointer 75 | */ 76 | void recursion(link_t **head, const binary_tree_t *tree) 77 | { 78 | size_t level = 0; 79 | 80 | if (tree != NULL) 81 | { 82 | level = binary_tree_depth(tree); 83 | linked_node(head, tree, level); 84 | recursion(head, tree->left); 85 | recursion(head, tree->right); 86 | } 87 | } 88 | /** 89 | * binary_tree_levelorder - print the nodes element in a lever-order 90 | * @tree: root node 91 | * @func: function to use 92 | * Return: Nothing 93 | */ 94 | void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int)) 95 | { 96 | link_t *head, *aux; 97 | size_t height = 0, count = 0; 98 | 99 | if (!tree || !func) 100 | { 101 | return; 102 | } 103 | else 104 | { 105 | height = binary_tree_height(tree); 106 | head = NULL; 107 | recursion(&head, tree); 108 | while (count <= height) 109 | { 110 | aux = head; 111 | while (aux != NULL) 112 | { 113 | if (count == aux->n) 114 | { 115 | func(aux->node->n); 116 | } 117 | aux = aux->next; 118 | } 119 | count++; 120 | } 121 | while (head != NULL) 122 | { 123 | aux = head; 124 | head = head->next; 125 | free(aux); 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /130-binary_tree_is_heap.c: -------------------------------------------------------------------------------- 1 | #include "binary_trees.h" 2 | 3 | /** 4 | * binary_tree_height - Function that measures the height of a binary tree 5 | * @tree: tree to go through 6 | * Return: the height 7 | */ 8 | 9 | size_t binary_tree_height(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->left == NULL && tree->right == NULL) 21 | return (tree->parent != NULL); 22 | if (tree) 23 | { 24 | l = tree->left ? 1 + binary_tree_height(tree->left) : 0; 25 | r = tree->right ? 1 + binary_tree_height(tree->right) : 0; 26 | } 27 | return ((l > r) ? l : r); 28 | } 29 | } 30 | 31 | /** 32 | * binary_tree_balance - Measures balance factor of a binary tree 33 | * @tree: tree to go through 34 | * Return: balanced factor 35 | */ 36 | int binary_tree_balance(const binary_tree_t *tree) 37 | { 38 | int right = 0, left = 0, total = 0; 39 | 40 | if (tree) 41 | { 42 | left = ((int)binary_tree_height(tree->left)); 43 | right = ((int)binary_tree_height(tree->right)); 44 | total = left - right; 45 | } 46 | return (total); 47 | } 48 | 49 | /** 50 | * tree_is_perfect - function that says if a tree is perfect or not 51 | * it has to be the same quantity of levels in left as right, and also 52 | * each node has to have 2 nodes or none 53 | * @tree: tree to check 54 | * Return: 0 if is not a perfect or other number that is the level of height 55 | */ 56 | int tree_is_perfect(const binary_tree_t *tree) 57 | { 58 | int l = 0, r = 0; 59 | 60 | if (tree->left && tree->right) 61 | { 62 | l = 1 + tree_is_perfect(tree->left); 63 | r = 1 + tree_is_perfect(tree->right); 64 | if (r == l && r != 0 && l != 0) 65 | return (r); 66 | return (0); 67 | } 68 | else if (!tree->left && !tree->right) 69 | { 70 | return (1); 71 | } 72 | else 73 | { 74 | return (0); 75 | } 76 | } 77 | 78 | /** 79 | * binary_tree_is_perfect - perfect or not a tree 80 | * @tree: tree to check 81 | * Return: 1 is it is or 0 if not 82 | */ 83 | int binary_tree_is_perfect(const binary_tree_t *tree) 84 | { 85 | int result = 0; 86 | 87 | if (tree == NULL) 88 | { 89 | return (0); 90 | } 91 | else 92 | { 93 | result = tree_is_perfect(tree); 94 | if (result != 0) 95 | { 96 | return (1); 97 | } 98 | return (0); 99 | } 100 | } 101 | 102 | /** 103 | * binary_tree_is_heap - checks if a binary tree is a valid Max Binary Heap 104 | * @tree: tree to check 105 | * Return: 1 is it is or 0 if not 106 | */ 107 | int binary_tree_is_heap(const binary_tree_t *tree) 108 | { 109 | int bval; 110 | 111 | if (tree == NULL) 112 | { 113 | return (0); 114 | } 115 | if (tree->left && tree->left->n > tree->n) 116 | { 117 | return (0); 118 | } 119 | if (tree->right && tree->right->n > tree->n) 120 | { 121 | return (0); 122 | } 123 | if (binary_tree_is_perfect(tree)) 124 | { 125 | return (1); 126 | } 127 | bval = binary_tree_balance(tree); 128 | if (bval == 0) 129 | { 130 | return (binary_tree_is_perfect(tree->left) 131 | && binary_tree_is_heap(tree->right)); 132 | } 133 | if (bval == 1) 134 | { 135 | return (binary_tree_is_heap(tree->left) 136 | && binary_tree_is_perfect(tree->right)); 137 | } 138 | else 139 | { 140 | return (0); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /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 | 10 | /** 11 | * struct binary_tree_s - Binary tree node 12 | * 13 | * @n: Integer stored in the node 14 | * @parent: Pointer to the parent node 15 | * @left: Pointer to the left child node 16 | * @right: Pointer to the right child node 17 | */ 18 | struct binary_tree_s 19 | { 20 | int n; 21 | struct binary_tree_s *parent; 22 | struct binary_tree_s *left; 23 | struct binary_tree_s *right; 24 | }; 25 | typedef struct binary_tree_s binary_tree_t; 26 | typedef struct binary_tree_s bst_t; 27 | typedef struct binary_tree_s avl_t; 28 | typedef struct binary_tree_s heap_t; 29 | /* linked list for advanced 101 the levelorder */ 30 | /** 31 | * struct link_s - structure for advanced tasks 32 | * 33 | * @n: depth of node specified 34 | * @node: node of tree to store 35 | * @next: next node of the linked list 36 | */ 37 | typedef struct link_s 38 | { 39 | size_t n; 40 | struct binary_tree_s const *node; 41 | struct link_s *next; 42 | } link_t; 43 | /* ----------------- mandatory task ---------------------------------*/ 44 | void binary_tree_print(const binary_tree_t *); 45 | binary_tree_t *binary_tree_node(binary_tree_t *parent, int value); 46 | binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value); 47 | binary_tree_t *binary_tree_insert_right(binary_tree_t *parent, int value); 48 | void binary_tree_delete(binary_tree_t *tree); 49 | int binary_tree_is_leaf(const binary_tree_t *node); 50 | int binary_tree_is_root(const binary_tree_t *node); 51 | void binary_tree_preorder(const binary_tree_t *tree, void (*func)(int)); 52 | void binary_tree_inorder(const binary_tree_t *tree, void (*func)(int)); 53 | void binary_tree_postorder(const binary_tree_t *tree, void (*func)(int)); 54 | size_t binary_tree_height(const binary_tree_t *tree); 55 | size_t binary_tree_depth(const binary_tree_t *tree); 56 | size_t binary_tree_size(const binary_tree_t *tree); 57 | size_t binary_tree_leaves(const binary_tree_t *tree); 58 | size_t binary_tree_nodes(const binary_tree_t *tree); 59 | int binary_tree_balance(const binary_tree_t *tree); 60 | int binary_tree_is_full(const binary_tree_t *tree); 61 | int binary_tree_is_perfect(const binary_tree_t *tree); 62 | binary_tree_t *binary_tree_sibling(binary_tree_t *node); 63 | binary_tree_t *binary_tree_uncle(binary_tree_t *node); 64 | /* ------------------ advanced task -----------------------------------*/ 65 | binary_tree_t *binary_trees_ancestor(const binary_tree_t *first, 66 | const binary_tree_t *second); 67 | void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int)); 68 | int binary_tree_is_complete(const binary_tree_t *tree); 69 | binary_tree_t *binary_tree_rotate_left(binary_tree_t *tree); 70 | binary_tree_t *binary_tree_rotate_right(binary_tree_t *tree); 71 | int binary_tree_is_bst(const binary_tree_t *tree); 72 | bst_t *bst_insert(bst_t **tree, int value); 73 | bst_t *array_to_bst(int *array, size_t size); 74 | bst_t *bst_search(const bst_t *tree, int value); 75 | bst_t *bst_remove(bst_t *root, int value); 76 | int binary_tree_is_avl(const binary_tree_t *tree); 77 | avl_t *avl_insert(avl_t **tree, int value); 78 | avl_t *array_to_avl(int *array, size_t size); 79 | avl_t *avl_remove(avl_t *root, int value); 80 | avl_t *sorted_array_to_avl(int *array, size_t size); 81 | int binary_tree_is_heap(const binary_tree_t *tree); 82 | heap_t *heap_insert(heap_t **root, int value); 83 | heap_t *array_to_heap(int *array, size_t size); 84 | int heap_extract(heap_t **root); 85 | int *heap_to_sorted_array(heap_t *heap, size_t *size); 86 | 87 | #endif /* _BINARY_TREES_H_ */ 88 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 0x1D C - Binary trees :pencil2: 2 | 3 | > C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. This projects covers the concepts of binary trees! This was a project done with the contribution of my peer in many other SWE projects [Jorge Zafra](https://github.com/jorgezafra94/) Kudos! 4 | 5 | At the end of this project, We were able to undesrtand these questions: 6 | 7 | * What is a binary tree 8 | * What is the difference between a binary tree and a Binary Search Tree 9 | * What is the possible gain in terms of time complexity compared to linked lists 10 | * What are the depth, the height, the size of a binary tree 11 | * What are the different traversal methods to go through a binary tree 12 | * What is a complete, a full, a perfect, a balanced binary tree 13 | 14 | ## Tasks :heavy_check_mark: 15 | 16 | 0. Function that creates a binary tree node 17 | 1. Function that inserts a node as the left-child of another node 18 | 2. Function that inserts a node as the right-child of another node 19 | 3. Function that deletes an entire binary tree 20 | 4. Function that checks if a node is a leaf 21 | 5. Function that checks if a given node is a root 22 | 6. Function that goes through a binary tree using pre-order traversal 23 | 7. Function that goes through a binary tree using in-order traversal 24 | 8. Function that goes through a binary tree using post-order traversal 25 | 9. Function that measures the height of a binary tree 26 | 10. Function that measures the depth of a node in a binary tree 27 | 11. Function that measures the size of a binary tree 28 | 12. Function that counts the leaves in a binary tree 29 | 13. Function that counts the nodes with at least 1 child in a binary tree 30 | 14. Function that measures the balance factor of a binary tree 31 | 15. Function that checks if a binary tree is full 32 | 16. Function that checks if a binary tree is perfect 33 | 17. Function that finds the sibling of a node 34 | 18. Function that finds the uncle of a node 35 | 19. Function that finds the lowest common ancestor of two nodes 36 | 20. Function that goes through a binary tree using level-order traversal 37 | 21. Function that checks if a binary tree is complete 38 | 22. Function that performs a left-rotation on a binary tree 39 | 23. Function that performs a right-rotation on a binary tree 40 | 24. Function that checks if a binary tree is a valid Binary Search Tree 41 | 25. Function that inserts a value in a Binary Search Tree 42 | 26. Function that builds a Binary Search Tree from an array 43 | 27. Function that searches for a value in a Binary Search Tree 44 | 28. Function that removes a node from a Binary Search Tree 45 | 29. What are the average time complexities of those operations on a Binary Search Tree 46 | 30. Function that checks if a binary tree is a valid AVL Tree 47 | 31. Function that inserts a value in an AVL Tree 48 | 32. Function that builds an AVL tree from an array 49 | 33. Function that removes a node from an AVL tree 50 | 34. Function that builds an AVL tree from an array 51 | 35. What are the average time complexities of those operations on an AVL Tree 52 | 36. Function that checks if a binary tree is a valid Max Binary Heap (Task in progress) 53 | 37. Function that inserts a value in Max Binary Heap (Task in progress) 54 | 38. Function that builds a Max Binary Heap tree from an array (Task in progress) 55 | 39. Function that extracts the root node of a Max Binary Heap (Task in progress) 56 | 40. Function that converts a Binary Max Heap to a sorted array of integers (Task in progress) 57 | 41. What are the average time complexities of those operations on a Binary Heap 58 | 59 | 60 | ## Results :chart_with_upwards_trend: 61 | 62 | | Filename | 63 | | ------ | 64 | | [0-binary_tree_node.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/0-binary_tree_node.c)| 65 | | [1-binary_tree_insert_left.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/1-binary_tree_insert_left.c)| 66 | | [2-binary_tree_insert_right.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/2-binary_tree_insert_right.c)| 67 | | [3-binary_tree_delete.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/3-binary_tree_delete.c)| 68 | | [4-binary_tree_is_leaf.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/4-binary_tree_is_leaf.c)| 69 | | [5-binary_tree_is_root.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/5-binary_tree_is_root.c)| 70 | | [6-binary_tree_preorder.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/6-binary_tree_preorder.c)| 71 | | [7-binary_tree_inorder.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/7-binary_tree_inorder.c)| 72 | | [8-binary_tree_postorder.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/8-binary_tree_postorder.c)| 73 | | [9-binary_tree_height.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/9-binary_tree_height.c)| 74 | | [10-binary_tree_depth.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/10-binary_tree_depth.c)| 75 | | [11-binary_tree_size.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/11-binary_tree_size.c)| 76 | | [12-binary_tree_leaves.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/12-binary_tree_leaves.c)| 77 | | [13-binary_tree_nodes.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/13-binary_tree_nodes.c)| 78 | | [14-binary_tree_balance.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/14-binary_tree_balance.c)| 79 | | [15-binary_tree_is_full.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/15-binary_tree_is_full.c)| 80 | | [16-binary_tree_is_perfect.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/16-binary_tree_is_perfect.c)| 81 | | [17-binary_tree_sibling.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/17-binary_tree_sibling.c)| 82 | | [18-binary_tree_uncle.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/18-binary_tree_uncle.c)| 83 | | [100-binary_trees_ancestor.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/100-binary_trees_ancestor.c)| 84 | | [101-binary_tree_levelorder.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/101-binary_tree_levelorder.c)| 85 | | [102-binary_tree_is_complete.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/102-binary_tree_is_complete.c)| 86 | | [103-binary_tree_rotate_left.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/103-binary_tree_rotate_left.c)| 87 | | [104-binary_tree_rotate_right.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/104-binary_tree_rotate_right.c)| 88 | | [110-binary_tree_is_bst.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/110-binary_tree_is_bst.c)| 89 | | [111-bst_insert.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/111-bst_insert.c)| 90 | | [112-array_to_bst.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/112-array_to_bst.c)| 91 | | [113-bst_search.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/113-bst_search.c)| 92 | | [114-bst_remove.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/114-bst_remove.c)| 93 | | [115-O](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/115-O)| 94 | | [120-binary_tree_is_avl.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/120-binary_tree_is_avl.c)| 95 | | [121-avl_insert.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/121-avl_insert.c)| 96 | | [122-array_to_avl.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/122-array_to_avl.c)| 97 | | [123-avl_remove.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/123-avl_remove.c)| 98 | | [124-sorted_array_to_avl.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/124-sorted_array_to_avl.c)| 99 | | [125-O](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/125-O)| 100 | | [130-binary_tree_is_heap.c](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/130-binary_tree_is_heap.c)| 101 | | 131-heap_insert.c - Task pending| 102 | | 132-array_to_heap.c - Task pending| 103 | | 133-heap_extract.c - Task pending| 104 | | 134-heap_to_sorted_array.c - Task pending| 105 | | [135-O](https://github.com/edward0rtiz/0x1D-binary_trees/blob/master/135-O)| 106 | 107 | 108 | ## Additional info :construction: 109 | ### Resources 110 | 111 | - GLIBC 2.24 112 | - gcc 4.8.4 113 | - betty linter 0.32 114 | - Valgrind 115 | 116 | 117 | ### Try It On Your Machine :computer: 118 | 119 | SORRY, DESPITE THE PROJECT IS FUNCTIONAL FOR EDUCATIONAL PURPOSES FOR YOU TO LEARN I AM NOT LEAVING IT AVAILABLE FOR TEST 120 | 121 | 122 | --------------------------------------------------------------------------------