https://github.com/orelba/binary-search-tree
Building a balanced binary search tree (BST)
https://github.com/orelba/binary-search-tree
balanced-search-trees binary-search-tree computer-science data-structures
Last synced: about 1 year ago
JSON representation
Building a balanced binary search tree (BST)
- Host: GitHub
- URL: https://github.com/orelba/binary-search-tree
- Owner: Orelba
- Created: 2023-02-04T17:03:54.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-09T20:16:12.000Z (about 3 years ago)
- Last Synced: 2025-01-13T05:42:08.329Z (about 1 year ago)
- Topics: balanced-search-trees, binary-search-tree, computer-science, data-structures
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Data Structures: Binary Search Tree
An implementation of a balanced binary search tree (BST) in Javascript.
## To run the script:
Run `script.js`.
There is already a demonstration of some of the methods in the file,
You can use all the methods available below.
## Includes the following methods:
* `insert(key)` - inserts a new node with the given key into the tree
* `delete(key)` - deletes the node holding the given key from the tree
* `find(key)` - returns the node holding the given key in the tree
* `levelOrder(callbackFn)` ***** - traverses each node of the tree in level order.
* `inorder(callbackFn)` ***** - traverses each node of the tree inorder
* `preorder(callbackFn)` ***** - traverses each node of the tree preorder
* `postorder(callbackFn)` ***** - traverses each node of the tree postorder
* `height(node)` - returns the height of a node -- defined as the longest path between the node and a leaf node
* `depth(node)` - returns the depth of a node -- defined as the distance between the node and the root
* `isBalanced()` - returns true/false based on whether or not the tree is balanced
* `rebalance()` - rebalances the tree
* `prettyPrint()` - prints the tree in the console in a human reader friendly format
### Additional note for methods that have an asterisk(*):
***** **If a callback function is provided, each node of the binary tree will be provided as the argument to the provided function, otherwise an array of node keys will be returned.**