A simple AVL Tree and Max(Min) Heap library which works in both client side and server side with no dependency.
Make sure you have NodeJS and NPM installed.
Install this library
npm install tree-n-heap
Add script reference for tree-n-heap.js
<script type="text/javascript" src="tree-n-heap.js"></script>
let { Tree, Heap, Errors } = require('tree-n-heap');
let { Tree, Heap, Errors } = Tree_N_Heap;
AVL Tree is a self balanced binary search tree. You can refer to here for details.
The init(list, isObjectMode)
has two arguments,
list
is anarray
. The array element can be numerical value or JSON object. For JSON object, make sure each element has a key property. For example{key: 6, ...}
isObjectMode
isboolean
. You can set it totrue
if you want to use the object mode. Default isfalse
let tree = new Tree();
tree.init([2, 6, 3, 1, 4, 9]);
tree.print();
You should get below output in console
3
/ \
1 6
\ / \
2 4 9
The initial data can be put in the constructor too. For example,
new Tree([1, 2, 3])
.
tree.insert(12);
tree.insert([8, 17]);
tree.print();
Insert()
supports both single and multiple values.
You should get below output in console
3
/ \
1 9
\ / \
2 6 12
/ \ \
4 8 17
Notice several rotations have been done to maintain its AVL properties.
let n = tree.search(3);
The returned
n
is aTree.Node
object.
tree.remove(6);
There are three traversal orders: PreOrder
, InOrder
and PostOrder
. You can use the built in enumerate values for order.
tree.init([2, 6, 3, 1, 4, 9]);
let ORDER = tree.CONST.ORDER;
let result = tree.traversal(ORDER.InOrder);
console.log(result);
Since it's InOrder, the result is a sorted array.
[1, 2, 3, 4, 6, 9]
The toJSON()
method will generate a JSON object of the tree. Just in case.
{
"v": 3,
"l": {
"v": 1,
"r": {
"v": 2
}
},
"r": {
"v": 6,
"l": {
"v": 4
},
"r": {
"v": 9
}
}
}
The toString()
method will generate a string to visualize the tree.
The print()
method will print the result of toString()
to console
.
This library supports both Max Heap and Min Heap.
The init(list, isMinHeap, isObjectMode)
has three arguments,
list
is anarray
. The array element can be numerical value or JSON object. For JSON object, make sure each element has a key property. For example{key: 6, ...}
isMinHeap
isboolean
. You can set it totrue
if you want to build a Min Heap. Otherwise the heap will be a Max Heap. Default isfalse
isObjectMode
isboolean
. You can set it totrue
if you want to use the object mode. Default isfalse
let heap = new Heap();
heap.init([2, 6, 3, 1, 4, 9]);
heap.print();
The initial data can be put in the constructor too. For example,
new Heap([1, 2, 3])
.
You should get below output in console
9
/ \
6 3
/ \ /
1 4 2
heap.push(12);
heap.push([8, 17]);
heap.print();
Push()
supports both single and multiple values.
You should get below output in console
17
/ \
12 9
/ \ / \
8 4 2 3
/ \
1 6
console.log(heap.pop());
console.log(heap.pop());
heap.print();
pop()
will remove the root node from the heap.
You should get below output in console
17
12
9
/ \
8 3
/ \ / \
6 4 2 1
Peek will return the root node of the heap, but won't remove it from heap.
let n = heap.peek();
let n = heap.search(3);
The returned
n
is the found element.
The toString()
method will generate a string to visualize the heap.
The print()
method will print the result of toString()
to console
.