@datastrucures-js/avl-tree

node's data type: string, number.
Usage
const avlTree = require('@datastructures-js/avl-tree');
const avl = avlTree();
API
.node(value, parent, left, right)
creates a binary tree node with height property.
- .setValue(value) sets the node's value.
- .getValue() gets the node's value.
- .setParent(parent) sets the parent node.
- .getParent() gets the parent node.
- .setLeft(left) sets the node's left child.
- .getLeft() gets the node's left child.
- .setRight(right) sets the node's right child.
- .getRight() gets the node's right child.
- .setHeight(height) sets the node's height.
- .getHeight() gets the node's height.
const n = avl.node('test');
console.log(n.getValue());
console.log(n.getParent());
console.log(n.getLeft());
console.log(n.getRight());
console.log(n.getHeight());
.insert(value)
inserts a value into the tree and maintains the tree balance by rotating the imbalanced node.
avl.insert(20);
avl.insert(30);
avl.insert(40);
avl.insert(50);
avl.insert(60);
avl.insert(70);
avl.insert(80);
.root()
gets the root node
console.log(avl.root().getValue());
.min()
finds the min value node (most left).
console.log(avl.min().getValue());
.max()
finds the min value node (most right).
console.log(avl.max().getValue());
.count()
gets nodes count.
console.log(avl.count());
.find(value)
finds the value's node or returns null if not found.
let n = avl.find(30);
console.log(n.getValue());
console.log(n.getRight().getValue());
console.log(n.getLeft().getValue());
console.log(n.getHeight());
.traverseInOrder(cb)
avl.traverseInOrder(node => console.log(node.getValue()));
.traversePreOrder(cb)
avl.traversePreOrder(node => console.log(node.getValue()));
.traversePostOrder(cb)
avl.traverse(node => console.log(node.getValue()));
.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
avl.traverse(node => console.log(node.getValue()));
avl.traverse(node => console.log(node.getValue()), 'preOrder');
.remove(value)
removes a value's node (if exists) from the tree and maintains the tree balance by rotating the imbalanced node.
console.log(bst.find(30).getValue());
bst.remove(30);
console.log(bst.find(30));
.clear()
clears the tree.
avl.clear();
console.log(avl.count());
Build
grunt build
License
The MIT License. Full License is here