datacorejs

Description
Datacorejs is a JavaScript library that provides a collection of commonly used data structures to simplify your development tasks. Whether you're working on algorithms, data manipulation, or need efficient storage solutions, this package has you covered. It's designed to be easy to use, well-documented, and highly performant.
Table of Contents
Install
npm install --save datacorejs
import
import {
LinkedList,
DoublyLinkedList,
Stack,
Queue,
BinarySearchTree,
Graph,
} from "datacorejs";
API
constructor
const linkedList = new LinkedList();
const doublyLinkedList = new DoublyLinkedList();
const stack = new Stack();
const queue = new Queue();
const bst = new BinarySearchTree();
const graph = new Graph();
LinkedList
const linkedList = new LinkedList();
console.log("Is the list empty?", linkedList.isEmpty());
linkedList.push(10);
linkedList.push(20);
linkedList.push(30);
console.log("Size of the list:", linkedList.size());
console.log("Linked list as an array:", linkedList.toArray());
console.log("Head of the list:", linkedList.getHead());
linkedList.insertAt(1, 15);
console.log("Linked list as an array:", linkedList.toArray());
console.log("Node at index 2:", linkedList.get(2));
linkedList.set(0, 5);
console.log("Linked list as an array:", linkedList.toArray());
const removedNode = linkedList.removeAt(2);
console.log("Linked list as an array:", linkedList.toArray());
console.log("Removed node:", removedNode);
linkedList.reverse();
console.log("Reversed linked list:", linkedList.toArray());
linkedList.clear();
console.log("Is the list empty after clearing?", linkedList.isEmpty());
DoublyLinkedList
const doublyLinkedList = new DoublyLinkedList();
console.log("Is the list empty?", doublyLinkedList.isEmpty());
doublyLinkedList.push(10);
doublyLinkedList.push(20);
doublyLinkedList.push(30);
console.log("Size of the list:", doublyLinkedList.size());
console.log("Doubly linked list as an array:", doublyLinkedList.toArray());
console.log("Head of the list:", doublyLinkedList.getHead());
doublyLinkedList.insertAt(1, 15);
console.log("Doubly linked list as an array:", doublyLinkedList.toArray());
console.log("Node at index 2:", doublyLinkedList.get(2));
doublyLinkedList.set(0, 5);
console.log("Doubly linked list as an array:", doublyLinkedList.toArray());
const removedNode = doublyLinkedList.removeAt(2);
console.log("Doubly linked list as an array:", doublyLinkedList.toArray());
console.log("Removed node:", removedNode);
doublyLinkedList.reverse();
console.log("Reversed doubly linked list:", doublyLinkedList.toArray());
doublyLinkedList.clear();
console.log("Is the list empty after clearing?", doublyLinkedList.isEmpty());
Stack
const stack = new Stack();
console.log("Is the stack empty?", stack.isEmpty());
stack.push(10);
stack.push(20);
stack.push(30);
console.log("Size of the stack:", stack.size());
console.log("Top of the stack:", stack.peek());
console.log("Stack as an array:", stack.toArray());
const poppedValue1 = stack.pop();
const poppedValue2 = stack.pop();
console.log("Size of the stack after popping:", stack.size());
console.log("Popped value 1:", poppedValue1);
console.log("Popped value 2:", poppedValue2);
stack.clear();
console.log("Is the stack empty after clearing?", stack.isEmpty());
Queue
const queue = new Queue();
console.log("Is the queue empty?", queue.isEmpty());
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
console.log("Size of the queue:", queue.size());
console.log("Front of the queue:", queue.peek());
console.log("Queue as an array:", queue.toArray());
const dequeuedValue1 = queue.dequeue();
const dequeuedValue2 = queue.dequeue();
console.log("Size of the queue after dequeuing:", queue.size());
console.log("Dequeued value 1:", dequeuedValue1);
console.log("Dequeued value 2:", dequeuedValue2);
queue.clear();
console.log("Is the queue empty after clearing?", queue.isEmpty());
BinarySearchTree
const bst = new BinarySearchTree();
console.log("Is the BST empty?", bst.isEmpty());
bst.insert(50);
bst.insert(30);
bst.insert(70);
bst.insert(20);
bst.insert(40);
bst.insert(60);
bst.insert(80);
console.log("Is the BST empty after inserting?", bst.isEmpty());
const root = bst.getRoot();
console.log("Root node value:", root.value);
console.log("Does BST contain 30?", bst.contains(30));
console.log("Does BST contain 90?", bst.contains(90));
console.log("BFS traversal:", bst.bfs());
console.log("DFS (Pre-order) traversal:", bst.dfsPreOrder());
console.log("DFS (Post-order) traversal:", bst.dfsPostOrder());
console.log("DFS (In-order) traversal:", bst.dfsInOrder());
console.log("Minimum value in BST:", bst.min());
console.log("Maximum value in BST:", bst.max());
bst.delete(30);
console.log("Deleted 30 from BST. In-order traversal:", bst.dfsInOrder());
console.log("Height of BST:", bst.height(root));
console.log("Nodes at level 2:");
bst.printLevel(root, 2);
console.log("Is the BST a valid BST?", bst.isBST(root, -Infinity, Infinity));
Graph
const graph = new Graph();
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
graph.addEdge("A", "B");
graph.addEdge("A", "C");
graph.addEdge("B", "D");
graph.addEdge("C", "E");
console.log("Graph Adjacency List:");
graph.printGraph();
console.log("Does graph have edge A to B?", graph.hasEdge("A", "B"));
console.log("Does graph have edge B to C?", graph.hasEdge("B", "C"));
console.log("Does graph have vertex A?", graph.hasVertex("A"));
console.log("Does graph have vertex F?", graph.hasVertex("F"));
graph.removeEdge("A", "B");
console.log("Graph Adjacency List after removing edge A to B:");
graph.printGraph();
graph.removeVertex("C");
console.log("Graph Adjacency List after removing vertex C:");
graph.printGraph();
console.log("DFS starting from vertex A:", graph.dfs("A"));
console.log("BFS starting from vertex B:", graph.bfs("B"));
License
MIT License
Author
Sandip Deb