β‘Tubelight.js
A pure javascript library providing most common data structures implementation.
Table of Contents
π Quick Start
π Installation
Install Tubelight from npm
npm install tubelight
Stack
A stack is a linear data structure that follows the principle of Last In First Out (LIFO).
const Tubelight = require("tubelight");
const stack = new Tubelight.Stack();
βοΈ Operations that can be performed on Stack :
π Push : Add an element to the top of a stack
/**
* Push element on the top of the stack
* @param {Object} element
*/
stack.push(element);
π Pop : Remove an element from the top of a stack
/**
* Remove the topmost element from the stack
* @return {Object} element
*/
stack.pop();
π Peek: Get the value of the top element without removing it
/**
* return the element at the top of stack without removing it
* @return {Object} element
*/
stack.peek();
π IsEmpty : Check if the stack is empty
/**
* True if the stack is empty otherwise return false
* @return {Boolean}
*/
stack.isEmpty();
β³ Time Complexity :
π Push, pop, IsEmpty & peek Operations take O(1) time.
π¦ Space Complexity :
π Stack requires O(n) Space Complexity where n is no. of elements in stack.
Queue
Queue follows the First In First Out (FIFO) rule - the item that goes in first is the item that comes out first.
const Tubelight = require("tubelight");
const queue = new Tubelight.Queue();
βοΈ Operations that can be performed on Queue :
π Enqueue: Add an element to the end of the queue
/**
* add element to the end of the queue
* @param {Object} element
*/
queue.enqueue(element);
π Dequeue: Remove an element from the front of the queue
/**
* remove element from the front of the queue
* @return {Object} element
*/
queue.dequeue(element);
π front: Get the value of the front of the queue without removing it
/**
* return front element from the queue with removing it
* @return {Object} element
*/
queue.front();
π IsEmpty: Check if the queue is empty
/**
* return true if queue is empty otherwise false
* @return {Boolean}
*/
queue.isEmpty();
β³ Time Complexity :
π Enqueue, dequeue, peek, isEmpty take O(1) times.
π¦ Space Complexity :
π Queue requires O(n) space complexity where n is no. of elements in queue.
Priority Queue
A priority queue is a special type of queue in which each element is associated with a priority value and elements are served based on their priority.
const Tubelight = require("tubelight");
const comparator = (a, b) => {
// priority of a will be higher than b;
return a > b;
};
/**
* This function will be used to decide the priority between two object
* @param {function} comparator
*/
const priorityQueue = new Tubelight.PriorityQueue(comparator);
βοΈ Operations that can be performed on Priority-Queue :
π Push : Insert an element in priority-queue
/**
* insert element in the priority-queue
* @param {Object} element
*/
priorityQueue.push(element);
π Pop : Remove the topmost priority element.
/**
* remove and return the topmost priority element
* @return {Object} element
*/
priorityQueue.pop();
π Top : Get the topmost priority element without removing it.
/**
* return the topmost priority element
* @return {Object} element
*/
priorityQueue.top();
π isEmpty : Check if the priority-queue is empty
/**
* return true if priority-queue is empty otherwise false
* @return {Boolean}
*/
priorityQueue.pop();
β³ Time Complexity :
π push and pop take O(log(n)) time.
π top and isEmpty take O(1) time.
π¦ Space Complexity :
π Priority-Queue requires O(n) space.
Disjoint Set
Disjoint set union provides near-constant-time operations to add new sets, to merge existing sets, and to determine whether elements are in the same set.
const Tubelight = require("tubelight");
const disjointSet = new Tubelight.DisjointSet();
βοΈ Operations that can be performed on Disjoint-Set :
π Union : Combine two subsets into one.
/**
* Union(x, y) combines the set containing element x and set containing element y
* @param {Object} x
* @param {Object} y
*/
disjointSet.union(x, y);
π sameSet : Check if two elements belong to the same subset or not.
/**
* check if the set containing element x and set containing element y are same or not
* @param {Object} x
* @param {Object} y
* @returns {Boolean} true if x and y are in same set otherwise false
*/
disjointSet.sameSet(x, y);
β³ Time Complexity :
π union and sameSet operations acheive almost constant time complexity.Although, the final amortized time complexity is calculated to be O(Ξ±(n)).
π¦ Space Complexity :
π Disjoint-Set requires O(n) space.