{"id":25204271,"url":"https://github.com/kbelltree/odin-bst","last_synced_at":"2025-07-25T23:18:33.359Z","repository":{"id":251464535,"uuid":"829184766","full_name":"kbelltree/odin-bst","owner":"kbelltree","description":"The Odin Project: Binary Search Trees","archived":false,"fork":false,"pushed_at":"2024-08-20T08:33:22.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-10T08:17:14.153Z","etag":null,"topics":["bst","javascript","odin-project"],"latest_commit_sha":null,"homepage":"https://github.com/kbelltree/odin-bst/blob/main/bst.js","language":"JavaScript","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/kbelltree.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-07-15T23:49:52.000Z","updated_at":"2024-08-20T08:33:26.000Z","dependencies_parsed_at":"2024-08-03T08:23:26.712Z","dependency_job_id":"3e10aed3-0fb7-443a-8b8d-550bf3bc6e88","html_url":"https://github.com/kbelltree/odin-bst","commit_stats":null,"previous_names":["kbelltree/odin-bst"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kbelltree%2Fodin-bst","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kbelltree%2Fodin-bst/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kbelltree%2Fodin-bst/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kbelltree%2Fodin-bst/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kbelltree","download_url":"https://codeload.github.com/kbelltree/odin-bst/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266483,"owners_count":20910832,"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":["bst","javascript","odin-project"],"created_at":"2025-02-10T08:17:16.005Z","updated_at":"2025-04-04T23:43:27.724Z","avatar_url":"https://github.com/kbelltree.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Project: Binary Search Trees\n\nThis project involves the implementation of a Binary Search Tree in JavaScript, using either class or factory functions. For comprehensive details on this project, please refer to [The Odin Project - Project: Binary Search Trees](https://www.theodinproject.com/lessons/javascript-binary-search-trees).\n\n## Key Project Instructions:\n\nRemove duplicates and sort the array before creating the BST.\n\n### Two Classes or Factory Functions\n\nThe project consists of two main components:\n\n- `Node`:\u003cbr\u003e\n  Consists of data, left child, and right child attributes.\n\n- `Tree`:\u003cbr\u003e\n  Takes an array and stores the BST created from `buildTree` in the root attribute.\n\n### Methods To Implement\n\n- `buildTree(array)`:\u003cbr\u003e\n  Develop a balanced BST from an array of data. This tree should contain Node instances for each piece of data. The returned value should be the topmost node.\n\n- `insert(value)`:\u003cbr\u003e\n  Insert a value. The key is to directly manipulate the BST by traversing it, which should achieve a time efficiency of O(log n).\n\n- `delete(value)`:\u003cbr\u003e\n  Delete a value. This operation should consider time efficiency of O(log n) and include several key factors as conditions to achieve deletion.\n\n- `find(value)`:\u003cbr\u003e\n  Search for and return a node containing the value.\n\n- `levelOrder(callback)`, `inOrder(callback)`, `preOrder(callback)`, `postOrder(callback)`:\u003cbr\u003e\n  Traverse each node in their distinctive breadth or depth-first orders, like `Array.prototype.forEach`, and apply a callback on each node. This callback takes a node as its parameter. An error should be thrown if no callback is passed.\n- `height(node)`:\u003cbr\u003e\n  Returns the height of the node passed.\n\n- `depth(node)`:\u003cdr\u003e\n  Returns the depth of the node passed.\n\n- `isBalanced`:\u003cdr\u003e\n  Inspect if the tree is balanced.\n\n- `rebalance`:\u003cbr\u003e\n  Fix the unbalanced tree to be balanced. The key is to recreate an array utilizing a traversal method and apply it to `buildTree`.\n\n**[Link to my final solution](./bst.js)**\n\n### Driver Script\n\n1. Create a BST from an array of numbers less than 100.\n2. Verify the BST is balanced using `isBalanced`.\n3. Print all elements in level, pre, post, and in orders.\n4. Add some numbers that are more than 100 to unbalance the tree.\n5. Verify the BST is unbalanced using `isBalanced`.\n6. Make the BST balanced using `rebalance`.\n7. Verify the BST is balanced using `isBalanced`.\n8. Print all elements in level, pre, post, and in orders.\n\n**[Link to the driver script](./driver.js)**\n\n## Built With\n\n- JavaScript\n- ESLint\n- Prettier\n\n---\n\n### Referenced Tutorials\n\n- [What is the fastest way to remove duplicates from an array in javascript? by Mathieu Collette](https://medium.com/@collettemathieu/what-is-the-fastest-way-to-remove-duplicates-from-an-array-in-javascript-9e5b4d3f55e1)\n\n- [Breadth first search by Programiz](https://www.programiz.com/dsa/graph-bfs)\n\n- [Recursive Level-order Traversal by Baeldung](https://www.baeldung.com/cs/level-order-traversal-binary-tree#2-recursive-level-order-traversal)\n\n- [Recursive Binary Tree Traversals: Preorder, Inorder and Postorder by EnjoyAlgorithms](https://www.enjoyalgorithms.com/blog/binary-tree-traversals-preorder-inorder-postorder)\n\n- [Find height of a binary tree by mycodeschool](https://youtu.be/_pnqMz5nrRs?si=EJlci-rvKYeszRZI)\n\n- [Finding height in Binary Search Tree by Stack Overflow](https://stackoverflow.com/questions/2597637/finding-height-in-binary-search-tree)\n\n- [Test If A Binary Tree Is Height Balanced (\"Balanced Binary Tree\" on LeetCode) by Back To Back SWE](https://youtu.be/LU4fGD-fgJQ?si=Kujoe52ti6EAZmJx);\n\n- [Program to check whether a tree is height balanced or not in C++ by Tutorials Point](https://www.tutorialspoint.com/program-to-check-whether-a-tree-is-height-balanced-or-not-in-cplusplus)\n\n- [Interview Question: Balanced Binary Tree by Byte by Byte](https://youtu.be/nOcFiGl5Vy4?si=Ihn2ABXT4apioIEE)\n\n- [Balanced Binary Tree - Leetcode 110 - Python by NeetCode](https://youtu.be/QfJsau0ItOY?si=dJ73cjLHHd4aB5D7)\n\n- [Generate a JavaScript array of random integers in a given range by 30 seconds of code](https://www.30secondsofcode.org/js/s/random-integer-array-in-range/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkbelltree%2Fodin-bst","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkbelltree%2Fodin-bst","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkbelltree%2Fodin-bst/lists"}