diff --git a/0-main.c b/0-main.c new file mode 100644 index 0000000..2a93561 --- /dev/null +++ b/0-main.c @@ -0,0 +1,25 @@ +#include +#include "binary_trees.h" + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + binary_tree_t *root; + + root = binary_tree_node(NULL, 98); + + root->left = binary_tree_node(root, 12); + root->left->left = binary_tree_node(root->left, 6); + root->left->right = binary_tree_node(root->left, 16); + + root->right = binary_tree_node(root, 402); + root->right->left = binary_tree_node(root->right, 256); + root->right->right = binary_tree_node(root->right, 512); + + binary_tree_print(root); + return (0); +} diff --git a/1-binary_tree_insert_left.c b/1-binary_tree_insert_left.c new file mode 100644 index 0000000..b40fea4 --- /dev/null +++ b/1-binary_tree_insert_left.c @@ -0,0 +1,31 @@ +#include "binary_trees.h" + +/** + * binary_tree_insert_left - inserts a node as the left-child of another node + * @parent: A pointer to the node to insert the left-child in + * @value: Value to store in the new node + * Return: A pointer to the created node + */ + +binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value) +{ + binary_tree_t *node; + + if (parent == NULL) + return (NULL); + + node = binary_tree_node(parent, value); + + if (node == NULL) + return (NULL); + + if (parent->left) + { + node->left = parent->left; + parent->left->parent = node; + } + + parent->left = node; + + return (node); +} diff --git a/1-left b/1-left new file mode 100755 index 0000000..575c3c0 Binary files /dev/null and b/1-left differ diff --git a/1-main.c b/1-main.c new file mode 100644 index 0000000..5392a7a --- /dev/null +++ b/1-main.c @@ -0,0 +1,23 @@ +#include +#include +#include "binary_trees.h" + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + binary_tree_t *root; + + root = binary_tree_node(NULL, 98); + root->left = binary_tree_node(root, 12); + root->right = binary_tree_node(root, 402); + binary_tree_print(root); + printf("\n"); + binary_tree_insert_left(root->right, 128); + binary_tree_insert_left(root, 54); + binary_tree_print(root); + return (0); +} diff --git a/10-binary_tree_depth.c b/10-binary_tree_depth.c new file mode 100644 index 0000000..03bf782 --- /dev/null +++ b/10-binary_tree_depth.c @@ -0,0 +1,27 @@ +#include "binary_trees.h" + +/** + * binary_tree_depth - measures the depth of a node in a binary tree + * @tree: A pointer to the node to measure the depth + * Return: size_t value indicating the depth of the node + */ + + +size_t binary_tree_depth(const binary_tree_t *tree) +{ + size_t depth; + const binary_tree_t *current; + + if (tree == NULL) + return (0); + + current = tree; + depth = 0; + + while (current->parent != NULL) + { + depth++; + current = current->parent; + } + return (depth); +} diff --git a/10-depth b/10-depth new file mode 100755 index 0000000..75c1600 Binary files /dev/null and b/10-depth differ diff --git a/10-main.c b/10-main.c new file mode 100644 index 0000000..8c9625a --- /dev/null +++ b/10-main.c @@ -0,0 +1,29 @@ +#include +#include +#include "binary_trees.h" + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + binary_tree_t *root; + size_t depth; + + root = binary_tree_node(NULL, 98); + root->left = binary_tree_node(root, 12); + root->right = binary_tree_node(root, 402); + binary_tree_insert_right(root->left, 54); + binary_tree_insert_right(root, 128); + binary_tree_print(root); + + depth = binary_tree_depth(root); + printf("Depth of %d: %lu\n", root->n, depth); + depth = binary_tree_depth(root->right); + printf("Depth of %d: %lu\n", root->right->n, depth); + depth = binary_tree_depth(root->left->right); + printf("Depth of %d: %lu\n", root->left->right->n, depth); + return (0); +} diff --git a/2-binary_tree_insert_right.c b/2-binary_tree_insert_right.c new file mode 100644 index 0000000..354b11e --- /dev/null +++ b/2-binary_tree_insert_right.c @@ -0,0 +1,31 @@ +#include "binary_trees.h" + +/** + * binary_tree_insert_right - inserts a node as the right-child of another node + * @parent: A pointer to the node to insert the left-child in + * @value: Value to store in the new node + * Return: A pointer to the created node + */ + +binary_tree_t *binary_tree_insert_right(binary_tree_t *parent, int value) +{ + binary_tree_t *node; + + if (parent == NULL) + return (NULL); + + node = binary_tree_node(parent, value); + + if (node == NULL) + return (NULL); + + if (parent->right) + { + node->right = parent->right; + parent->right->parent = node; + } + + parent->right = node; + + return (node); +} diff --git a/2-main.c b/2-main.c new file mode 100644 index 0000000..3bd3229 --- /dev/null +++ b/2-main.c @@ -0,0 +1,23 @@ +#include +#include +#include "binary_trees.h" + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + binary_tree_t *root; + + root = binary_tree_node(NULL, 98); + root->left = binary_tree_node(root, 12); + root->right = binary_tree_node(root, 402); + binary_tree_print(root); + printf("\n"); + binary_tree_insert_right(root->left, 54); + binary_tree_insert_right(root, 128); + binary_tree_print(root); + return (0); +} diff --git a/2-right b/2-right new file mode 100755 index 0000000..31984a8 Binary files /dev/null and b/2-right differ diff --git a/3-binary_tree_delete.c b/3-binary_tree_delete.c new file mode 100644 index 0000000..8868c6a --- /dev/null +++ b/3-binary_tree_delete.c @@ -0,0 +1,18 @@ +#include "binary_trees.h" + + +/** + * binary_tree_delete - deletes an entire binary tree + * @tree: Pointer to the root node of the tree to delete + */ + +void binary_tree_delete(binary_tree_t *tree) +{ + if (tree == NULL) + return; + + binary_tree_delete(tree->left); + binary_tree_delete(tree->right); + + free(tree); +} diff --git a/3-binary_tree_delete.c~ b/3-binary_tree_delete.c~ new file mode 100644 index 0000000..f55285d --- /dev/null +++ b/3-binary_tree_delete.c~ @@ -0,0 +1,18 @@ +#include "binary_trees.h" + + +/** + * binary_trees_delete - deletes an entire binary tree + * @tree: Pointer to the root node of the tree to delete + */ + +void binary_tree_delete(binary_tree_t *tree) +{ + if (tree == NULL) + return; + + binary_tree_delete(tree->left); + binary_tree_delete(tree->right); + + free(tree); +} diff --git a/3-del b/3-del new file mode 100755 index 0000000..d5d1406 Binary files /dev/null and b/3-del differ diff --git a/3-main.c b/3-main.c new file mode 100644 index 0000000..bbcfde1 --- /dev/null +++ b/3-main.c @@ -0,0 +1,22 @@ +#include +#include +#include "binary_trees.h" + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + binary_tree_t *root; + + root = binary_tree_node(NULL, 98); + root->left = binary_tree_node(root, 12); + root->right = binary_tree_node(root, 402); + binary_tree_insert_right(root->left, 54); + binary_tree_insert_right(root, 128); + binary_tree_print(root); + binary_tree_delete(root); + return (0); +} diff --git a/4-binary_tree_is_leaf.c b/4-binary_tree_is_leaf.c new file mode 100644 index 0000000..debaec0 --- /dev/null +++ b/4-binary_tree_is_leaf.c @@ -0,0 +1,20 @@ +#include "binary_trees.h" + + +/** + * binary_tree_is_leaf - checks if a node is a leaf + * @node: A pointer to the node to check + * Return: 1 if node is a leaf otherwise 0 + */ + + +int binary_tree_is_leaf(const binary_tree_t *node) +{ + if (node == NULL) + return (0); + + if (node->left == NULL && node->right == NULL) + return (1); + + return (0); +} diff --git a/4-leaf b/4-leaf new file mode 100755 index 0000000..272be73 Binary files /dev/null and b/4-leaf differ diff --git a/4-main.c b/4-main.c new file mode 100644 index 0000000..627f793 --- /dev/null +++ b/4-main.c @@ -0,0 +1,29 @@ +#include +#include +#include "binary_trees.h" + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + binary_tree_t *root; + int ret; + + root = binary_tree_node(NULL, 98); + root->left = binary_tree_node(root, 12); + root->right = binary_tree_node(root, 402); + binary_tree_insert_right(root->left, 54); + binary_tree_insert_right(root, 128); + binary_tree_print(root); + + ret = binary_tree_is_leaf(root); + printf("Is %d a leaf: %d\n", root->n, ret); + ret = binary_tree_is_leaf(root->right); + printf("Is %d a leaf: %d\n", root->right->n, ret); + ret = binary_tree_is_leaf(root->right->right); + printf("Is %d a leaf: %d\n", root->right->right->n, ret); + return (0); +} diff --git a/5-binary_tree_is_root.c b/5-binary_tree_is_root.c new file mode 100644 index 0000000..760d845 --- /dev/null +++ b/5-binary_tree_is_root.c @@ -0,0 +1,20 @@ +#include "binary_trees.h" + + +/** + * binary_tree_is_root - check if a given node is a root + * @node: Pointer to the node to check + * Return: 1 if node is root, otherwise 0 + */ + + +int binary_tree_is_root(const binary_tree_t *node) +{ + if (node == NULL) + return (0); + + if (node->parent == NULL) + return (1); + + return (0); +} diff --git a/5-binary_tree_is_root.c~ b/5-binary_tree_is_root.c~ new file mode 100644 index 0000000..ada097c --- /dev/null +++ b/5-binary_tree_is_root.c~ @@ -0,0 +1,20 @@ +#include "binary_trees.h" + + +/** + * bnary_tree_is_root - check if a given node is a root + * @node: Pointer to the node to check + * Return: 1 if node is root, otherwise 0 + */ + + +int binary_tree_is_root(const binary_tree_t *node) +{ + if (node == NULL) + return (0); + + if (node->parent == NULL) + return (1); + + return (0); +} diff --git a/5-main.c b/5-main.c new file mode 100644 index 0000000..611320b --- /dev/null +++ b/5-main.c @@ -0,0 +1,29 @@ +#include +#include +#include "binary_trees.h" + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + binary_tree_t *root; + int ret; + + root = binary_tree_node(NULL, 98); + root->left = binary_tree_node(root, 12); + root->right = binary_tree_node(root, 402); + binary_tree_insert_right(root->left, 54); + binary_tree_insert_right(root, 128); + binary_tree_print(root); + + ret = binary_tree_is_root(root); + printf("Is %d a root: %d\n", root->n, ret); + ret = binary_tree_is_root(root->right); + printf("Is %d a root: %d\n", root->right->n, ret); + ret = binary_tree_is_root(root->right->right); + printf("Is %d a root: %d\n", root->right->right->n, ret); + return (0); +} diff --git a/5-root b/5-root new file mode 100755 index 0000000..f44b280 Binary files /dev/null and b/5-root differ diff --git a/6-binary_tree_preorder.c b/6-binary_tree_preorder.c new file mode 100644 index 0000000..ed167fe --- /dev/null +++ b/6-binary_tree_preorder.c @@ -0,0 +1,20 @@ +#include "binary_trees.h" + +/** + * binary_tree_preorder - Goes through a binary tree using pre-order traversal + * @tree: A pointer to the root node of the tree to traverse + * @func: Pointer to a function to call for each node + */ + +void binary_tree_preorder(const binary_tree_t *tree, void (*func)(int)) +{ + if (tree == NULL || func == NULL) + return; + + + func(tree->n); + + binary_tree_preorder(tree->left, func); + + binary_tree_preorder(tree->right, func); +} diff --git a/6-binary_tree_preorder.c~ b/6-binary_tree_preorder.c~ new file mode 100644 index 0000000..8423630 --- /dev/null +++ b/6-binary_tree_preorder.c~ @@ -0,0 +1,20 @@ +#include "binary_trees.h" + +/** + * binary_tree_preorder - Goes through a binary tree using pre-order traversal + * @tree: A pointer to the root node of the tree to traverse + * @func: Pointer to a function to call for each node + */ + +void binary_tree_preorder(const binary_tree_t *tree, void (*func)(int)) +{ + if (tree == NULL || func == NULL) + return; + + + func(tree->n); + + binary_tree_preorder(tree->left, func); + + binary_tree_preorder(tree->right, func); +} diff --git a/6-main.c b/6-main.c new file mode 100644 index 0000000..b6ab1df --- /dev/null +++ b/6-main.c @@ -0,0 +1,35 @@ +#include +#include +#include "binary_trees.h" + +/** + * print_num - Prints a number + * + * @n: Number to be printed + */ +void print_num(int n) +{ + printf("%d\n", n); +} + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + binary_tree_t *root; + + root = binary_tree_node(NULL, 98); + root->left = binary_tree_node(root, 12); + root->right = binary_tree_node(root, 402); + root->left->left = binary_tree_node(root->left, 6); + root->left->right = binary_tree_node(root->left, 56); + root->right->left = binary_tree_node(root->right, 256); + root->right->right = binary_tree_node(root->right, 512); + + binary_tree_print(root); + binary_tree_preorder(root, &print_num); + return (0); +} diff --git a/6-pre b/6-pre new file mode 100755 index 0000000..8beff3a Binary files /dev/null and b/6-pre differ diff --git a/7-binary_tree_inorder.c b/7-binary_tree_inorder.c new file mode 100644 index 0000000..e88df5f --- /dev/null +++ b/7-binary_tree_inorder.c @@ -0,0 +1,19 @@ +#include "binary_trees.h" + +/** + * binary_tree_inorder - Goes through a binary tree using in-order traversal + * @tree: A pointer to the root node of the tree to traverse + * @func: Pointer to a function to call for each node + */ + +void binary_tree_inorder(const binary_tree_t *tree, void (*func)(int)) +{ + if (tree == NULL || func == NULL) + return; + + binary_tree_inorder(tree->left, func); + + func(tree->n); + + binary_tree_inorder(tree->right, func); +} diff --git a/7-in b/7-in new file mode 100755 index 0000000..98aa860 Binary files /dev/null and b/7-in differ diff --git a/7-main.c b/7-main.c new file mode 100644 index 0000000..4ffb693 --- /dev/null +++ b/7-main.c @@ -0,0 +1,35 @@ +#include +#include +#include "binary_trees.h" + +/** + * print_num - Prints a number + * + * @n: Number to be printed + */ +void print_num(int n) +{ + printf("%d\n", n); +} + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + binary_tree_t *root; + + root = binary_tree_node(NULL, 98); + root->left = binary_tree_node(root, 12); + root->right = binary_tree_node(root, 402); + root->left->left = binary_tree_node(root->left, 6); + root->left->right = binary_tree_node(root->left, 56); + root->right->left = binary_tree_node(root->right, 256); + root->right->right = binary_tree_node(root->right, 512); + + binary_tree_print(root); + binary_tree_inorder(root, &print_num); + return (0); +} diff --git a/8-binary_tree_postorder.c b/8-binary_tree_postorder.c new file mode 100644 index 0000000..db44b3d --- /dev/null +++ b/8-binary_tree_postorder.c @@ -0,0 +1,19 @@ +#include "binary_trees.h" + +/** + * binary_tree_postorder - Goes through a binary tree using in-order traversal + * @tree: A pointer to the root node of the tree to traverse + * @func: Pointer to a function to call for each node + */ + +void binary_tree_postorder(const binary_tree_t *tree, void (*func)(int)) +{ + if (tree == NULL || func == NULL) + return; + + binary_tree_postorder(tree->left, func); + + binary_tree_postorder(tree->right, func); + + func(tree->n); +} diff --git a/8-main.c b/8-main.c new file mode 100644 index 0000000..8d68e1b --- /dev/null +++ b/8-main.c @@ -0,0 +1,35 @@ +#include +#include +#include "binary_trees.h" + +/** + * print_num - Prints a number + * + * @n: Number to be printed + */ +void print_num(int n) +{ + printf("%d\n", n); +} + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + binary_tree_t *root; + + root = binary_tree_node(NULL, 98); + root->left = binary_tree_node(root, 12); + root->right = binary_tree_node(root, 402); + root->left->left = binary_tree_node(root->left, 6); + root->left->right = binary_tree_node(root->left, 56); + root->right->left = binary_tree_node(root->right, 256); + root->right->right = binary_tree_node(root->right, 512); + + binary_tree_print(root); + binary_tree_postorder(root, &print_num); + return (0); +} diff --git a/8-post b/8-post new file mode 100755 index 0000000..9cd58bd Binary files /dev/null and b/8-post differ diff --git a/9-binary_tree_height.c b/9-binary_tree_height.c new file mode 100644 index 0000000..251f869 --- /dev/null +++ b/9-binary_tree_height.c @@ -0,0 +1,34 @@ +#include "binary_trees.h" + + +/** + * binary_tree_height - measures the height of a binary tree + * @tree: A pointer to the root node of the tree to measure the height + * Return: Size_t value as the height of the tree + */ + + +size_t binary_tree_height(const binary_tree_t *tree) +{ + size_t left, right, height = 0; + + if (tree == NULL) + return (0); + + if (tree->left) + left = 1 + binary_tree_height(tree->left); + else + left = 0; + + if (tree->right) + right = 1 + binary_tree_height(tree->right); + else + right = 0; + + if (left > right) + height += left; + else + height += right; + + return (height); +} diff --git a/9-height b/9-height new file mode 100755 index 0000000..d6f0425 Binary files /dev/null and b/9-height differ diff --git a/9-main.c b/9-main.c new file mode 100644 index 0000000..9614258 --- /dev/null +++ b/9-main.c @@ -0,0 +1,29 @@ +#include +#include +#include "binary_trees.h" + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + binary_tree_t *root; + size_t height; + + root = binary_tree_node(NULL, 98); + root->left = binary_tree_node(root, 12); + root->right = binary_tree_node(root, 402); + binary_tree_insert_right(root->left, 54); + binary_tree_insert_right(root, 128); + binary_tree_print(root); + + height = binary_tree_height(root); + printf("Height from %d: %lu\n", root->n, height); + height = binary_tree_height(root->right); + printf("Height from %d: %lu\n", root->right->n, height); + height = binary_tree_height(root->left->right); + printf("Height from %d: %lu\n", root->left->right->n, height); + return (0); +} diff --git a/binary_tree_print.c b/binary_tree_print.c new file mode 100644 index 0000000..010fccb --- /dev/null +++ b/binary_tree_print.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include "binary_trees.h" + +/* Original code from http://stackoverflow.com/a/13755911/5184480 */ + +/** + * print_t - Stores recursively each level in an array of strings + * + * @tree: Pointer to the node to print + * @offset: Offset to print + * @depth: Depth of the node + * @s: Buffer + * + * Return: length of printed tree after process + */ +static int print_t(const binary_tree_t *tree, int offset, int depth, char **s) +{ + char b[6]; + int width, left, right, is_left, i; + + if (!tree) + return (0); + is_left = (tree->parent && tree->parent->left == tree); + width = sprintf(b, "(%03d)", tree->n); + left = print_t(tree->left, offset, depth + 1, s); + right = print_t(tree->right, offset + left + width, depth + 1, s); + for (i = 0; i < width; i++) + s[depth][offset + left + i] = b[i]; + if (depth && is_left) + { + for (i = 0; i < width + right; i++) + s[depth - 1][offset + left + width / 2 + i] = '-'; + s[depth - 1][offset + left + width / 2] = '.'; + } + else if (depth && !is_left) + { + for (i = 0; i < left + width; i++) + s[depth - 1][offset - width / 2 + i] = '-'; + s[depth - 1][offset + left + width / 2] = '.'; + } + return (left + width + right); +} + +/** + * _height - Measures the height of a binary tree + * + * @tree: Pointer to the node to measures the height + * + * Return: The height of the tree starting at @node + */ +static size_t _height(const binary_tree_t *tree) +{ + size_t height_l; + size_t height_r; + + height_l = tree->left ? 1 + _height(tree->left) : 0; + height_r = tree->right ? 1 + _height(tree->right) : 0; + return (height_l > height_r ? height_l : height_r); +} + +/** + * binary_tree_print - Prints a binary tree + * + * @tree: Pointer to the root node of the tree to print + */ +void binary_tree_print(const binary_tree_t *tree) +{ + char **s; + size_t height, i, j; + + if (!tree) + return; + height = _height(tree); + s = malloc(sizeof(*s) * (height + 1)); + if (!s) + return; + for (i = 0; i < height + 1; i++) + { + s[i] = malloc(sizeof(**s) * 255); + if (!s[i]) + return; + memset(s[i], 32, 255); + } + print_t(tree, 0, 0, s); + for (i = 0; i < height + 1; i++) + { + for (j = 254; j > 1; --j) + { + if (s[i][j] != ' ') + break; + s[i][j] = '\0'; + } + printf("%s\n", s[i]); + free(s[i]); + } + free(s); +}