Construct a Binary Search Tree and implement that implements insert, search, traverse and delete methods.
We are going to implement - search(), insert(), traverse(), and delete() methods that have their respective functionalities.
class BST {
constructor(value) {
this.left = null;
this.value = value;
this.right = null;
}
}
The BST class has a constructor which has left and right pointers to denote left and right children nodes. They're initialized to null.
It also has a value property that contains the actual value in the node.
less than the current node and right children will be greater than the current node - We first essentially search for the right place.less than the current node we are standing at. If so, we get into the left subtree by writing currentNode = currentNode.left and loop again.NO LEFT CHILD - meaning we have reached the end point - we insert the element there - since insertion can only take place at leaf nodes.greater than the current node we are standing at. If so, we get into the left subtree by writing currentNode = currentNode.right and loop again.NO RIGHT CHILD - meaning we have reached the end point - we insert the element there - since insertion can only take place at leaf nodes.Pretty much the same implementation like insert. In insert method, we first search for the right place and then insert. Here, we are going to simply search based on the properties of a BST.
let currentNode = this;
while (currentNode !== null) {
if (value < currentNode.value) {
currentNode = currentNode.left;
} else if (value > currentNode.value) {
currentNode = currentNode.right;
} else {
return currentNode;
}
}
return null;
value === targetNode - search successful - return true.targetNode < value - Traverse the left subtreetargetNode > value - Traverse the right subtreefalse because the element will not be present in the BST. let currentNode = this;
if (currentNode === null) {
return;
}
console.log(currentNode.value);
currentNode.left && currentNode.left.traverse();
currentNode.right && currentNode.right.traverse();
Preorder traversal follows the concept of root->left->right. Which means that the value of the root will be printed first, then we will traverse the left subtree and then we will traverse the right subtree.
currentNode is null, return because you've reached the end of the tree.currentNodeFor deletion, we have various cases to deal with.
leaf nodeleft subtreeright subtreeleft and right children.Let's look at the algorithm:
currentNode = this.curentNode is NOT equal to null, we check for the values and traverse to either left or right subtree. That is, we are essentially searching for the element to be deleted first.parentNode in order to come back at it later since our base case will be null check, we need to make sure we know where we came from.if (currentNode.left !== null && currentNode.right !== null) {
currentNode.value = currentNode.right.getMinValue();
currentNode.right.delete(currentNode.value, currentNode);
}
if (currentNode.left !== null) {
currentNode.value = currentNode.left.value;
currentNode.right = currentNode.left.right;
currentNode.left = currentNode.left.left;
} else if (currentNode.right !== null) {
currentNode.value = currentNode.right.value;
currentNode.left = currentNode.right.left;
currentNode.right = currentNode.right.right;
}