{"id":19079437,"url":"https://github.com/hackersa3edy/binary_trees","last_synced_at":"2026-05-21T09:30:17.684Z","repository":{"id":220071756,"uuid":"750657177","full_name":"hackerSa3edy/binary_trees","owner":"hackerSa3edy","description":"This repository contains C programs for various operations on binary trees, including node manipulation, tree traversal, and property checks (like height, depth, size, and balance factor).","archived":false,"fork":false,"pushed_at":"2024-08-20T06:58:50.000Z","size":43,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-02T18:49:47.660Z","etag":null,"topics":["algorithms","alx-software-engineering","binary-tree","c","c-language","c-programming","data-structures","tree-traversal","valgrind"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hackerSa3edy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-31T03:53:09.000Z","updated_at":"2024-08-20T07:03:18.000Z","dependencies_parsed_at":"2024-01-31T06:31:25.026Z","dependency_job_id":"6236666a-b76b-40bd-9669-7aaf1d0a4295","html_url":"https://github.com/hackerSa3edy/binary_trees","commit_stats":null,"previous_names":["hackersa3edy/binary_trees"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hackerSa3edy%2Fbinary_trees","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hackerSa3edy%2Fbinary_trees/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hackerSa3edy%2Fbinary_trees/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hackerSa3edy%2Fbinary_trees/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hackerSa3edy","download_url":"https://codeload.github.com/hackerSa3edy/binary_trees/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240131770,"owners_count":19752725,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithms","alx-software-engineering","binary-tree","c","c-language","c-programming","data-structures","tree-traversal","valgrind"],"created_at":"2024-11-09T02:14:41.481Z","updated_at":"2026-05-21T09:30:15.009Z","avatar_url":"https://github.com/hackerSa3edy.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Binary Tree Project\n\n## Objectives\n\nThis project aims to provide a comprehensive understanding of binary trees, including their structure, operations, and various properties. The objectives include:\n\n1. **Understanding Binary Trees**: Clarify the concept of a binary tree and its basic structure.\n\n2. **Difference Between Binary Tree and Binary Search Tree**: Highlight the distinctions between a binary tree and a binary search tree (BST), focusing on their key characteristics and use cases.\n\n3. **Time Complexity Gain Compared to Linked Lists**: Discuss the potential advantages in terms of time complexity that binary trees offer over linked lists for certain operations.\n\n4. **Exploration of Binary Tree Properties**: Introduce essential properties such as depth, height, and size of a binary tree and their significance in analyzing tree structures.\n\n5. **Traversal Methods**: Present various traversal methods used to navigate through binary trees, including pre-order, in-order, and post-order traversals.\n\n6. **Types of Binary Trees**: Define different types of binary trees such as complete, full, perfect, and balanced trees, highlighting their characteristics and applications.\n\n## Subtasks\n\n### 0. New node\n\n**file:**\n[0-binary_tree_node.c](./0-binary_tree_node.c)\n\n**Description:**\nCreate a function that creates a binary tree node.\n\n- Prototype: `binary_tree_t *binary_tree_node(binary_tree_t *parent, int value);`\n- Where `parent` is a pointer to the parent node of the node to create\n- And `value` is the value to put in the new node\n- When created, a node does not have any child\n- Your function must return a pointer to the new node, or NULL on failure\n\n```c\nalex@/tmp/binary_trees$ cat 0-main.c\n#include \u003cstdlib.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n\n    root = binary_tree_node(NULL, 98);\n\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eleft-\u003eleft = binary_tree_node(root-\u003eleft, 6);\n    root-\u003eleft-\u003eright = binary_tree_node(root-\u003eleft, 16);\n\n    root-\u003eright = binary_tree_node(root, 402);\n    root-\u003eright-\u003eleft = binary_tree_node(root-\u003eright, 256);\n    root-\u003eright-\u003eright = binary_tree_node(root-\u003eright, 512);\n\n    binary_tree_print(root);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 0-main.c 0-binary_tree_node.c -o 0-node\nalex@/tmp/binary_trees$ ./0-node\n       .-------(098)-------.\n  .--(012)--.         .--(402)--.\n(006)     (016)     (256)     (512)\nalex@/tmp/binary_trees$\n```\n\n### 1. Insert left\n\n**file:**\n[1-binary_tree_insert_left.c](./1-binary_tree_insert_left.c)\n\n**Description:**\nImplement a function to insert a node as the left-child of another node.\n\n- Prototype: `binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value);`\n- Where `parent` is a pointer to the node to insert the left-child in\n- And `value` is the value to store in the new node\n- Your function must return a pointer to the created node, or `NULL` on failure or if `parent` is `NULL`\n- If `parent` already has a left-child, the new node must take its place, and the old left-child must be set as the left-child of the new node.\n\n```c\nalex@/tmp/binary_trees$ cat 1-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_print(root);\n    printf(\"\\n\");\n    binary_tree_insert_left(root-\u003eright, 128);\n    binary_tree_insert_left(root, 54);\n    binary_tree_print(root);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 1-main.c 1-binary_tree_insert_left.c 0-binary_tree_node.c -o 1-left\nalex@/tmp/binary_trees$ ./1-left\n  .--(098)--.\n(012)     (402)\n\n       .--(098)-------.\n  .--(054)       .--(402)\n(012)          (128)\nalex@/tmp/binary_trees$\n```\n\n### 2. Insert right\n\n**file:**\n[2-binary_tree_insert_right.c](./2-binary_tree_insert_right.c)\n\n**Description:**\nDevelop a function to insert a node as the right-child of another node.\n\n- Prototype: `binary_tree_t *binary_tree_insert_right(binary_tree_t *parent, int value);`\n- Where `parent` is a pointer to the node to insert the right-child in\n- And `value` is the value to store in the new node\n- Your function must return a pointer to the created node, or `NULL` on failure or if `parent` is `NULL`\n- If `parent` already has a right-child, the new node must take its place, and the old right-child must be set as the right-child of the new node.\n\n```c\nalex@/tmp/binary_trees$ cat 2-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_print(root);\n    printf(\"\\n\");\n    binary_tree_insert_right(root-\u003eleft, 54);\n    binary_tree_insert_right(root, 128);\n    binary_tree_print(root);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 2-main.c 2-binary_tree_insert_right.c 0-binary_tree_node.c -o 2-right\nalex@/tmp/binary_trees$ ./2-right\n  .--(098)--.\n(012)     (402)\n\n  .-------(098)--.\n(012)--.       (128)--.\n     (054)          (402)\nalex@/tmp/binary_trees$\n```\n\n### 3. Delete\n\n**file:**\n[3-binary_tree_delete.c](./3-binary_tree_delete.c)\n\n**Description:**\nWrite a function to delete an entire binary tree.\n\n- Prototype: `void binary_tree_delete(binary_tree_t *tree);`\n- Where `tree` is a pointer to the root node of the tree to delete\n- If `tree` is `NULL`, do nothing\n\n```c\nalex@/tmp/binary_trees$ cat 3-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_insert_right(root-\u003eleft, 54);\n    binary_tree_insert_right(root, 128);\n    binary_tree_print(root);\n    binary_tree_delete(root);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 3-main.c 3-binary_tree_delete.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 3-del\nalex@/tmp/binary_trees$ valgrind ./3-del\n==13264== Memcheck, a memory error detector\n==13264== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.\n==13264== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info\n==13264== Command: ./3-del\n==13264==\n  .-------(098)--.\n(012)--.       (128)--.\n     (054)          (402)\n==13264==\n==13264== HEAP SUMMARY:\n==13264==     in use at exit: 0 bytes in 0 blocks\n==13264==   total heap usage: 9 allocs, 9 frees, 949 bytes allocated\n==13264==\n==13264== All heap blocks were freed -- no leaks are possible\n==13264==\n==13264== For counts of detected and suppressed errors, rerun with: -v\n==13264== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)\nalex@/tmp/binary_trees$\n```\n\n### 4. Is leaf\n\n**file:**\n[4-binary_tree_is_leaf.c](./4-binary_tree_is_leaf.c)\n\n**Description:**\nCreate a function to check if a node is a leaf.\n\n- Prototype: `int binary_tree_is_leaf(const binary_tree_t *node);`\n- Where `node` is a pointer to the node to check\n- Your function must return `1` if `node` is a leaf, otherwise `0`\n- If `node` is `NULL`, return `0`\n\n```c\nalex@/tmp/binary_trees$ cat 4-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n    int ret;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_insert_right(root-\u003eleft, 54);\n    binary_tree_insert_right(root, 128);\n    binary_tree_print(root);\n\n    ret = binary_tree_is_leaf(root);\n    printf(\"Is %d a leaf: %d\\n\", root-\u003en, ret);\n    ret = binary_tree_is_leaf(root-\u003eright);\n    printf(\"Is %d a leaf: %d\\n\", root-\u003eright-\u003en, ret);\n    ret = binary_tree_is_leaf(root-\u003eright-\u003eright);\n    printf(\"Is %d a leaf: %d\\n\", root-\u003eright-\u003eright-\u003en, ret);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 4-binary_tree_is_leaf.c 4-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 4-leaf\nalex@/tmp/binary_trees$ ./4-leaf\n  .-------(098)--.\n(012)--.       (128)--.\n     (054)          (402)\nIs 98 a leaf: 0\nIs 128 a leaf: 0\nIs 402 a leaf: 1\nalex@/tmp/binary_trees$\n```\n\n### 5. Is root\n\n**file:**\n[5-binary_tree_is_root.c](./5-binary_tree_is_root.c)\n\n**Description:**\nImplement a function to check if a given node is a root.\n\n- Prototype: `int binary_tree_is_root(const binary_tree_t *node);`\n- Where `node` is a pointer to the node to check\n- Your function must return `1` if `node` is a root, otherwise `0`\n- If `node` is `NULL`, return `0`\n\n```c\nalex@/tmp/binary_trees$ cat 5-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n    int ret;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_insert_right(root-\u003eleft, 54);\n    binary_tree_insert_right(root, 128);\n    binary_tree_print(root);\n\n    ret = binary_tree_is_root(root);\n    printf(\"Is %d a root: %d\\n\", root-\u003en, ret);\n    ret = binary_tree_is_root(root-\u003eright);\n    printf(\"Is %d a root: %d\\n\", root-\u003eright-\u003en, ret);\n    ret = binary_tree_is_root(root-\u003eright-\u003eright);\n    printf(\"Is %d a root: %d\\n\", root-\u003eright-\u003eright-\u003en, ret);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 5-binary_tree_is_root.c 5-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 5-root\nalex@/tmp/binary_trees$ ./5-root\n  .-------(098)--.\n(012)--.       (128)--.\n     (054)          (402)\nIs 98 a root: 1\nIs 128 a root: 0\nIs 402 a root: 0\nalex@/tmp/binary_trees$\n```\n\n### 6. Pre-order traversal\n\n**file:**\n[6-binary_tree_preorder.c](./6-binary_tree_preorder.c)\n\n**Description:**\nDevelop a function to traverse a binary tree using pre-order traversal.\n\n- Prototype: `void binary_tree_preorder(const binary_tree_t *tree, void (*func)(int));`\n- Where `tree` is a pointer to the root node of the tree to traverse\n- And `func` is a pointer to a function to call for each node. The value in the node must be passed as a parameter to this function.\n- If `tree` or `func` is `NULL`, do nothing\n\n```c\nalex@/tmp/binary_trees$ cat 6-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * print_num - Prints a number\n *\n * @n: Number to be printed\n */\nvoid print_num(int n)\n{\n    printf(\"%d\\n\", n);\n}\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    root-\u003eleft-\u003eleft = binary_tree_node(root-\u003eleft, 6);\n    root-\u003eleft-\u003eright = binary_tree_node(root-\u003eleft, 56);\n    root-\u003eright-\u003eleft = binary_tree_node(root-\u003eright, 256);\n    root-\u003eright-\u003eright = binary_tree_node(root-\u003eright, 512);\n\n    binary_tree_print(root);\n    binary_tree_preorder(root, \u0026print_num);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 6-main.c 6-binary_tree_preorder.c 0-binary_tree_node.c -o 6-pre\nalex@/tmp/binary_trees$ ./6-pre\n       .-------(098)-------.\n  .--(012)--.         .--(402)--.\n(006)     (056)     (256)     (512)\n98\n12\n6\n56\n402\n256\n512\nalex@/tmp/binary_trees$\n```\n\n### 7. In-order traversal\n\n**file:**\n[7-binary_tree_inorder.c](./7-binary_tree_inorder.c)\n\n**Description:**\nWrite a function to traverse a binary tree using in-order traversal.\n\n- Prototype: `void binary_tree_inorder(const binary_tree_t *tree, void (*func)(int));`\n- Where `tree` is a pointer to the root node of the tree to traverse\n- And `func` is a pointer to a function to call for each node. The value in the node must be passed as a parameter to this function.\n- If `tree` or `func` is `NULL`, do nothing\n\n```c\nalex@/tmp/binary_trees$ cat 7-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * print_num - Prints a number\n *\n * @n: Number to be printed\n */\nvoid print_num(int n)\n{\n    printf(\"%d\\n\", n);\n}\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    root-\u003eleft-\u003eleft = binary_tree_node(root-\u003eleft, 6);\n    root-\u003eleft-\u003eright = binary_tree_node(root-\u003eleft, 56);\n    root-\u003eright-\u003eleft = binary_tree_node(root-\u003eright, 256);\n    root-\u003eright-\u003eright = binary_tree_node(root-\u003eright, 512);\n\n    binary_tree_print(root);\n    binary_tree_inorder(root, \u0026print_num);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 7-main.c 7-binary_tree_inorder.c 0-binary_tree_node.c -o 7-in\nalex@/tmp/binary_trees$ ./7-in\n       .-------(098)-------.\n  .--(012)--.         .--(402)--.\n(006)     (056)     (256)     (512)\n6\n12\n56\n98\n256\n402\n512\nalex@/tmp/binary_trees$\n```\n\n### 8. Post-order traversal\n\n**file:**\n[8-binary_tree_postorder.c](./8-binary_tree_postorder.c)\n\n**Description:**\nCreate a function to traverse a binary tree using post-order traversal.\n\nPrototype: `void binary_tree_postorder(const binary_tree_t *tree, void (*func)(int));`\n\n- Where `tree` is a pointer to the root node of the tree to traverse\n- And `func` is a pointer to a function to call for each node. The value in the node must be passed as a parameter to this function.\n- If `tree` or `func` is `NULL`, do nothing\n\n```c\nalex@/tmp/binary_trees$ cat 8-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * print_num - Prints a number\n *\n * @n: Number to be printed\n */\nvoid print_num(int n)\n{\n    printf(\"%d\\n\", n);\n}\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    root-\u003eleft-\u003eleft = binary_tree_node(root-\u003eleft, 6);\n    root-\u003eleft-\u003eright = binary_tree_node(root-\u003eleft, 56);\n    root-\u003eright-\u003eleft = binary_tree_node(root-\u003eright, 256);\n    root-\u003eright-\u003eright = binary_tree_node(root-\u003eright, 512);\n\n    binary_tree_print(root);\n    binary_tree_postorder(root, \u0026print_num);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 8-main.c 8-binary_tree_postorder.c 0-binary_tree_node.c -o 8-post\nalex@/tmp/binary_trees$ ./8-post\n       .-------(098)-------.\n  .--(012)--.         .--(402)--.\n(006)     (056)     (256)     (512)\n6\n56\n12\n256\n512\n402\n98\nalex@/tmp/binary_trees$\n```\n\n### 9. Height\n\n**file:**\n[9-binary_tree_height.c](./9-binary_tree_height.c)\n\n**Description:**\nImplement a function to measure the height of a binary tree.\n\n- Prototype: `size_t binary_tree_height(const binary_tree_t *tree);`\n- Where `tree` is a pointer to the root node of the tree to measure the height.\n- If `tree` is `NULL`, your function must return `0`\n\n```c\nalex@/tmp/binary_trees$ cat 9-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n    size_t height;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_insert_right(root-\u003eleft, 54);\n    binary_tree_insert_right(root, 128);\n    binary_tree_print(root);\n\n    height = binary_tree_height(root);\n    printf(\"Height from %d: %lu\\n\", root-\u003en, height);\n    height = binary_tree_height(root-\u003eright);\n    printf(\"Height from %d: %lu\\n\", root-\u003eright-\u003en, height);\n    height = binary_tree_height(root-\u003eleft-\u003eright);\n    printf(\"Height from %d: %lu\\n\", root-\u003eleft-\u003eright-\u003en, height);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 9-binary_tree_height.c 9-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 9-height\nalex@/tmp/binary_trees$ ./9-height\n  .-------(098)--.\n(012)--.       (128)--.\n     (054)          (402)\nHeight from 98: 2\nHeight from 128: 1\nHeight from 54: 0\nalex@/tmp/binary_trees$\n```\n\n### 10. Depth\n\n**file:**\n[10-binary_tree_depth.c](./10-binary_tree_depth.c)\n\n**Description:**\nDevelop a function to measure the depth of a given node in a binary tree.\n\n- Prototype: `size_t binary_tree_depth(const binary_tree_t *tree);`\n- Where `tree` is a pointer to the node to measure the depth\n- If `tree` is `NULL`, your function must return `0`\n\n```c\nalex@/tmp/binary_trees$ cat 10-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n    size_t depth;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_insert_right(root-\u003eleft, 54);\n    binary_tree_insert_right(root, 128);\n    binary_tree_print(root);\n\n    depth = binary_tree_depth(root);\n    printf(\"Depth of %d: %lu\\n\", root-\u003en, depth);\n    depth = binary_tree_depth(root-\u003eright);\n    printf(\"Depth of %d: %lu\\n\", root-\u003eright-\u003en, depth);\n    depth = binary_tree_depth(root-\u003eleft-\u003eright);\n    printf(\"Depth of %d: %lu\\n\", root-\u003eleft-\u003eright-\u003en, depth);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 10-binary_tree_depth.c 10-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 10-depth\nalex@/tmp/binary_trees$ ./10-depth\n  .-------(098)--.\n(012)--.       (128)--.\n     (054)          (402)\nDepth of 98: 0\nDepth of 128: 1\nDepth of 54: 2\nalex@/tmp/binary_trees$\n```\n\n### 11. Size\n\n**file:**\n[11-binary_tree_size.c](./11-binary_tree_size.c)\n\n**Description:**\nWrite a function to measure the size of a binary tree (the number of nodes).\n\n- Prototype: `size_t binary_tree_size(const binary_tree_t *tree);`\n- Where `tree` is a pointer to the root node of the tree to measure the size\n- If `tree` is `NULL`, the function must return `0`\n\n```c\nalex@/tmp/binary_trees$ cat 11-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n    size_t size;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_insert_right(root-\u003eleft, 54);\n    binary_tree_insert_right(root, 128);\n    binary_tree_print(root);\n\n    size = binary_tree_size(root);\n    printf(\"Size of %d: %lu\\n\", root-\u003en, size);\n    size = binary_tree_size(root-\u003eright);\n    printf(\"Size of %d: %lu\\n\", root-\u003eright-\u003en, size);\n    size = binary_tree_size(root-\u003eleft-\u003eright);\n    printf(\"Size of %d: %lu\\n\", root-\u003eleft-\u003eright-\u003en, size);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 11-binary_tree_size.c 11-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 11-size\nalex@/tmp/binary_trees$ ./11-size\n  .-------(098)--.\n(012)--.       (128)--.\n     (054)          (402)\nSize of 98: 5\nSize of 128: 2\nSize of 54: 1\nalex@/tmp/binary_trees$\n```\n\n### 12. Leaves\n\n**file:**\n[12-binary_tree_leaves.c](./12-binary_tree_leaves.c)\n\n**Description:**\nCreate a function to count the leaves in a binary tree.\n\n- Prototype: `size_t binary_tree_leaves(const binary_tree_t *tree);`\n- Where `tree` is a pointer to the root node of the tree to count the number of leaves\n  If `tree` is `NULL`, the function must return 0\n- A `NULL` pointer is not a leaf\n\n```c\nalex@/tmp/binary_trees$ cat 12-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n    size_t leaves;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_insert_right(root-\u003eleft, 54);\n    binary_tree_insert_right(root, 128);\n    binary_tree_print(root);\n\n    leaves = binary_tree_leaves(root);\n    printf(\"Leaves in %d: %lu\\n\", root-\u003en, leaves);\n    leaves = binary_tree_leaves(root-\u003eright);\n    printf(\"Leaves in %d: %lu\\n\", root-\u003eright-\u003en, leaves);\n    leaves = binary_tree_leaves(root-\u003eleft-\u003eright);\n    printf(\"Leaves in %d: %lu\\n\", root-\u003eleft-\u003eright-\u003en, leaves);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 12-binary_tree_leaves.c 12-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 12-leaves\nalex@/tmp/binary_trees$ ./12-leaves\n  .-------(098)--.\n(012)--.       (128)--.\n     (054)          (402)\nLeaves in 98: 2\nLeaves in 128: 1\nLeaves in 54: 1\nalex@/tmp/binary_trees$\n```\n\n### 13. Nodes\n\n**file:**\n[13-binary_tree_nodes.c](./13-binary_tree_nodes.c)\n\n**Description:**\nImplement a function to count the nodes with at least one child in a binary tree.\n\n- Prototype: `size_t binary_tree_nodes(const binary_tree_t *tree);`\n- Where `tree` is a pointer to the root node of the tree to count the number of nodes\n- If `tree` is `NULL`, the function must return 0\n- A `NULL` pointer is not a node\n\n```c\nalex@/tmp/binary_trees$ cat 13-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n    size_t nodes;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_insert_right(root-\u003eleft, 54);\n    binary_tree_insert_right(root, 128);\n    binary_tree_print(root);\n\n    nodes = binary_tree_nodes(root);\n    printf(\"Nodes in %d: %lu\\n\", root-\u003en, nodes);\n    nodes = binary_tree_nodes(root-\u003eright);\n    printf(\"Nodes in %d: %lu\\n\", root-\u003eright-\u003en, nodes);\n    nodes = binary_tree_nodes(root-\u003eleft-\u003eright);\n    printf(\"Nodes in %d: %lu\\n\", root-\u003eleft-\u003eright-\u003en, nodes);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 13-binary_tree_nodes.c 13-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 13-nodes\nalex@/tmp/binary_trees$ ./13-nodes\n  .-------(098)--.\n(012)--.       (128)--.\n     (054)          (402)\nNodes in 98: 3\nNodes in 128: 1\nNodes in 54: 0\nalex@/tmp/binary_trees$\n```\n\n### 14. Balance factor\n\n**file:**\n[14-binary_tree_balance.c](./14-binary_tree_balance.c)\n\n**Description:**\nDevelop a function to measure the balance factor of a binary tree.\n\n- Prototype: `int binary_tree_balance(const binary_tree_t *tree);`\n- Where `tree` is a pointer to the root node of the tree to measure the balance factor\n- If `tree` is `NULL`, return `0`\n\n```c\nalex@/tmp/binary_trees$ cat 14-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n    int balance;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_insert_right(root-\u003eleft, 54);\n    binary_tree_insert_right(root, 128);\n    binary_tree_insert_left(root, 45);\n    binary_tree_insert_right(root-\u003eleft, 50);\n    binary_tree_insert_left(root-\u003eleft-\u003eleft, 10);\n    binary_tree_insert_left(root-\u003eleft-\u003eleft-\u003eleft, 8);\n    binary_tree_print(root);\n\n    balance = binary_tree_balance(root);\n    printf(\"Balance of %d: %+d\\n\", root-\u003en, balance);\n    balance = binary_tree_balance(root-\u003eright);\n    printf(\"Balance of %d: %+d\\n\", root-\u003eright-\u003en, balance);\n    balance = binary_tree_balance(root-\u003eleft-\u003eleft-\u003eright);\n    printf(\"Balance of %d: %+d\\n\", root-\u003eleft-\u003eleft-\u003eright-\u003en, balance);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 14-binary_tree_balance.c 14-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c 1-binary_tree_insert_left.c -o 14-balance\nalex@/tmp/binary_trees$ ./14-balance\n                      .-------(098)--.\n            .-------(045)--.       (128)--.\n       .--(012)--.       (050)          (402)\n  .--(010)     (054)\n(008)\nBalance of 98: +2\nBalance of 128: -1\nBalance of 54: +0\nalex@/tmp/binary_trees$\n```\n\n### 15. Is full\n\n**file:**\n[15-binary_tree_is_full.c](./15-binary_tree_is_full.c)\n\n**Description:**\nWrite a function to check if a binary tree is full (every node has 0 or 2 children).\n\n- Prototype: `int binary_tree_is_full(const binary_tree_t *tree);`\n- Where `tree` is a pointer to the root node of the tree to check\n- If `tree` is `NULL`, your function must return `0`\n\n```c\nalex@/tmp/binary_trees$ cat 15-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n    int full;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_insert_right(root-\u003eleft, 54);\n    binary_tree_insert_right(root, 128);\n    root-\u003eleft-\u003eleft = binary_tree_node(root-\u003eleft, 10);\n    binary_tree_print(root);\n\n    full = binary_tree_is_full(root);\n    printf(\"Is %d full: %d\\n\", root-\u003en, full);\n    full = binary_tree_is_full(root-\u003eleft);\n    printf(\"Is %d full: %d\\n\", root-\u003eleft-\u003en, full);\n    full = binary_tree_is_full(root-\u003eright);\n    printf(\"Is %d full: %d\\n\", root-\u003eright-\u003en, full);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 15-binary_tree_is_full.c 15-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 15-full\nalex@/tmp/binary_trees$ ./15-full\n       .-------(098)--.\n  .--(012)--.       (128)--.\n(010)     (054)          (402)\nIs 98 full: 0\nIs 12 full: 1\nIs 128 full: 0\nalex@/tmp/binary_trees$\n```\n\n### 16. Is perfect\n\n**file:**\n[16-binary_tree_is_perfect.c](./16-binary_tree_is_perfect.c)\n\n**Description:**\nCreate a function to check if a binary tree is perfect (all levels are fully filled).\n\n- Prototype: `int binary_tree_is_perfect(const binary_tree_t *tree);`\n- Where `tree` is a pointer to the root node of the tree to check\n- If `tree` is `NULL`, your function must return `0`\n\n```c\nalex@/tmp/binary_trees$ cat 16-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n    int perfect;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 402);\n    binary_tree_insert_right(root-\u003eleft, 54);\n    binary_tree_insert_right(root, 128);\n    root-\u003eleft-\u003eleft = binary_tree_node(root-\u003eleft, 10);\n    root-\u003eright-\u003eleft = binary_tree_node(root-\u003eright, 10);\n\n    binary_tree_print(root);\n    perfect = binary_tree_is_perfect(root);\n    printf(\"Perfect: %d\\n\\n\", perfect);\n\n    root-\u003eright-\u003eright-\u003eleft = binary_tree_node(root-\u003eright-\u003eright, 10);\n    binary_tree_print(root);\n    perfect = binary_tree_is_perfect(root);\n    printf(\"Perfect: %d\\n\\n\", perfect);\n\n    root-\u003eright-\u003eright-\u003eright = binary_tree_node(root-\u003eright-\u003eright, 10);\n    binary_tree_print(root);\n    perfect = binary_tree_is_perfect(root);\n    printf(\"Perfect: %d\\n\", perfect);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 16-binary_tree_is_perfect.c 16-main.c 0-binary_tree_node.c 2-binary_tree_insert_right.c -o 16-perfect\nalex@/tmp/binary_trees$ ./16-perfect\n       .-------(098)-------.\n  .--(012)--.         .--(128)--.\n(010)     (054)     (010)     (402)\nPerfect: 1\n\n       .-------(098)-------.\n  .--(012)--.         .--(128)-------.\n(010)     (054)     (010)       .--(402)\n                              (010)\nPerfect: 0\n\n       .-------(098)-------.\n  .--(012)--.         .--(128)-------.\n(010)     (054)     (010)       .--(402)--.\n                              (010)     (010)\nPerfect: 0\nalex@/tmp/binary_trees$\n```\n\n### 17. Sibling\n\n**file:**\n[17-binary_tree_sibling.c](./17-binary_tree_sibling.c)\n\n**Description:**\nCreate a function to find the sibling of a node\n\n- Prototype: `binary_tree_t *binary_tree_sibling(binary_tree_t *node);`\n- Where `node` is a pointer to the node to find the sibling\n- Your function must return a pointer to the sibling node\n- If `node` is `NULL` or the parent is `NULL`, return `NULL`\n- If `node` has no sibling, return `NULL`\n\n```c\nalex@/tmp/binary_trees$ cat 17-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n    binary_tree_t *sibling;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 128);\n    root-\u003eleft-\u003eright = binary_tree_node(root-\u003eleft, 54);\n    root-\u003eright-\u003eright = binary_tree_node(root-\u003eright, 402);\n    root-\u003eleft-\u003eleft = binary_tree_node(root-\u003eleft, 10);\n    root-\u003eright-\u003eleft = binary_tree_node(root-\u003eright, 110);\n    root-\u003eright-\u003eright-\u003eleft = binary_tree_node(root-\u003eright-\u003eright, 200);\n    root-\u003eright-\u003eright-\u003eright = binary_tree_node(root-\u003eright-\u003eright, 512);\n\n    binary_tree_print(root);\n    sibling = binary_tree_sibling(root-\u003eleft);\n    printf(\"Sibling of %d: %d\\n\", root-\u003eleft-\u003en, sibling-\u003en);\n    sibling = binary_tree_sibling(root-\u003eright-\u003eleft);\n    printf(\"Sibling of %d: %d\\n\", root-\u003eright-\u003eleft-\u003en, sibling-\u003en);\n    sibling = binary_tree_sibling(root-\u003eleft-\u003eright);\n    printf(\"Sibling of %d: %d\\n\", root-\u003eleft-\u003eright-\u003en, sibling-\u003en);\n    sibling = binary_tree_sibling(root);\n    printf(\"Sibling of %d: %p\\n\", root-\u003en, (void *)sibling);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 17-main.c 17-binary_tree_sibling.c 0-binary_tree_node.c -o 17-sibling\nalex@/tmp/binary_trees$ ./17-sibling\n       .-------(098)-------.\n  .--(012)--.         .--(128)-------.\n(010)     (054)     (110)       .--(402)--.\n                              (200)     (512)\nSibling of 12: 128\nSibling of 110: 402\nSibling of 54: 10\nSibling of 98: (nil)\nalex@/tmp/binary_trees$\n```\n\n### 18. Uncle\n\n**file:**\n[18-binary_tree_uncle.c](./18-binary_tree_uncle.c)\n\n**Description:**\nCreate a function to find the uncle of a node\n\n- Prototype: `binary_tree_t *binary_tree_uncle(binary_tree_t *node);`\n- Where `node` is a pointer to the node to find the uncle\n- Your function must return a pointer to the uncle node\n- If `node` is `NULL`, return `NULL`\n- If `node` has no uncle, return `NULL`\n\n```c\nalex@/tmp/binary_trees$ cat 18-main.c\n#include \u003cstdlib.h\u003e\n#include \u003cstdio.h\u003e\n#include \"binary_trees.h\"\n\n/**\n * main - Entry point\n *\n * Return: Always 0 (Success)\n */\nint main(void)\n{\n    binary_tree_t *root;\n    binary_tree_t *uncle;\n\n    root = binary_tree_node(NULL, 98);\n    root-\u003eleft = binary_tree_node(root, 12);\n    root-\u003eright = binary_tree_node(root, 128);\n    root-\u003eleft-\u003eright = binary_tree_node(root-\u003eleft, 54);\n    root-\u003eright-\u003eright = binary_tree_node(root-\u003eright, 402);\n    root-\u003eleft-\u003eleft = binary_tree_node(root-\u003eleft, 10);\n    root-\u003eright-\u003eleft = binary_tree_node(root-\u003eright, 110);\n    root-\u003eright-\u003eright-\u003eleft = binary_tree_node(root-\u003eright-\u003eright, 200);\n    root-\u003eright-\u003eright-\u003eright = binary_tree_node(root-\u003eright-\u003eright, 512);\n\n    binary_tree_print(root);\n    uncle = binary_tree_uncle(root-\u003eright-\u003eleft);\n    printf(\"Uncle of %d: %d\\n\", root-\u003eright-\u003eleft-\u003en, uncle-\u003en);\n    uncle = binary_tree_uncle(root-\u003eleft-\u003eright);\n    printf(\"Uncle of %d: %d\\n\", root-\u003eleft-\u003eright-\u003en, uncle-\u003en);\n    uncle = binary_tree_uncle(root-\u003eleft);\n    printf(\"Uncle of %d: %p\\n\", root-\u003eleft-\u003en, (void *)uncle);\n    return (0);\n}\nalex@/tmp/binary_trees$ gcc -Wall -Wextra -Werror -pedantic binary_tree_print.c 18-main.c 18-binary_tree_uncle.c 0-binary_tree_node.c -o 18-uncle\nalex@/tmp/binary_trees$ ./18-uncle\n       .-------(098)-------.\n  .--(012)--.         .--(128)-------.\n(010)     (054)     (110)       .--(402)--.\n                              (200)     (512)\nUncle of 110: 12\nUncle of 54: 128\nUncle of 12: (nil)\nalex@/tmp/binary_trees$\n```\n\n## Authors\n\n- [Abdelrahman Mohamed](https://github.com/hackerSa3edy)\n- [Zakaria Aaichaou](https://github.com/Z-Sitawi)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhackersa3edy%2Fbinary_trees","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhackersa3edy%2Fbinary_trees","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhackersa3edy%2Fbinary_trees/lists"}