https://github.com/hattima-tim/binary-search-tree
An implementation of a binary search tree in JavaScript
https://github.com/hattima-tim/binary-search-tree
algorithms-and-data-structures binary-search-tree javascript
Last synced: 9 months ago
JSON representation
An implementation of a binary search tree in JavaScript
- Host: GitHub
- URL: https://github.com/hattima-tim/binary-search-tree
- Owner: hattima-tim
- Created: 2022-10-21T04:07:29.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-17T14:37:58.000Z (about 3 years ago)
- Last Synced: 2025-01-18T13:48:28.238Z (11 months ago)
- Topics: algorithms-and-data-structures, binary-search-tree, javascript
- Language: JavaScript
- Homepage:
- Size: 111 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JavaScript Binary Search Tree
An implementation of a binary search tree data structure written in JavaScript.
Includes the following methods:
- `buildTree(array)` - takes an array of values and turns it into a balanced binary tree
- `insert(value)` - inserts a new node with the given value into the tree
- `deleteNode(value)` - removes the node holding the given value from the tree
- `find(value)` - returns the node holding the given value in the tree
- `levelOrder()` - traverses each node of the tree in level order
- `inorder()` - traverses each node of the tree inorder
- `preorder()` - traverses each node of the tree preorder
- `postorder()` - traverses each node of the tree postorder
- `height(value)` - returns the height of a node -- defined as the longest path between the node and a leaf node
- `depth(value)` - 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
To see some of the methods in action, run the `script.js` file with the `node script.js` command. To see how the methods are supposed to work, check the test file.