├── DataStructers ├── BinarySearchTree.js ├── DoublyLinkedList.js ├── HashTable.js ├── LinkedList.js ├── PriorityQueue.js ├── Queue.js ├── QueueLinked.js └── Stack.js ├── README.md ├── index.js └── package.json /DataStructers/BinarySearchTree.js: -------------------------------------------------------------------------------- 1 | var Node = function(data, left = null, right = null){ 2 | this.data = data; 3 | this.left = left; 4 | this.right = right; 5 | } 6 | 7 | var BinarySearchTree = function(){ 8 | this.root = null; 9 | } 10 | 11 | BinarySearchTree.prototype = { 12 | add: function(data){ 13 | var newNode = new Node(data); 14 | if(this.root === null){ 15 | this.root = newNode; 16 | }else{ 17 | addNode(this.root, newNode); 18 | } 19 | }, 20 | 21 | remove: function(index){ 22 | this.root = removeNode(this.root, index); 23 | }, 24 | 25 | findMin: function(){ 26 | let current = this.root; 27 | while(current.left !== null){ 28 | current = current.left; 29 | } 30 | return current.data; 31 | }, 32 | 33 | findMax: function(){ 34 | let current = this.root; 35 | while(current.right!== null){ 36 | current = current.right; 37 | } 38 | return current.data; 39 | }, 40 | 41 | getIndex: function(index){ 42 | let current = this.root; 43 | while(current.data !== index){ 44 | if(data < current.data){ 45 | current = current.left; 46 | }else{ 47 | current = current.right; 48 | } 49 | if(current === null){ 50 | return null; 51 | } 52 | } 53 | return current.data; 54 | }, 55 | 56 | findHeight: function(node = this.root){ 57 | if(node == null){ 58 | return -1; 59 | } 60 | let left = this.FindHeight(node.left); 61 | let right = this.FindHeight(node.right); 62 | if(left > right){ 63 | return left + 1; 64 | }else{ 65 | return right + 1; 66 | } 67 | }, 68 | 69 | traverse: function(){ 70 | if(this.root === null){ 71 | return null; 72 | }else{ 73 | var result = new Array(); 74 | var InOrder = function(node){ 75 | node.left && InOrder(node.left); 76 | result.push(node.data); 77 | node.right && InOrder(node.right); 78 | } 79 | InOrder(this.root, result); 80 | return result; 81 | } 82 | } 83 | } 84 | 85 | const addNode = function(node, newNode) { 86 | if(newNode.data < node.data) { 87 | if(node.left === null){ 88 | node.left = newNode; 89 | }else{ 90 | addNode(node.left, newNode); 91 | } 92 | }else{ 93 | if(node.right === null){ 94 | node.right = newNode; 95 | }else{ 96 | addNode(node.right,newNode); 97 | } 98 | } 99 | } 100 | 101 | 102 | const removeNode = function(node, data){ 103 | if(node == null){ 104 | return null; 105 | } 106 | 107 | if(data === node.data){ 108 | if(node.left == null && node.right == null){ 109 | return null; 110 | } 111 | if(node.left == null){ 112 | return node.right; 113 | } 114 | if(node.right == null){ 115 | return node.left; 116 | } 117 | var tempNode = node.right; 118 | while(tempNode.left !== null){ 119 | tempNode = tempNode.left; 120 | } 121 | node.data = tempNode.data; 122 | node.right = removeNode(node.right, data); 123 | return node; 124 | }else if(data < node.data){ 125 | node.left = removeNode(node.left, data); 126 | return node; 127 | }else{ 128 | node.right = removeNode(node.right, data); 129 | return node; 130 | } 131 | } 132 | 133 | module.exports = { 134 | BinarySearchTree: BinarySearchTree 135 | } 136 | 137 | /* 138 | Function Cheat Sheet: 139 | BinarySearchTree.Add(item); //Adds item to BinarySearchTree 140 | BinarySearchTree.Remove(index); //Removes item from BinarySearchTree 141 | BinarySearchTree.GetIndex(index); //Returns item at index 142 | BinarySearchTree.FindMin(); //Returns smallest item 143 | BinarySearchTree.FindMax(); //Returns largest item 144 | BinarySearchTree.FindHieght(); //Returns the hieght of BinarySearchTree 145 | BinarySearchTree.Traverse(); //Console logs all items in order 146 | */ 147 | -------------------------------------------------------------------------------- /DataStructers/DoublyLinkedList.js: -------------------------------------------------------------------------------- 1 | var Node = function(data){ 2 | this.data = data; 3 | this.next = null; 4 | this.previous = null; 5 | } 6 | 7 | var doublyLinkedList = function(){ 8 | this.head = null; 9 | this.tail = null; 10 | } 11 | 12 | doublyLinkedList.prototype = { 13 | insert: function(data){ 14 | const newNode = new Node(data); 15 | 16 | if (this.head === null) { 17 | this.head = newNode; 18 | this.tail = node; 19 | } else { 20 | this.tail.next = newNode; 21 | newNode.previous = this.tail; 22 | } 23 | 24 | this.tail = newNode; 25 | }, 26 | 27 | insertIndex(data, index) { 28 | if(index === undefined){ 29 | return console.log("Could not insertIndex to DoublyLinkedList no index") 30 | } 31 | let current = this.head; 32 | let counter = 1; 33 | let newNode = new Node(data); 34 | if(index == 0){ 35 | this.head.previous = newNode; 36 | newNode.next = this.head; 37 | this.head = newNode; 38 | }else{ 39 | while(current){ 40 | current = current.next; 41 | if(counter == index){ 42 | newNode.previous = current.prev; 43 | current.previous.next = newNode; 44 | newNode.next = current; 45 | current.previous = newNode; 46 | } 47 | counter++; 48 | } 49 | } 50 | }, 51 | 52 | getIndex: function(index){ 53 | if (index > -1) { 54 | let current = this[head]; 55 | let i = 0; 56 | 57 | while ((current !== null) && (i < index)) { 58 | current = current.next; 59 | i++; 60 | } 61 | return current !== null ? current.data : undefined; 62 | } else { 63 | return undefined; 64 | } 65 | }, 66 | 67 | removeIndex: function(index){ 68 | let current = this.head; 69 | let counter = 1; 70 | if(index == 0){ 71 | this.head = this.head.next; 72 | this.head.previous = null; 73 | }else{ 74 | while( current ) { 75 | current = current.next 76 | if (current == this.tail){ 77 | this.tail = this.tail.previous; 78 | this.tail.next = null; 79 | }else if(counter == index){ 80 | current.previous.next = current.next; 81 | current.next.previous = current.previous; 82 | break; 83 | } 84 | counter++; 85 | } 86 | } 87 | }, 88 | 89 | printReverse: function(){ 90 | let current = this.tail; 91 | 92 | while(current !== null){ 93 | console.log(current.data); 94 | current = current.previous; 95 | } 96 | }, 97 | 98 | print: function(){ 99 | let current = this.head; 100 | 101 | while(current){ 102 | console.log(current.data); 103 | current = current.next; 104 | } 105 | }, 106 | 107 | clearList: function(){ 108 | this.head = null; 109 | this.tail = null; 110 | }, 111 | 112 | removeFirst: function(){ 113 | if(!this.head){ 114 | return; 115 | } 116 | this.head = this.head.next; 117 | return this.head; 118 | } 119 | } 120 | 121 | module.exports = { 122 | doublyLinkedList: doublyLinkedList 123 | } 124 | 125 | /* 126 | Function Cheat Sheet: 127 | DoublyLinkedList.insertFirst(item); //Adds item at the start of the DoublyLinkedList 128 | DoublyLinkedList.insertIndex(item); //Adds item at the end of the DoublyLinkedList 129 | DoublyLinkedList.getIndex(index); //Returns value at the index of the DoublyLinkedLst 130 | DoublyLinkedList.removeIndex(index); //Removes the item at the indexs of the DoublyLinkedList 131 | DoublyLinkedList.clearAll(); // Removes all the items in the DoublyLinkedList 132 | DoublyLinkedList.print(); //Console logs all items of the DoublyLinkedList 133 | DoublyLinkedList.printReverse(); //Console logs all items of the DoublyLinkedList in reverse 134 | */ 135 | -------------------------------------------------------------------------------- /DataStructers/HashTable.js: -------------------------------------------------------------------------------- 1 | var HashTable = function(){ 2 | this.storage = []; 3 | this.buckets = 10; 4 | } 5 | 6 | HashTable.prototype = { 7 | add: function(key, value){ 8 | if(value === undefined){ 9 | return console.log("Could not add to HashTable no value"); 10 | } 11 | var index = hash(key, this.buckets); 12 | if(this.storage[index] === undefined){ 13 | this.storage[index] = [ 14 | [key, value] 15 | ]; 16 | }else{ 17 | var inserted = false; 18 | for (var i = 0; i < this.storage[index].length; i++) { 19 | if(this.storage[index][i][0] === key){ 20 | this.storage[index][i][1] = value; 21 | inserted = true; 22 | } 23 | } 24 | if(inserted === false){ 25 | this.storage[index].push([key, value]); 26 | } 27 | } 28 | }, 29 | 30 | remove: function(key){ 31 | var index = hash(key, this.buckets); 32 | if(this.storage[index].length === 1 && this.storage[index][0][0] === key){ 33 | delete storage 34 | }else{ 35 | for(var i =0; 0 < this.storage[index]; i++){ 36 | if(this.storage[index][i][0] === key){ 37 | delete this.storage[index[i]]; 38 | } 39 | } 40 | } 41 | }, 42 | 43 | getIndex: function(key){ 44 | var index = hash(key, this.buckets); 45 | if(this.storage[index] === undefined){ 46 | return undefined; 47 | }else{ 48 | for(var i = 0; i < this.storage[index].length; i++){ 49 | if(this.storage[index][i][0] === key){ 50 | console.log(this.storage[index][i][1]); 51 | return this.storage[index][i][1]; 52 | } 53 | } 54 | } 55 | }, 56 | 57 | print: function(){ 58 | return console.log(this.storage); 59 | } 60 | } 61 | 62 | module.exports = { 63 | HashTable: HashTable 64 | } 65 | 66 | var hash = function(string, max){ 67 | var hash = 0; 68 | for(var i = 0; i < string.length; i++) hash += string.charCodeAt(i); 69 | return hash % max; 70 | } 71 | 72 | /* 73 | Function Cheat Sheet: 74 | Hash.add(name, item); //Adds item into the HashTable 75 | Hash.remove(name); //Removes item from the HashTable 76 | Hash.print(); //console logs all items in the HashTable 77 | Hash.getIndex(); //Returns value at the index of the HashTable 78 | */ 79 | -------------------------------------------------------------------------------- /DataStructers/LinkedList.js: -------------------------------------------------------------------------------- 1 | var Node = function(data, next = null){ 2 | this.data = data; 3 | this.next = next; 4 | } 5 | 6 | var linkedList = function(){ 7 | this.head = null; 8 | this.size = 0; 9 | } 10 | 11 | linkedList.prototype = { 12 | insertFirst: function(data){ 13 | this.head = new Node(data, this.head); 14 | this.size++; 15 | }, 16 | 17 | insertLast: function(data){ 18 | let node = new Node(data); 19 | if(!this.head) return this.head = node; 20 | 21 | let current = this.head; 22 | while(current.next) current = current.next; 23 | current.next = node; 24 | 25 | this.size++; 26 | }, 27 | 28 | insertIndex: function(data, index){ 29 | if(index === undefined){ 30 | return console.log("Could not insertIndex to LinkedList no index"); 31 | } 32 | 33 | if(index < 0 || index > this.size){ 34 | console.log("invaild index"); 35 | return undefined; 36 | } 37 | 38 | let node = new Node(data); 39 | let current 40 | let pervious; 41 | 42 | if(index === 0){ 43 | this.head = new Node(data, this.head); 44 | return; 45 | } 46 | 47 | current = this.head; 48 | let count = 0; 49 | 50 | while(count < index){ 51 | pervious = current; 52 | count++; 53 | current = current.next; 54 | } 55 | 56 | node.next = current; 57 | pervious.next = node; 58 | this.size++; 59 | }, 60 | 61 | getIndex: function(index){ 62 | let current = this.head; 63 | let count = 0; 64 | 65 | while(current){ 66 | if(count === index){ 67 | console.log(current.data); 68 | return current.data; 69 | } 70 | count++; 71 | current = current.next; 72 | } 73 | return null; 74 | }, 75 | 76 | removeFirst: function(){ 77 | if(!this.head){ 78 | return; 79 | } 80 | this.head = this.head.next; 81 | return this.head; 82 | }, 83 | 84 | removeIndex: function(index){ 85 | if(index > 0 && index > this.size){ 86 | console.log("invaild index"); 87 | return; 88 | } 89 | let current = this.head; 90 | let pervious; 91 | let count = 0; 92 | 93 | if(index === 0){ 94 | this.head = current.next; 95 | return; 96 | } 97 | while(count < index){ 98 | pervious = current; 99 | count++; 100 | current = current.next; 101 | } 102 | pervious.next = current.next; 103 | this.size--; 104 | }, 105 | 106 | print: function(){ 107 | let current = this.head; 108 | 109 | while(current){ 110 | console.log(current.data); 111 | current = current.next; 112 | } 113 | }, 114 | 115 | clearList: function(){ 116 | this.head = null; 117 | this.size = 0; 118 | } 119 | } 120 | module.exports = { 121 | linkedList: linkedList 122 | } 123 | 124 | /* 125 | Function Cheat Sheet: 126 | LinkedList.insertFirst(item); //Adds item at the start of the LinkedList 127 | LinkedList.insertLast(item); //Adds item at the end of the LinkedList 128 | LinkedList.insertIndex(item, index); /Adds item at the index of the LinkedList 129 | LinkedList.getIndex(index); //Returns value at the index of the LinkedLst 130 | LinkedList.removeFirst(); //Removes the first item of the LinkedList 131 | LinkedList.removeIndex(index); //Removes the item at the indexs of the LinkedList 132 | LinkedList.clearAll(); // Removes all the items in the LinkedList 133 | LinkedList.print(); //Console logs all items of the LinkedList 134 | */ 135 | -------------------------------------------------------------------------------- /DataStructers/PriorityQueue.js: -------------------------------------------------------------------------------- 1 | var Node = function(element, priority){ 2 | this.element = element; 3 | this.priority = priority; 4 | } 5 | 6 | var PriorityQueue = function(){ 7 | this.collection = []; 8 | } 9 | 10 | PriorityQueue.prototype = { 11 | enqueue: function(element, priority){ 12 | var newNode = new Node(element, priority); 13 | var contain = false; 14 | 15 | for (var i = 0; i < this.collection.length; i++) { 16 | if (this.collection[i].priority > newNode.priority) { 17 | this.collection.splice(i, 0, newNode); 18 | contain = true; 19 | break; 20 | } 21 | } 22 | 23 | if (!contain) this.collection.push(newNode); 24 | }, 25 | 26 | dequeue: function(){ 27 | if(this.collection.length === 0) return console.log("Queue is empty"); 28 | return this.collection.shift(); 29 | }, 30 | 31 | peek: function(){ 32 | if(this.collection.length === 0) return console.log("Queue is empty"); 33 | console.log(this.collection[0]); 34 | return this.collection[0]; 35 | }, 36 | 37 | size: function(){ 38 | return this.collection.length; 39 | }, 40 | 41 | print: function(){ 42 | console.log(this.collection); 43 | } 44 | } 45 | 46 | module.exports = { 47 | PriorityQueue: PriorityQueue 48 | } 49 | 50 | 51 | /* 52 | Function Cheat Sheet: 53 | PriorityQueue.enqueue(item); //Adds item to queue 54 | PriorityQueue.dequeue(); //Removes item from queue 55 | PriorityQueue.print(); //Console logs all items of the Queue 56 | PriorityQueue.size(); //Returns Queues size 57 | PriorityQueue.peek(); //Returns first item in Queue 58 | */ 59 | -------------------------------------------------------------------------------- /DataStructers/Queue.js: -------------------------------------------------------------------------------- 1 | var Queue = function(){ 2 | this.collection = []; 3 | } 4 | 5 | Queue.prototype = { 6 | enqueue: function(element){ 7 | this.collection.push(element); 8 | }, 9 | 10 | print: function(){ 11 | return console.log(this.collection); 12 | }, 13 | 14 | dequeue: function(){ 15 | if(this.collection.length === 0) return console.log("Queue is empty"); 16 | this.collection.shift(); 17 | return collection[0]; 18 | }, 19 | 20 | peek: function(){ 21 | if(this.collection.length === 0) return console.log("Queue is empty"); 22 | return this.collection[0]; 23 | }, 24 | 25 | size: function(){ 26 | console.log(this.collection.length); 27 | return this.collection.length; 28 | } 29 | } 30 | module.exports = { 31 | Queue: Queue 32 | } 33 | 34 | /* 35 | Function Cheat Sheet: 36 | Queue.enqueue(item); //Adds item to queue 37 | Queue.dequeue(); //Removes item from queue 38 | Queue.print(); //Console logs all items of the Queue 39 | Queue.size(); //Returns Queues size 40 | Queue.peek(); //Returns first item in Queue 41 | */ 42 | -------------------------------------------------------------------------------- /DataStructers/QueueLinked.js: -------------------------------------------------------------------------------- 1 | let ll = require("./LinkedList"); 2 | let Ll = new ll.linkedList(); 3 | 4 | var LinkedQueue = function(){ 5 | this.collection = new Ll(); 6 | } 7 | 8 | LinkedQueue.prototype = { 9 | enqueue: function(element){ 10 | collection.insertLast(element); 11 | }, 12 | 13 | print: function(){ 14 | collection.print(); 15 | }, 16 | 17 | dequeue: function(){ 18 | if(collection.length === 0) return console.log("Queue is empty"); 19 | collection.removeFirst(); 20 | return collection[0]; 21 | }, 22 | 23 | peek: function(){ 24 | if(collection.length === 0) return console.log("Queue is empty"); 25 | return collection[0]; 26 | }, 27 | 28 | size: function(){ 29 | if(collection.length === 0) return console.log("Queue is empty"); 30 | return collection.length; 31 | } 32 | } 33 | 34 | module.exports = { 35 | LinkedQueue: LinkedQueue 36 | } 37 | 38 | /* 39 | Function Cheat Sheet: 40 | Queue.enqueue(item); //Adds item to queue 41 | Queue.dequeue(); //Removes item from queue 42 | Queue.print(); //console logs all items of the Queue 43 | Queue.size(); //Returns Queues size 44 | Queue.peek(); //Returns first item in Queue 45 | */ 46 | -------------------------------------------------------------------------------- /DataStructers/Stack.js: -------------------------------------------------------------------------------- 1 | var Stack = function(){ 2 | this.count = 0; 3 | this.storage = []; 4 | } 5 | 6 | Stack.prototype = { 7 | push: function(value){ 8 | this.storage.push(value) 9 | this.count++; 10 | }, 11 | 12 | pop: function(){ 13 | if(this.count === 0) return undefined; 14 | this.storage.length -= 1; 15 | this.count--; 16 | }, 17 | 18 | peek: function(){ 19 | if(count === 0) return undefined; 20 | return this.storage[this.count-1] 21 | }, 22 | 23 | size: function(){ 24 | return this.count; 25 | }, 26 | 27 | print: function(){ 28 | return console.log(this.storage); 29 | } 30 | } 31 | 32 | module.exports = { 33 | Stack: Stack 34 | } 35 | 36 | /* 37 | Function Cheat Sheet: 38 | Stack.push(item); //Adds item to the top of the Stack 39 | Stack.pop(); //Removes item from the top of the Stack 40 | Stack.print(); //console logs all items in the Stack 41 | Stack.size(); //Returns Stacks size 42 | Stack.peek(); //Returns first item in Stack 43 | */ 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data-Structures 2 | 3 |

