Package Exports
- @datastructures-js/binary-search-tree
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@datastructures-js/binary-search-tree) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@datastrucures-js/binary-search-tree
node's data type: string, number.

Usage
const binarySearchTree = require('@datastructures-js/binary-search-tree');
const bst = binarySearchTree();
API
.node(value, parent, left, right)
creates a bst node.
- .setValue(value) sets the node's value.
- .getValue(value) gets the node's value.
- .setParent(parent) sets the parent node.
- .getParent(parent) gets the parent node.
- .setLeft(left) sets the node's left child.
- .getLeft() gets the node's left child.
- .setRight(left) sets the node's right child.
- .getRight() gets the node's right child.
const n = bst.node('test', null,);
console.log(n.getValue()); // test
console.log(n.getParent()); // null
console.log(n.getLeft()); // null
console.log(n.getRight()); // null
.insert(value)
inserts a value into the tree.
bst.insert(50);
bst.insert(80);
bst.insert(30);
bst.insert(90);
bst.insert(60);
bst.insert(40);
bst.insert(20);
.root()
gets the root node
console.log(bst.root().getValue()); // 90
.min()
finds the min value node (most left).
console.log(bst.min().getValue()); // 20
.max()
finds the min value node (most right).
console.log(bst.max().getValue()); // 90
.count()
gets nodes count.
console.log(bst.count()); // 7
.find(value)
finds the value's node or returns null if not found.
let n = bst.find(30);
console.log(n.getValue()); // 30
console.log(n.getRight().getValue()); // 40
console.log(n.getLeft().getValue()); // 20
.traverseInOrder(cb)
// in-order traverse (left-parent-right)
bst.traverseInOrder(node => console.log(node.getValue()));
// 20
// 30
// 40
// 50
// 60
// 80
// 90
.traversePreOrder(cb)
// pre-order traverse (parent-left-right)
bst.traversePreOrder(node => console.log(node.getValue()));
// 50
// 30
// 20
// 40
// 80
// 60
// 90
.traversePostOrder(cb)
// post-order traverse (left-right-parent)
bst.traverse(node => console.log(node.getValue()));
// 20
// 40
// 30
// 60
// 90
// 80
// 50
.traverse(cb, order)
traverse the tree in the defined order and apply a callback on each node.
order values: inOrder
, preOrder
OR postOrder
. default is inOrder
bst.traverse(node => console.log(node.getValue())); // in-order
// 20
// 30
// 40
// 50
// 60
// 80
// 90
bst.traverse(node => console.log(node.getValue()), 'preOrder');
// 50
// 30
// 20
// 40
// 80
// 60
// 90
.remove(value)
removes a value's node (if exists) from the tree.
bst.remove(30);
let n50 = bst.find(50);
let n40 = bst.find(40);
console.log(n50.getLeft().getValue()); // 40
console.log(n40.getLeft().getValue()); // 20
.clear()
clears the tree.
bst.clear();
console.log(bst.count()); // 0
Build
grunt build
License
The MIT License. Full License is here