npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tree-n-heap

v0.0.3

Published

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

Downloads

2

Readme

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.