Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JS implementation of Stacks,Queue,Heaps,BST & LRUCache #643

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions Binary_Search_Tree/BinarySearchTree.js
Original file line number Diff line number Diff line change
@@ -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(value<root.value)
{
root.left=recursiveDeletion(root.left,value);
}
else if(value>root.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(value<currentNode.value)
currentNode=currentNode.left;
else if(value>currentNode.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]
157 changes: 157 additions & 0 deletions Heaps/Heaps.js
Original file line number Diff line number Diff line change
@@ -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.heapArr[index] ||
this.rightChild(index)<this.heapArr[index])
{
let small=this.leftChildIndex(index);
if(this.rightChild(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.swap(this.parentIndex(index),index);
index=this.parentIndex(index);
}
}

MaxHeap.prototype.bubbleDown=function()
{
let index=0;
while(this.leftChild(index) &&
this.leftChild(index)>this.heapArr[index] ||
this.rightChild(index)>this.heapArr[index])
{
let big=this.leftChildIndex(index);
if(this.rightChild(index) && this.leftChild(index)<this.rightChild(index))
big=this.rightChildIndex(index);
this.swap(index,big)
index=big;
}
}

MaxHeap.prototype.add=function(item)
{
if(this.heapArr.indexOf(item)!=-1)
return;
this.heapArr[this.heapArr.length]=item;
this.bubbleUp();
}

MaxHeap.prototype.poll=function()
{
let element=this.heapArr[0];
this.heapArr[0]=this.heapArr.pop();
console.log(element);
this.bubbleDown();
}

let maxHeap=new MaxHeap();
[1,2,3,4,4,5,3,6,7].map((v,i)=>maxHeap.add(v));
maxHeap.poll(); //7
maxHeap.poll(); //6
maxHeap.poll(); //5

Loading