Skip to content

Instantly share code, notes, and snippets.

@PulkitAgg
Created May 22, 2019 14:24
Show Gist options
  • Save PulkitAgg/a1163912dc93216ed013a14f32f5a0c9 to your computer and use it in GitHub Desktop.
Save PulkitAgg/a1163912dc93216ed013a14f32f5a0c9 to your computer and use it in GitHub Desktop.
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
this.root = null;
}
/**
*
* @param {*} data data to be added in the tree.
*/
insert(data) {
let newNode = new Node(data);
if (this.root) {
this.insertAtNode(this.root, newNode);
} else {
// add new node at root if not defined already.
this.root = newNode;
}
}
/**
*
* @param {*} node node at which newNode will be added.
* @param {*} newNode new node that we have to add.
*/
insertAtNode(node, newNode) {
// check the node
if (newNode.data < node.data) {
// enter the node in left part.
if (node.left) {
this.insertAtNode(node.left, newNode)
} else {
node.left = newNode;
}
} else {
// enter the node in the right part
if (node.right) {
this.insertAtNode(node.right, newNode)
} else {
node.right = newNode;
}
}
}
/**
* function for the getting the root of the tree.
*/
getRoot() {
return this.root;
}
/**
*
* @param {*} node root of the tree.
*/
inOrderTraversal(node) {
if(node) {
this.inOrderTraversal(node.left);
console.log(node.data);
this.inOrderTraversal(node.right);
}
}
/**
*
* @param {*} node root of the tree.
*/
preOrderTraversal(node) {
if(node) {
console.log(node.data);
this.preOrderTraversal(node.left);
this.preOrderTraversal(node.right);
}
}
/**
*
* @param {*} node root of the tree.
*/
postOrderTraversal(node) {
if(node) {
this.preOrderTraversal(node.left);
this.preOrderTraversal(node.right);
console.log(node.data);
}
}
/**
*
* @param {*} node root of the tree.
* @param {*} data data to be searched in tree.
*/
search(node, data) {
if(node) {
if(data < node.data) {
// Search in the left tree.
return this.search(node.left, data);
} else if(data > node.data) {
// Search in the right tree.
return this.search(node.right, data);
} else {
return node;
}
} else {
return null;
}
}
/**
* find the minimum node nin the tree
* @param {*} node search start from the node.
*/
findMinNode(node) {
if(node) {
if(node.left) {
return this.findMinNode(node.left);
} else {
return node;
}
} else {
return null;
}
}
/**
*
* @param {*} data removing the node with this data.
*/
remove(data) {
// update the root of the tree.
this.root = this.removeNodeInSubtree(this.root, data);
}
/**
*
* @param {*} node node from which sub tree we want to delete given data.
* @param {*} data data of the node that we have to delete.
*/
removeNodeInSubtree(node, data) {
if(node) {
// check the data of the node.
if(data < node.data) {
// update the node left pointer and delete the node from it.
node.left = this.removeNodeInSubtree(node.left, data);
return node;
} else if (data > node.data){
// update the node right pointer and delete the node from it.
node.right = this.removeNodeInSubtree(node.right, data);
return node;
} else {
// node value is same as given data.
// check the left and right sub tree
if(node.left == null && node.right == null) {
// just delete the node.
node = null;
return node;
} else if(node.left == null) {
//update the node with right sub tree.
node = node.right;
return node;
} else if(node.right == null) {
// update the tree with left sub tree.
node = node.left;
return node;
} else {
// node has both child.
// find the minimum node from the right sub tree and update the node with that node.
var temp = this.findMinNode(node.right, data);
node.data = temp.data;
// remove temp node from the tree.
node.right = this.removeNodeInSubtree(node.right, temp.data);
return node;
}
}
} else {
// node is null
return null;
}
}
}
// Create binary search tree.
let bst = new BinarySearchTree();
// Add data to it.
bst.insert(15);
bst.insert(25);
bst.insert(10);
bst.insert(7);
bst.insert(22);
bst.insert(17);
bst.insert(13);
bst.insert(5);
bst.insert(9);
bst.insert(27);
// print the tree.
bst.inOrderTraversal(bst.getRoot());
console.log('Search 10- ', bst.search(bst.getRoot(), 10));
console.log('Node less than 25- ', bst.findMinNode(bst.search(bst.getRoot(), 25)));
console.log('delete node 25', bst.remove(25));
bst.inOrderTraversal(bst.getRoot());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment