An open API service indexing awesome lists of open source software.

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)

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.**