Data Structures and Algorithms For Web
Data Structures and Algorithms are highly useful for development of complex features in any Application. Here in this project we are targeting to implement most of the important and useful Data Structures and Algorithms for web Developers.
List of Implemented Data Structures
- AVL Tree
- Binary Search Tree
- Doubly Linked List
- Set (Ordered)
-
Priority Queue
- Min Heap
- Max Heap
- Queue
- Deque
- Stack
Installation
npm i @anorcle/dsa
Import
ECMAScript Modules
Import Everything
import * as dsa from '@anorcle/dsa';
Import Only Required Modules
import { AVL, BST, Deque, LinkedList, PriorityQueue, Queue, Set, Stack } from '@anorcle/dsa';
CommonJS
Import Everything
const dsa = require('@anorcle/dsa')
Import Only Required Modules
const { AVL, BST, Deque, LinkedList, PriorityQueue, Queue, Set, Stack } = require('@anorcle/dsa')
Examples
Max Heap
A Max Heap is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node.
TypeScript
import { PriorityQueue } from "@anorcle/dsa";
// compare function for Max Heap
const compare = (a: number, b: number): -1 | 0 | 1 => {
if (a < b)
return -1;
if (a > b)
return +1;
return 0;
};
// create new Priority Queue of Numbers
const pq = new PriorityQueue<number>(compare);
pq.push(3);
pq.push(1);
pq.push(100);
console.log(pq.front) // 100
// remove the top element from priority queue and return it
pq.pop()
JavaScript
import { PriorityQueue } from "@anorcle/dsa";
// compare function for Max Heap
const compare = (a, b) => {
if (a < b)
return -1;
if (a > b)
return +1;
return 0;
};
// create new Priority Queue of Numbers
const pq = new PriorityQueue(compare);
pq.push(3);
pq.push(1);
pq.push(100);
console.log(pq.front); // 100
// remove the top element from priority queue and return it
pq.pop();
Min Heap
A Min Heap is a complete binary tree in which the value in each internal node is smaller than or equal to the values in the children of that node.
TypeScript
import { PriorityQueue } from "@anorcle/dsa";
// compare function for Min Heap
const compare = (a: number, b: number): -1 | 0 | 1 => {
if (a < b)
return +1;
if (a > b)
return -1;
return 0;
};
// create new Priority Queue of Numbers
const pq = new PriorityQueue<number>(compare);
pq.push(3);
pq.push(1);
pq.push(100);
console.log(pq.front) // 1
// remove the top element from priority queue and return it
pq.pop()
JavaScript
import { PriorityQueue } from "@anorcle/dsa";
// compare function for Min Heap
const compare = (a, b) => {
if (a < b)
return +1;
if (a > b)
return -1;
return 0;
};
// create new Priority Queue of Numbers
const pq = new PriorityQueue(compare);
pq.push(3);
pq.push(1);
pq.push(100);
console.log(pq.front); // 1
// remove the top element from priority queue and return it
pq.pop();
Stack
Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
import { Stack } from "@anorcle/dsa";
const stack = new Stack<number>();
stack.push(3);
stack.push(1);
stack.push(100);
console.log(stack.top) // 100
// Remove Top Element from stack
stack.pop()
console.log(stack.top) // 1
Queue
A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO)
import { Queue } from "@anorcle/dsa";
const queue = new Queue<number>();
queue.push(3);
queue.push(1);
queue.push(100);
console.log(queue.front) // 3
// Remove First Element from Queue
queue.pop()
console.log(queue.front) // 1