{"id":18538491,"url":"https://github.com/bryanbill/binarysearchtree","last_synced_at":"2025-05-15T02:12:23.298Z","repository":{"id":111162798,"uuid":"349184327","full_name":"bryanbill/BinarySearchTree","owner":"bryanbill","description":"Binary Search Tree implemented in C language. The algorithms contain some common functions such as insert, delete, rebalance, search e.t.c","archived":false,"fork":false,"pushed_at":"2021-03-18T19:08:38.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-17T07:45:06.343Z","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/bryanbill.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":"2021-03-18T18:50:10.000Z","updated_at":"2021-03-20T21:01:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"b1cf808e-4f6c-4e7b-9c41-5ca07a018aad","html_url":"https://github.com/bryanbill/BinarySearchTree","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bryanbill%2FBinarySearchTree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bryanbill%2FBinarySearchTree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bryanbill%2FBinarySearchTree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bryanbill%2FBinarySearchTree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bryanbill","download_url":"https://codeload.github.com/bryanbill/BinarySearchTree/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254259439,"owners_count":22040821,"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-06T19:43:59.960Z","updated_at":"2025-05-15T02:12:23.255Z","avatar_url":"https://github.com/bryanbill.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BinarySearchTree\n\nBinary Search Tree implemented in C language. The algorithms contain some common functions such as insert, delete, rebalance, search e.t.c\n\n## Methods Implemented\n\n1. Initialize the Nodes\n\u003cpre\u003e\nNode *initBinaryTree(int data)\n{\n    Node *new_node = (Node *)malloc(sizeof(Node));\n    if (new_node == NULL)\n    {\n        fprintf(stderr, \"Out of memory\\n\");\n        exit(1);\n    }\n    new_node-\u003edata = data;\n    new_node-\u003eleft = NULL;\n    new_node-\u003eright = NULL;\n    return new_node;\n}\n\u003c/pre\u003e\n\n2. Insert New Nodes\n\u003cpre\u003e\nNode *insertItem(Node *root, comparer compare, int data)\n{\n\n    if (root == NULL)\n    {\n        root = initBinaryTree(data);\n    }\n    else\n    {\n        int is_left = 0;\n        int r = 0;\n        Node *cursor = root;\n        Node *prev = NULL;\n\n        while (cursor != NULL)\n        {\n            r = compare(data, cursor-\u003edata);\n            prev = cursor;\n            if (r \u003c 0)\n            {\n                is_left = 1;\n                cursor = cursor-\u003eleft;\n            }\n            else if (r \u003e 0)\n            {\n                is_left = 0;\n                cursor = cursor-\u003eright;\n            }\n        }\n        if (is_left)\n            prev-\u003eleft = initBinaryTree(data);\n        else\n            prev-\u003eright = initBinaryTree(data);\n    }\n    return root;\n}\n\u003c/pre\u003e\n\n3. Delete a Node\n\u003cpre\u003e\nNode *removeItem(Node *root, int data, comparer compare)\n{\n    if (root == NULL)\n        return NULL;\n\n    Node *cursor;\n    int r = compare(data, root-\u003edata);\n    if (r \u003c 0)\n        root-\u003eleft = removeItem(root-\u003eleft, data, compare);\n    else if (r \u003e 0)\n        root-\u003eright = removeItem(root-\u003eright, data, compare);\n    else\n    {\n        if (root-\u003eleft == NULL)\n        {\n            cursor = root-\u003eright;\n            free(root);\n            root = cursor;\n        }\n        else if (root-\u003eright == NULL)\n        {\n            cursor = root-\u003eleft;\n            free(root);\n            root = cursor;\n        }\n        else //2 children\n        {\n            cursor = root-\u003eright;\n            Node *parent = NULL;\n\n            while (cursor-\u003eleft != NULL)\n            {\n                parent = cursor;\n                cursor = cursor-\u003eleft;\n            }\n            root-\u003edata = cursor-\u003edata;\n            if (parent != NULL)\n                parent-\u003eleft = removeItem(parent-\u003eleft, parent-\u003eleft-\u003edata, compare);\n            else\n                root-\u003eright = removeItem(root-\u003eright, root-\u003eright-\u003edata, compare);\n        }\n    }\n    return root;\n}\n\u003c/pre\u003e\n\n4. Search for a key in the Nodes\n\u003cpre\u003e\n\nNode *findItem(Node *root, const int data, comparer compare)\n{\nif (root == NULL)\nreturn NULL;\n\n    int r;\n    Node *cursor = root;\n    while (cursor != NULL)\n    {\n        r = compare(data, cursor-\u003edata);\n        if (r \u003c 0)\n            cursor = cursor-\u003eleft;\n        else if (r \u003e 0)\n            cursor = cursor-\u003eright;\n        else\n            return cursor;\n    }\n    return cursor;\n\n}\n\n\u003c/pre\u003e\n\n5. Free Nodes\n\u003cpre\u003e\nvoid freeNodes(Node *root)\n{\n    if (root != NULL)\n    {\n        free(root);\n    }\n}\n\u003c/pre\u003e\n\n## Makefile\n\n### all: Build the app and install\n\u003ccode\u003e\n    make all\n\u003c/code\u003e\n \n ### clean:clean the build directory and remove all generated objects and libraries\n\u003ccode\u003e\n    make clean\n\u003c/code\u003e\n\n## The Makefile\n\u003cpre\u003e\nCC_VERBOSE = $(CC)\nCC_NO_VERBOSE = @echo \"Building $@...\"; $(CC)\nDESTDIR?=/usr/lib\n\nifeq ($(VERBOSE),YES)\n  V_CC = $(CC_VERBOSE)\n  AT := \nelse\n  V_CC = $(CC_NO_VERBOSE)\n  AT := @\nendif\n\nC_FILES = $(wildcard *.c bin/*.c)\nO_FILES = $(C_FILES:src/%.c=build/objects/%.o)\n\n.PHONY: all clean\n.DEFAULT: all\n\nall: main\n\nmain: $(O_FILES)\n\t$(V_CC) -I../include/ -o $@ $^\n\nbuild:\n\t$(AT)mkdir -p ../build/objects\n\t$(AT)mkdir -p ../build/lib\n\nbuild/%.o: src/%.c | build\n\t$(V_CC) -c $\u003c -o $@\nbin:\n\tgcc bin/main.c -o main\ninstall: \n\tmv ../build/lib ${DESTDIR}\nclean:\n\t@echo Removing build directory\n\t$(AT)-rm -rf build\n\u003c/pre\u003e\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbryanbill%2Fbinarysearchtree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbryanbill%2Fbinarysearchtree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbryanbill%2Fbinarysearchtree/lists"}