Removing a node in a Javascript Tree

In JavaScript, removing a node from a tree is a fundamental operation that requires handling different scenarios based on the node's structure. This article covers the three main cases for node deletion in Binary Search Trees (BST).

Tree Structure and Traversal Overview

A tree is a nonlinear data structure consisting of nodes connected by edges. Tree traversal retrieves and processes data from each node in a specific order:

  • In-order traversal ? Follows the path (Left ? Root ? Right)

  • Pre-order traversal ? Follows the path (Root ? Left ? Right)

  • Post-order traversal ? Follows the path (Left ? Right ? Root)

Node Removal Scenarios

When removing a node from a BST, there are three possible cases to handle:

Case 1: Deleting a Leaf Node

If the node has no children, simply remove it from the tree.

50 30 70 Delete

Case 2: Deleting a Node with One Child

Replace the node with its child, maintaining the BST property.

50 30 70 20 Delete

Case 3: Deleting a Node with Two Children

Find the in-order successor (smallest value in right subtree), replace the node's value with it, then delete the successor.

50 30 70 60 80 Delete Successor

Implementation Example

Here's a complete implementation of BST node deletion with all three cases:

<!DOCTYPE html>
<html>
<head>
   <script>
      class Node {
         constructor(item) {
            this.key = item;
            this.left = this.right = null;
         }
      }

      let root = null;
      
      function deleteKey(key) {
         root = deleteRecursive(root, key);
      }
      
      function deleteRecursive(root, key) {
         if (root == null)
            return root;
         if (key < root.key)
            root.left = deleteRecursive(root.left, key);
         else if (key > root.key)
            root.right = deleteRecursive(root.right, key);
         else {
            // Case 1: No child or one child
            if (root.left == null)
               return root.right;
            else if (root.right == null)
               return root.left;
            
            // Case 2: Two children - find inorder successor
            root.key = minimumValue(root.right);
            root.right = deleteRecursive(root.right, root.key);
         }
         return root;
      }
      
      function minimumValue(root) {
         let minValue = root.key;
         while (root.left != null) {
            minValue = root.left.key;
            root = root.left;
         }
         return minValue;
      }
      
      function insert(key) {
         root = insertRec(root, key);
      }
      
      function insertRec(root, key) {
         if (root == null) {
            root = new Node(key);
            return root;
         }
         if (key < root.key)
            root.left = insertRec(root.left, key);
         else if (key > root.key)
            root.right = insertRec(root.right, key);
         return root;
      }
      
      function inorder() {
         InorderRecursion(root);
      }
      
      function InorderRecursion(root) {
         if (root != null) {
            InorderRecursion(root.left);
            document.write(root.key + " ");
            InorderRecursion(root.right);
         }
      }
      
      // Build and test the tree
      insert(90);
      insert(40);
      insert(10);
      insert(50);
      insert(100);
      insert(70);
      insert(60);
      insert(80);
      insert(120);
      
      document.write("After inserting nodes, In-order traversal: <br>");
      inorder();
      
      document.write("<br><br>Delete node 10 (leaf node)<br>");
      deleteKey(10);
      document.write("After deletion: ");
      inorder();
      
      document.write("<br><br>Delete node 40 (one child)<br>");
      deleteKey(40);
      document.write("After deletion: ");
      inorder();
      
      document.write("<br><br>Delete node 90 (two children)<br>");
      deleteKey(90);
      document.write("Final result: ");
      inorder();
   </script>
</head>
<body>
</body>
</html>
After inserting nodes, In-order traversal:
10 40 50 60 70 80 90 100 120

Delete node 10 (leaf node)
After deletion: 40 50 60 70 80 90 100 120

Delete node 40 (one child)
After deletion: 50 60 70 80 90 100 120

Delete node 90 (two children)
Final result: 50 60 70 80 100 120

Algorithm Summary

Case Condition Action
Leaf Node No children Simply remove the node
One Child Either left or right child Replace node with its child
Two Children Both left and right children Replace with inorder successor, then delete successor

Conclusion

Node deletion in BSTs requires careful handling of three scenarios to maintain tree structure and BST properties. The inorder successor approach for two-child nodes ensures the BST ordering remains intact after deletion.

Updated on: 2026-03-15T23:18:59+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements