diff --git a/Binary_Search_Tree/BinarySearchTree.js b/Binary_Search_Tree/BinarySearchTree.js new file mode 100644 index 00000000..f7f415c6 --- /dev/null +++ b/Binary_Search_Tree/BinarySearchTree.js @@ -0,0 +1,189 @@ + //Binary Search Tree + +function BSTNode(value) +{ + this.value=value; + this.left=null; + this.right=null; +} + +function BinarySearchTree() +{ + this._root=null; +} + +BinarySearchTree.prototype.insert=function(value) +{ + let node=new BSTNode(value); + if(!this._root) + this._root=node; + else + { + let currentNode=this._root; + while(true) + { + if(currentNode.value>node.value) + { + if(!currentNode.left) + { + currentNode.left=node; + break; + } + currentNode=currentNode.left; + } + else if(node.value>currentNode.value) + { + if(!currentNode.right) + { + currentNode.right=node; + break; + } + currentNode=currentNode.right; + } + else + break; + } + } +} + +BinarySearchTree.prototype.deletion=function(value) +{ + function findRightTreeMin(root) + { + while(root.left) + root=root.left; //loops till leftmost node is reached + return root; + } + + function recursiveDeletion(root,value) + { + + if(!root) + return null; + else if(valueroot.value) + { + root.right=recursiveDeletion(root.right,value); + } + else + { + if(!root.left && !root.right) + return null; + else if(!root.left) + return root.right; + else if(!root.right) + return root.left; + else + { + let temp=findRightTreeMin(root.right); + root.value=temp.value; + root.right=recursiveDeletion(root.right,temp.value); + } + } + return root; + } + recursiveDeletion(this._root,value); +} + +BinarySearchTree.prototype.search=function(value) +{ + let currentNode=this._root; + while(currentNode) + { + if(valuecurrentNode.value) + currentNode=currentNode.right; + else + return (console.log("Found")); + } + console.log("Not Found"); +} + +let BST=new BinarySearchTree(); +BST.insert(10); +BST.insert(4); +BST.insert(21); +BST.insert(5); +BST.insert(50); +BST.insert(3); +BST.insert(11); +BST.insert(40); +BST.insert(70); +BST.deletion(21); +BST.search(21); //Not Found +BST.search(4); //Found +console.log(BST); + + //In-order Traversal +BinarySearchTree.prototype.inOrder=function() +{ + function traverser(node) + { + if(!node) + return; + traverser(node.left); + console.log(node.value); + traverser(node.right); + } + traverser(this._root); +} + //Pre-Order Traversal +BinarySearchTree.prototype.preOrder=function() +{ + function preOrderTraversal(node) + { + if(!node) + return; + console.log(node.value); + preOrderTraversal(node.left); + preOrderTraversal(node.right); + } + preOrderTraversal(this._root); +} + + //Post-order traversal + +BinarySearchTree.prototype.postOrder=function() +{ + function postOrderTraversal(node) + { + if(!node) + return; + postOrderTraversal(node.right); + postOrderTraversal(node.left); + console.log(node.value); + } + postOrderTraversal(this._root); +} + //Level-order traversal +BinarySearchTree.prototype.levelOrder=function() +{ + function levelOrderTraversal(node) + { + let Q=[]; + Q.push(node); + while(Q.length) + { + node=Q.shift(); + console.log(node.value); + if(node.left) + Q.push(node.left) + if(node.right) + Q.push(node.right); + } + } + levelOrderTraversal(this._root); +} + +let traversalBST=new BinarySearchTree(); +let nodeValues=[20,14,4,17,29,24,31,7,3]; +nodeValues.map((val,i)=>traversalBST.insert(val)); + +traversalBST.preOrder(); // [20,14,4,3,7,17,29,24,31] +traversalBST.inOrder(); // [3,4,7,14,17,20,24,29,31] +traversalBST.postOrder(); // [3,7,4,17,14,24,31,29,20] +traversalBST.levelOrder(); // [20,14,29,4,17,24,31,3,7] diff --git a/Heaps/Heaps.js b/Heaps/Heaps.js new file mode 100644 index 00000000..0ca88113 --- /dev/null +++ b/Heaps/Heaps.js @@ -0,0 +1,157 @@ + //Heaps + +function Heap() +{ + let heapArr=[]; +} + +Heap.prototype.swap=function(index1,index2) +{ + let temp=this.heapArr[index1]; + this.heapArr[index1]=this.heapArr[index2]; + this.heapArr[index2]=temp; +} + +Heap.prototype.parentIndex=function(N) +{ + return Math.floor((N-1)/2); +} + +Heap.prototype.parent=function(N) +{ + return this.heapArr[this.parentIndex(N)]; +} + +Heap.prototype.leftChildIndex=function(N) +{ + return (N*2)+1; +} + +Heap.prototype.leftChild=function(N) +{ + return this.heapArr[this.leftChildIndex(N)]; +} + +Heap.prototype.rightChildIndex=function(N) +{ + return (N*2)+2; +} + +Heap.prototype.rightChild=function(N) +{ + return this.heapArr[this.rightChildIndex(N)]; +} + + //Min-Heap +function MinHeap() +{ + this.heapArr=[]; +} + +MinHeap.prototype=Object.create(Heap.prototype); + +MinHeap.prototype.bubbleUp=function() +{ + let index=this.heapArr.length-1; + while(this.parent(index) && this.parent(index)>this.heapArr[index]) + { + this.swap(this.parentIndex(index),index); + index=this.parentIndex(index); + } +} + +MinHeap.prototype.bubbleDown=function() +{ + let index=0; + while(this.leftChild(index) && + this.leftChild(index)this.rightChild(index)) + small=this.rightChildIndex(index); + this.swap(index,small) + index=small; + } +} + +MinHeap.prototype.add=function(item) +{ + if(this.heapArr.indexOf(item)!=-1) + return; + this.heapArr[this.heapArr.length]=item; + this.bubbleUp(); +} + +MinHeap.prototype.poll=function() +{ + let element=this.heapArr[0]; + this.heapArr[0]=this.heapArr.pop(); + console.log(element); + this.bubbleDown(); +} + +let minHeap=new MinHeap(); +[1,1,2,5,4,3,6,8,7].map((v,i)=>minHeap.add(v)); +console.log(minHeap); +minHeap.poll(); //1 +minHeap.poll(); //2 +minHeap.poll(); //3 +minHeap.poll(); //4 + + //Max-Heap + +function MaxHeap() +{ + this.heapArr=[]; +} + +MaxHeap.prototype=Object.create(Heap.prototype); + +MaxHeap.prototype.bubbleUp=function() +{ + let index=this.heapArr.length-1; + while(this.parent(index) && this.parent(index)this.heapArr[index] || + this.rightChild(index)>this.heapArr[index]) + { + let big=this.leftChildIndex(index); + if(this.rightChild(index) && this.leftChild(index)maxHeap.add(v)); +maxHeap.poll(); //7 +maxHeap.poll(); //6 +maxHeap.poll(); //5 + diff --git a/LRUCache/LRUCache.js b/LRUCache/LRUCache.js new file mode 100644 index 00000000..82051314 --- /dev/null +++ b/LRUCache/LRUCache.js @@ -0,0 +1,73 @@ + //LRU +function LRUNode(key,value) +{ + this.key=key; + this.value=value; + this.prev=null; + this.next=null; +} +function LRUCache(capacity) +{ + this.keys={}; //A hash table + this.head=new LRUNode('',null); + this.tail=new LRUNode('',null); + this.capacity=capacity; + this.head.next=this.tail; + this.tail.prev=this.head; +} +LRUCache.prototype.addNode=function(node) +{ + let realTail=this.tail.prev; + node.prev=realTail; + realTail.next=node; + node.next=this.tail; + this.tail.prev=node; +} +LRUCache.prototype.delNode=function(node) +{ + let prevNode=node.prev; + let nextNode=node.next; + prevNode.next=nextNode; + nextNode.prev=prevNode; +} +LRUCache.prototype.set=function(key,value) +{ + let node=this.keys[key]; + + if(node) + this.delNode(node); + + node=new LRUNode(key,value); + this.keys[key]=node; + this.addNode(node); + if(this.capacityb +myLRU.set(3,3); //1<->b<->3 +myLRU.set(4,4); //1<->b<->3<->4 +myLRU.set(5,'e'); //1<->b<->3<->4<->e +myLRU.set(6,'f'); //b<->3<->4<->e<->f +myLRU.set(7,7); //3<->4<->5<->f<->7 +console.log(myLRU); +myLRU.get(4); //3<->5<->f<->7<->4 +myLRU.get(12); + diff --git a/Queue/Queue.js b/Queue/Queue.js new file mode 100644 index 00000000..524c54f1 --- /dev/null +++ b/Queue/Queue.js @@ -0,0 +1,56 @@ +//Queue + function Queue(arr_) + { + this.arr=[]; + if(arr_) + this.arr=arr_; + } + Queue.prototype.getBuffer=function() + { + return this.arr.slice(); + } + Queue.prototype.isEmpty=function() + { + return (Boolean(!(this.arr.length))); + } + Queue.prototype.enque=function(value) + { + this.arr.push(value); + } + Queue.prototype.deque=function() + { + return this.arr.shift(); + } + Queue.prototype.peek=function() + { + return this.arr[0]; + } + function queueNAccess(queue,n) + { + let bufferQueue=new Queue(queue.getBuffer()); + while(--n!=0) + bufferQueue.deque(); + return bufferQueue.deque(); + } + function queueSearch(queue,element) + { + let bufferQueue=new Queue(queue.getBuffer()); + while(!(bufferQueue.isEmpty())) + { + if(element==bufferQueue.deque()) + return true; + } + return false; + } + let Q=new Queue(); + Q.enque(1); // arr=[1] + Q.enque(2); // arr=[1,2] + Q.enque(3); // arr=[1,2,3] + Q.enque(4); // arr=[1,2,3,4] + Q.enque(5); // arr=[1,2,3,4,5] + Q.enque(6); // arr=[1,2,3,4,5,6] + Q.deque(); // arr=[2,3,4,5,6] + console.log(Q.isEmpty()); // returns false + console.log(Q.peek()); // returns '2' + console.log(queueSearch(Q,9)); // returns false + console.log(queueNAccess(Q,2)); // returns '3' diff --git a/Stack/Stacks.js b/Stack/Stacks.js new file mode 100644 index 00000000..05d15af6 --- /dev/null +++ b/Stack/Stacks.js @@ -0,0 +1,60 @@ +//Stacks + function Stack(arr_) + { + this.arr=[]; + if(arr_) + this.arr=arr_; + } + + Stack.prototype.isEmpty=function() + { + return(Boolean(!(this.arr.length))); + } + Stack.prototype.getBuffer=function() + { + return (this.arr.slice()); + } + + Stack.prototype.push=function(value) + { + this.arr.push(value); + } + Stack.prototype.peek=function() + { + return(console.log(this.arr[this.arr.length-1])); + } + + Stack.prototype.pop=function() + { + return(this.arr.pop()); + } + function Access(stack,n) + { + let bufferStack=new Stack(stack.getBuffer()); + while(--n!=0) + bufferStack.pop(); + return bufferStack.pop(); + } + function Search(stack,element) + { + let bufferStack=new Stack(stack.getBuffer()); + while(bufferStack.isEmpty()) + { + if(element==bufferStack.pop()) + { + return true; + } + } + return false; + } + let stk=new Stack(); + stk.push(5); // arr=[5] + stk.push(6); // arr=[5,6] + stk.pop(); // arr=[5] + stk.push(7); // arr=[5,7] + stk.push(8); // arr=[5,7,8] + stk.push(9); // arr=[5,7,8,9] + stk.push(10); // arr=[5,7,8,9,10] + console.log(stk.isEmpty()); // returns false + console.log(Access(stk,2)); // returns 9 + console.log(Search(stk,21)); // returns false