Structures-Wiz is a JavaScript based npm package for using awesome data structures like Stacks, Queue, LinkedList, PriorityQueues etc.
Use the npm package manager to install Structures-Wiz.
npm install structures-wiz
Priority Queues are an extension to queues. Each entry into a Priority Queue is based on its Priority. The advantage of using Priority Queues is that insertion of elements takes O(log n) time because they are implementation is based on Max Heap or Min Heap.
import { PriorityQueue } from 'structures-wiz';
//Default contructor will create a Max-Heap
const priorityQ = new PriorityQueue();
//Passing custom comparator for Min Heap
const priorityQ = new PriorityQueue((a,b) => a <= b);
Following are the methods exposed for usage:
Adds new value to the Queue based on its priority and if Priority is not passed then Priority is equal to Value.
import { PriorityQueue } from 'structures-wiz';
//Default contructor will create a Max-Heap
const priorityQ = new PriorityQueue();
//Way 1 - Without specifying Priority (Priority = Value in this case by default)
priorityQ.enqueue(10);
//Way 2 - Passing Value & Priority
priorityQ.enqueue(10, 100);
Removes the Maximum element (In case of Max-Heap config) or Minimum Element (In case of Min-Heap config).
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 90);
priorityQ.dequeue(); //Removes the Highest Priority Element
Returns the size of the queue.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 90);
priorityQ.getSize(); //2
Returns the left child index of the passed index.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const leftIndex = priorityQ.getLeftIndex(0);
console.log("LeftIndex of 0th index is :",leftIndex); //1
Returns the right child index of the passed index.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const rightIndex = priorityQ.getRightIndex(0);
console.log("RightIndex of 0th index is :",rightIndex); //2
Returns the parent node index of the passed index.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const parentIndex = priorityQ.getParentIndex(1);
console.log("ParentIndex of 1st index is :",parentIndex);//0
Returns the Maximum element (In case of Max-Heap config) or Minimum Element (In case of Min-Heap config).
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const peekValue = priorityQ.peek();
console.log("Peak Value is :",peekValue); //[ 70 , 200 ]
Returns the current heap in the sorted order ( Descending if config is set as Max-Heap else Ascending in case of Min-Heap)
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const sortedHeap = priorityQ.getSortedHeap();
console.log("Sorted Heap is :",sortedHeap);
/*[
[ 70, 200 ],
[ 10, 100 ],
[ 60, 90 ],
[ 20, 80 ],
[ 50, 40 ],
[ 40, 20 ]
]*/
If the Priority Queue is a Max Heap( by default ) then this method returns 'kth' Max Element in the Sorted Max Heap otherwise in case of Min Heap it returns 'kth' Min Element in the Sorted Min Heap .
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
// kthMax element ( As Max Heap )
const k = 2;
const kthElement = priorityQ.getKth(2);
console.log("kth - ",k," is :",kthElement); //[ 10 , 100 ]
Returns the height of the heap.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const height = priorityQ.getHeight();
console.log("Height of Heap is :",height); // 2
Returns true if the node at the index is a leaf node else returns false.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const index = 4;
const isIndexLeafNode = priorityQ.isLeafNode(index);
console.log("Is ",index,"Th a leaf node ?",isIndexLeafNode); // true
Returns the list of leaf nodes.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const leafNodes = priorityQ.getLeafNodes();
console.log("Leaf Nodes are",leafNodes); // [ [ 60, 90 ], [ 40, 20 ], [ 20, 80 ], [ 50, 40 ] ]
Prints the current state of the queue.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
priorityQ.print();
/*
[
[ 70, 200 ],
[ 10, 100 ],
[ 60, 90 ],
[ 40, 20 ],
[ 20, 80 ],
[ 50, 40 ]
]
*/
Prints the current heap in the sorted order ( Descending if config is set as Max-Heap else Ascending in case of Min-Heap)
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
priorityQ.printSortedHeap();
/*
[
[ 70, 200 ],
[ 10, 100 ],
[ 60, 90 ],
[ 20, 80 ],
[ 50, 40 ],
[ 40, 20 ]
]
*/
Implementation of stack is pretty straightforward with a few additional features like creating a stack with unique elements and removing duplicates in a stack.
import { Stack } from 'structures-wiz';
//Default contructor will create a Stack with empty elements and "isUnique" flag false
const stack = new Stack();
//Passing custom values and setting the second parameter makes the Stack only accept unique Elements
const uniqueStack = new Stack([20, 90],true);
Following are the methods exposed for usage:
import { Stack } from 'structures-wiz';
const stack = new Stack([20, 80, 10, 90]);
const size = stack.getSize();
console.log("Size of the stack is:",size); // 4
Pushes the received value at the end of the stack.
import { Stack } from 'structures-wiz';
const stack = new Stack();
stack.push(100);
stack.push(100);
stack.push(400);
stack.push(600);
stack.push(100);
stack.push(800);
stack.print() // [ 100, 100, 400, 600, 100, 800 ]
Pushes all the received values at the end of the stack.
import { Stack } from 'structures-wiz';
//Config 1 - Pass set of values
const stack = new Stack();
stack.pushAll( 100, 100, 400, 600, 100, 800 );
stack.print() // [ 100, 100, 400, 600, 100, 800 ]
//Config 2 - Pass Array of values
const stack = new Stack();
stack.pushAll([100, 100, 400, 600, 100, 800]);
stack.print() // [ 100, 100, 400, 600, 100, 800 ]
Returns the top most value.
import { Stack } from 'structures-wiz';
const stack = new Stack();
stack.pushAll( 100, 100, 400, 600, 100, 800 );
const peekValue = stack.peek();
console.log("Peek Value is :",peekValue);//800
Removes and returns the top most element of the stack.
import { Stack } from 'structures-wiz';
const stack = new Stack();
stack.pushAll( 100, 100, 400, 600, 100, 800 );
stack.print(); //[ 100, 100, 400, 600, 100, 800 ]
const poppedValue = stack.pop();
console.log("Popped Value is :",poppedValue);//800
stack.print();//[ 100, 100, 400, 600, 100 ]
Removes the duplicates elements of the stack.
import { Stack } from 'structures-wiz';
const stack = new Stack();
stack.pushAll( 100, 100, 400, 600, 100, 800 );
stack.print(); //[ 100, 100, 400, 600, 100, 800 ]
stack.removeDuplicates();
stack.print();//[ 100, 400, 600, 800 ]
Removes all the elements of the stack.
import { Stack } from 'structures-wiz';
const stack = new Stack();
stack.pushAll( 100, 100, 400, 600, 100, 800 );
stack.print(); //[ 100, 100, 400, 600, 100, 800 ]
stack.clear();
stack.print();//[ ]
Prints the current stack state.
import { Stack } from 'structures-wiz';
const stack = new Stack();
stack.pushAll( 100, 100, 400, 600, 100, 800 );
stack.print(); //[ 100, 100, 400, 600, 100, 800 ]
Singly linked lists contain nodes which have a data field as well as 'next' field, which points to the next node in line of nodes. Operations that can be performed on singly linked lists include insertion, deletion and traversal. (Wikipedia)
import { SinglyLinkedList } from 'structures-wiz';
// Default Constructor which creates a singly linkedlist.
const list = new SinglyLinkedList();
Following are the methods exposed for usage.
import { SinglyLinkedList } from 'structures-wiz';
var list = new SinglyLinkedList();
list.addFirst(20);
list.addFirst(10);
lis.addLast(30);
const size = list.getSize();
console.log("size: " + size); // 3
import { SinglyLinkedList } from 'structures-wiz';
var list = new SinglyLinkedList();
list.addFirst(30);
list.addFirst(20);
list.addFirst(10);
list.print(); // [ 10->20->30 ]
import { SinglyLinkedList } from 'structures-wiz';
var list = new SinglyLinkedList();
list.addLast(30);
list.addLast(20);
list.addLast(10);
list.print(); // [ 30->20->10 ]
import { SinglyLinkedList } from 'structures-wiz';
var list = new SinglyLinkedList();
list.addFirst(10);
list.addFirst(20);
list.print(); // [ 20->10 ]
list.addLast(30);
list.print(); // [ 20->10->30 ]
list.addLast(70);
list.addFirst(40);
list.print(); // [ 40->20->10->30->70 ]
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.