Computer Science: Binary Search Tree demonstration using JavaScript.
npm install @tekksunn/cs-binary-search-tree!Computer Science | Binary Search Tree
This repository contains a JavaScript implementation of a Binary Search Tree (BST), providing foundational tree operations like insertion, deletion, searching, and traversal. The project supports traversal methods including in-order, pre-order, post-order, and level-order, along with balance checking and rebalancing.
``bash`
npm install @tekksunn/cs-hashmap
- insert(value) - Adds a new node to the BST while maintaining order.deleteItem(value)
- - Removes a node by value, handling cases with no children, one child, or two children.find(value)
- - Locates and retrieves a node based on its value.levelOrder(callback)
- - Traverses the tree in breadth-first level order, calling the provided callback function on each node.inOrder(callback)
- - Traverses the tree in in-order sequence (left-root-right) and calls the provided callback function on each node.preOrder(callback)
- - Traverses the tree in pre-order sequence (root-left-right) and calls the provided callback function on each node.postOrder(callback)
- - Traverses the tree in post-order sequence (left-right-root) and calls the provided callback function on each node.height(node)
- - Calculates and returns the height of the given node.depth(node)
- - Calculates and returns the depth of the given node relative to the tree’s root.isBalanced()
- - Checks if the tree is balanced.rebalance()
- - Rebalances the tree when it becomes unbalanced.prettyPrint()` - Visualizes the tree structure in a console-friendly format.
-
Feel free to fork this project and create a pull request if you'd like to contribute. Please make sure to write tests for any new features you add.
This project is open source and available under the MIT License.