4 | 5 | Logo 6 | 7 | 8 | ## 📌 Introduction 9 | 10 | Data Structures is an NPM Package built for Developers to get a High-Level API Access to various Data Structures. This is my first NPM Package and this tool was developed for practising various Data Structures and was later wrapped as a Package and published on NPM. 11 | 12 | The Package is available on [NPM](https://www.npmjs.com/package/data_structure_models). 13 | 14 | ## 💥 Installation 15 | 16 | The Package can be installed by running the follow command on your Command Prompt/Shell: 17 | 18 | ``` 19 | $ npm install data_structure_models 20 | ``` 21 | 22 | After installation of the the Package, you can run the following script to validate if the Package is running fit and fine: 23 | ``` 24 | var dataStructureModels = require("data_structure_models"); 25 | var datastructures= new dataStructureModels(); 26 | var stack=datastructures.Stack(); 27 | stack.push(5); 28 | stack.push(3); 29 | stack.print(); 30 | ``` 31 | 32 | This will print the Stack on the Console and you are free to experiment with the wide number of Data Structures available with the Package. 33 | 34 | ## How to use the Package? 35 | 36 | This Package has a lot of Data Structures which you can try out. Some of the Data Structures and their particular implementations have been listed below. We are taking the example of Binary Search Tree here. Binary search tree is a data structure that quickly allows us to maintain a sorted list of numbers. 37 | 38 | To access Binary Search Trees, try out the following script: 39 | ``` 40 | var dataStructureModels = require("data_structure_models"); 41 | var datastructures= new dataStructureModels(); 42 | var bst=datastructures.BinarySerachTree(); 43 | bst.add(5); 44 | bst.add(3); 45 | bst.add(8); 46 | console.log(bst.findMax()) 47 | // Output: 8 48 | ``` 49 | Besides these, other Data Structures that can be called and access using the package are: 50 | - Hash Table 51 | - Linked List 52 | - Priority Queue 53 | - Queue 54 | - Stack 55 | - Doubly Linked List 56 | - Queue using Linked List 57 | 58 | ## 📜 LICENSE 59 | 60 | [ISC](https://opensource.org/licenses/ISC) 61 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function(){ 2 | let bst = require("./DataStructers/BinarySearchTrees"); 3 | let dll = require("./DataStructers/DoublyLinkedList"); 4 | let ll = require("./DataStructers/LinkedList"); 5 | let q = require("./DataStructers/Queue"); 6 | let ql = require("./DataStructers/QueueLinked"); 7 | let pq = require("./DataStructers/PriorityQueue"); 8 | let s = require("./DataStructers/Stack"); 9 | let hs = require("./DataStructers/HashTable"); 10 | 11 | this.DoublyLinkedList = function(){ 12 | return new dll.doublyLinkedList(); 13 | } 14 | 15 | this.LinkedList = function(){ 16 | return new ll.linkedList(); 17 | } 18 | 19 | this.Queue = function(){ 20 | return new q.Queue(); 21 | } 22 | 23 | this.QueueLinked = function(){ 24 | return new ql.LinkedQueue(); 25 | } 26 | 27 | this.PriortyQueue = function(){ 28 | return new pq.PriorityQueue(); 29 | } 30 | 31 | this.Stack = function(){ 32 | return new s.Stack(); 33 | } 34 | 35 | this.BinarySerachTree = function(){ 36 | return new bst.BinarySearchTree(); 37 | } 38 | 39 | this.HashTable = function(){ 40 | return new hs.HashTable(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "data_structure_models", 3 | "version": "1.0.0", 4 | "description": "A NPM Package which provides access to various Data Structures in Javascript eliminating the purpose of coding them from scratch.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "data-structures", 11 | "algorithms", 12 | "npm", 13 | "first-package", 14 | "structures-data" 15 | ], 16 | "author": "Harsh Bardhan Mishra", 17 | "license": "ISC" 18 | } 19 | --------------------------------------------------------------------------------