Deleting desired node from a Binary Search Tree in JavaScrip

JavascriptWeb DevelopmentFront End Technology

Problem

Suppose, we have the following code that creates a Binary Search Tree DS and provides us with the functionality to insert node −

class Node{
   constructor(data) {
      this.data = data;
      this.left = null;
      this.right = null;
   };
};
class BinarySearchTree{
   constructor(){
      // root of a binary seach tree
      this.root = null;
   }
   insert(data){
      var newNode = new Node(data);
      if(this.root === null){
         this.root = newNode;
      }else{
         this.insertNode(this.root, newNode);
      };
   };
   insertNode(node, newNode){
      if(newNode.data < node.data){
         if(node.left === null){
            node.left = newNode;
         }else{
            this.insertNode(node.left, newNode);
         };
      } else {
         if(node.right === null){
            node.right = newNode;
         }else{
            this.insertNode(node.right,newNode);
         };
      };
   };
};
const BST = new BinarySearchTree();
BST.insert(5);
BST.insert(3);
BST.insert(6);
BST.insert(2);
BST.insert(4);
BST.insert(7);

After the execution of this very code, our BST will look something like this −

5
/ \
3 6
/ \ \
2 4 7

We are required to write yet another function deleteNode() that takes in the root of any BST as the first argument and a numerical value as the second argument.

And if the value specified by the second argument exists in the Tree, our function should delete that node which holds the value otherwise our function should do nothing. In both the cases, our function should return the updated root of the BST.

Example

The code for this will be −

class Node{
   constructor(data) {
      this.data = data;
      this.left = null;
      this.right = null;
   };
};
class BinarySearchTree{
   constructor(){
      // root of a binary seach tree
      this.root = null;
   }
   insert(data){
      var newNode = new Node(data);
      if(this.root === null){
         this.root = newNode;
      }else{
         this.insertNode(this.root, newNode);
      };
   };
   insertNode(node, newNode){
      if(newNode.data < node.data){
         if(node.left === null){
            node.left = newNode;
         }else{
            this.insertNode(node.left, newNode);
         };
      } else {
         if(node.right === null){
            node.right = newNode;
         }else{
            this.insertNode(node.right,newNode);
         };
      };
   };
};
const BST = new BinarySearchTree();
BST.insert(5);
BST.insert(3);
BST.insert(6);
BST.insert(2);
BST.insert(4);
BST.insert(7);
const printTree = (node) => {
   if(node !== null) {
      printTree(node.left);
      console.log(node.data);
      printTree(node.right);
   };
};
const deleteNode = function(root, key) {
   if(!root){
      return null;
   };
   if(root.data > key){
      if(!root.left){
         return root;
      }else{
         root.left = deleteNode(root.left, key);
      };
   } else if(root.data < key){
      if(!root.right) return root;
      else root.right = deleteNode(root.right, key);
   } else {
      if(!root.left || !root.right){
         return root.left || root.right;
      } else {
         let nd = new TreeNode();
         let right = root.right;
         nd.left = root.left;
         while(right.left){
            right = right.left;
         }
         nd.data = right.data;
         nd.right = deleteNode(root.right, right.data);
         return nd;
      }
   }
   return root;
};
console.log('Before Deleting any node');
printTree(BST.root);
console.log('After deleting node with data 4');
printTree(deleteNode(BST.root, 4));

Code Explanation:

There are in total three conditions we need to think about once we find the target node.

  • a leaf (no left, no right);

  • has left, no right; no left, has right;

  • has both left and right.

1 and 2 are easy, we just need to return null or whatever we have (left or right);

And for the last condition, we need to know that after we delete the target node, what's going to replace it. If we simply drag its left or right up, the BST will be invailid. So we have to find either the smallest from the right subtree or the biggest from the left subtree.

Output

And the output in the console will be −

Before Deleting any node
2
3
4
5
6
7
After deleting node with data 4
2
3
5
6
7
raja
Published on 18-Mar-2021 08:29:37
Advertisements