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 treedelete(key)
- deletes the node holding the given key from the treefind(key)
- returns the node holding the given key in the treelevelOrder(callbackFn)
* - traverses each node of the tree in level order.inorder(callbackFn)
* - traverses each node of the tree inorderpreorder(callbackFn)
* - traverses each node of the tree preorderpostorder(callbackFn)
* - traverses each node of the tree postorderheight(node)
- returns the height of a node -- defined as the longest path between the node and a leaf nodedepth(node)
- returns the depth of a node -- defined as the distance between the node and the rootisBalanced()
- returns true/false based on whether or not the tree is balancedrebalance()
- rebalances the treeprettyPrint()
- 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.