0.0.3 • Published 5 years ago

tree-n-heap v0.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Tree-N-Heap

A simple AVL Tree and Max(Min) Heap library which works in both client side and server side with no dependency.

Installation

For NodeJS

Make sure you have NodeJS and NPM installed.

Install this library

npm install tree-n-heap

For browser

Add script reference for tree-n-heap.js

<script type="text/javascript" src="tree-n-heap.js"></script>

Create Instance

For NodeJS

let { Tree, Heap, Errors } = require('tree-n-heap');

For Browser

let { Tree, Heap, Errors } = Tree_N_Heap;

Get Started - Tree (AVL)

AVL Tree is a self balanced binary search tree. You can refer to here for details.

Init

The init(list, isObjectMode) has two arguments,

  • list is an array. 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 is boolean. You can set it to true if you want to use the object mode. Default is false
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]) .

Insert

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.

Search

let n = tree.search(3);

The returned n is a Tree.Node object.

Remove

tree.remove(6);

Traversal

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]

Others

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.

Get Started - Heap

This library supports both Max Heap and Min Heap.

Init

The init(list, isMinHeap, isObjectMode) has three arguments,

  • list is an array. 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 is boolean. You can set it to true if you want to build a Min Heap. Otherwise the heap will be a Max Heap. Default is false
  • isObjectMode is boolean. You can set it to true if you want to use the object mode. Default is false
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                  

Push

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                                                                             

Pop

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

Peek will return the root node of the heap, but won't remove it from heap.

let n = heap.peek();

Search

let n = heap.search(3);

The returned n is the found element.

Others

The toString() method will generate a string to visualize the heap.

The print() method will print the result of toString() to console.