{"id":19248813,"url":"https://github.com/kevinkoech357/binary_trees","last_synced_at":"2025-02-23T16:16:15.862Z","repository":{"id":185958629,"uuid":"673947751","full_name":"kevinkoech357/binary_trees","owner":"kevinkoech357","description":"Exploring binary trees in C.","archived":false,"fork":false,"pushed_at":"2023-08-03T21:39:38.000Z","size":53,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-05T05:41:44.644Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/kevinkoech357.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":"2023-08-02T19:37:53.000Z","updated_at":"2023-08-02T19:48:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"a20fabf4-3c7b-476c-8af6-684ae32e194e","html_url":"https://github.com/kevinkoech357/binary_trees","commit_stats":null,"previous_names":["kevinkoech357/binary_trees"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinkoech357%2Fbinary_trees","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinkoech357%2Fbinary_trees/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinkoech357%2Fbinary_trees/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevinkoech357%2Fbinary_trees/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kevinkoech357","download_url":"https://codeload.github.com/kevinkoech357/binary_trees/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240339582,"owners_count":19785957,"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":[],"created_at":"2024-11-09T18:10:42.431Z","updated_at":"2025-02-23T16:16:15.819Z","avatar_url":"https://github.com/kevinkoech357.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 0x1D. C - Binary trees\n\nA binary tree is a hierarchical data structure where each node has at most two children: a left child and a right child. It is commonly used for efficient searching, insertion, and deletion operations. Each node in a binary tree contains a value and pointers to its left and right children, which can be NULL if the child does not exist.\n\nHere's a basic structure for a binary tree node in C:\n\n```c\nstruct Node {\n    int data;\n    struct Node* left;\n    struct Node* right;\n};\n```\n\nTo create a binary tree, you'll typically start with its root node, and then build the tree by adding new nodes as needed. Here's a simple function to create a new node:\n\n```c\nstruct Node* createNode(int data) {\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\n    newNode-\u003edata = data;\n    newNode-\u003eleft = NULL;\n    newNode-\u003eright = NULL;\n    return newNode;\n}\n```\n\nTo insert a new node into the binary tree, you'll need to find the appropriate position based on the node's value. Here's an example of an insertion function:\n\n```c\nstruct Node* insert(struct Node* root, int data) {\n    if (root == NULL) {\n        return createNode(data);\n    }\n\n    if (data \u003c root-\u003edata) {\n        root-\u003eleft = insert(root-\u003eleft, data);\n    } else if (data \u003e root-\u003edata) {\n        root-\u003eright = insert(root-\u003eright, data);\n    }\n\n    return root;\n}\n```\n\nFor binary tree traversal, there are three common methods: inorder, preorder, and postorder. In-order traversal visits the left subtree, then the current node, and finally the right subtree. Pre-order traversal visits the current node, then the left subtree, and finally the right subtree. Post-order traversal visits the left subtree, then the right subtree, and finally the current node.\n\nHere's an example of an in-order traversal function:\n\n```c\nvoid inorderTraversal(struct Node* root) {\n    if (root == NULL) {\n        return;\n    }\n\n    inorderTraversal(root-\u003eleft);\n    printf(\"%d \", root-\u003edata);\n    inorderTraversal(root-\u003eright);\n}\n```\n\nRemember to deallocate memory once you are done using the binary tree to prevent memory leaks.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkevinkoech357%2Fbinary_trees","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkevinkoech357%2Fbinary_trees","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkevinkoech357%2Fbinary_trees/lists"}