Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

BST Delete Operation in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST Delete Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting a leaf node in BST
What is the printed state of the BST after deleting the leaf node with value 20?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

class BST {
  constructor() {
    this.root = null;
  }

  insert(data) {
    const newNode = new Node(data);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (data < current.data) {
        if (!current.left) {
          current.left = newNode;
          break;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = newNode;
          break;
        }
        current = current.right;
      }
    }
  }

  delete(data) {
    this.root = this._deleteRec(this.root, data);
  }

  _deleteRec(root, data) {
    if (!root) return root;
    if (data < root.data) {
      root.left = this._deleteRec(root.left, data);
    } else if (data > root.data) {
      root.right = this._deleteRec(root.right, data);
    } else {
      if (!root.left && !root.right) {
        return null;
      } else if (!root.left) {
        return root.right;
      } else if (!root.right) {
        return root.left;
      } else {
        let minLargerNode = this._minValueNode(root.right);
        root.data = minLargerNode.data;
        root.right = this._deleteRec(root.right, minLargerNode.data);
      }
    }
    return root;
  }

  _minValueNode(node) {
    let current = node;
    while (current.left) {
      current = current.left;
    }
    return current;
  }

  inorder(root = this.root, result = []) {
    if (root) {
      this.inorder(root.left, result);
      result.push(root.data);
      this.inorder(root.right, result);
    }
    return result;
  }

  print() {
    const values = this.inorder();
    console.log(values.join(' -> ') + ' -> null');
  }
}

const bst = new BST();
[50, 30, 70, 20, 40, 60, 80].forEach(n => bst.insert(n));
bst.delete(20);
bst.print();
A30 -> 40 -> 50 -> 60 -> 70 -> 80 -> null
B20 -> 30 -> 40 -> 50 -> 60 -> 70 -> 80 -> null
C50 -> 30 -> 40 -> 60 -> 70 -> 80 -> null
D30 -> 40 -> 50 -> 60 -> 70 -> null
Attempts:
2 left
💡 Hint
Deleting a leaf node removes it without affecting other nodes.
❓ Predict Output
intermediate
2:00remaining
Output after deleting a node with one child in BST
What is the printed state of the BST after deleting the node with value 30, which has one child?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

class BST {
  constructor() {
    this.root = null;
  }

  insert(data) {
    const newNode = new Node(data);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (data < current.data) {
        if (!current.left) {
          current.left = newNode;
          break;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = newNode;
          break;
        }
        current = current.right;
      }
    }
  }

  delete(data) {
    this.root = this._deleteRec(this.root, data);
  }

  _deleteRec(root, data) {
    if (!root) return root;
    if (data < root.data) {
      root.left = this._deleteRec(root.left, data);
    } else if (data > root.data) {
      root.right = this._deleteRec(root.right, data);
    } else {
      if (!root.left && !root.right) {
        return null;
      } else if (!root.left) {
        return root.right;
      } else if (!root.right) {
        return root.left;
      } else {
        let minLargerNode = this._minValueNode(root.right);
        root.data = minLargerNode.data;
        root.right = this._deleteRec(root.right, minLargerNode.data);
      }
    }
    return root;
  }

  _minValueNode(node) {
    let current = node;
    while (current.left) {
      current = current.left;
    }
    return current;
  }

  inorder(root = this.root, result = []) {
    if (root) {
      this.inorder(root.left, result);
      result.push(root.data);
      this.inorder(root.right, result);
    }
    return result;
  }

  print() {
    const values = this.inorder();
    console.log(values.join(' -> ') + ' -> null');
  }
}

const bst = new BST();
[50, 30, 70, 40, 60, 80].forEach(n => bst.insert(n));
bst.delete(30);
bst.print();
A30 -> 40 -> 50 -> 60 -> 70 -> 80 -> null
B40 -> 50 -> 60 -> 70 -> 80 -> null
C50 -> 40 -> 60 -> 70 -> 80 -> null
D40 -> 30 -> 50 -> 60 -> 70 -> 80 -> null
Attempts:
2 left
💡 Hint
When deleting a node with one child, the child replaces the node.
❓ Predict Output
advanced
2:00remaining
Output after deleting a node with two children in BST
What is the printed state of the BST after deleting the node with value 50, which has two children?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

class BST {
  constructor() {
    this.root = null;
  }

  insert(data) {
    const newNode = new Node(data);
    if (!this.root) {
      this.root = newNode;
      return;
    }
    let current = this.root;
    while (true) {
      if (data < current.data) {
        if (!current.left) {
          current.left = newNode;
          break;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = newNode;
          break;
        }
        current = current.right;
      }
    }
  }

  delete(data) {
    this.root = this._deleteRec(this.root, data);
  }

  _deleteRec(root, data) {
    if (!root) return root;
    if (data < root.data) {
      root.left = this._deleteRec(root.left, data);
    } else if (data > root.data) {
      root.right = this._deleteRec(root.right, data);
    } else {
      if (!root.left && !root.right) {
        return null;
      } else if (!root.left) {
        return root.right;
      } else if (!root.right) {
        return root.left;
      } else {
        let minLargerNode = this._minValueNode(root.right);
        root.data = minLargerNode.data;
        root.right = this._deleteRec(root.right, minLargerNode.data);
      }
    }
    return root;
  }

  _minValueNode(node) {
    let current = node;
    while (current.left) {
      current = current.left;
    }
    return current;
  }

  inorder(root = this.root, result = []) {
    if (root) {
      this.inorder(root.left, result);
      result.push(root.data);
      this.inorder(root.right, result);
    }
    return result;
  }

  print() {
    const values = this.inorder();
    console.log(values.join(' -> ') + ' -> null');
  }
}

const bst = new BST();
[50, 30, 70, 20, 40, 60, 80].forEach(n => bst.insert(n));
bst.delete(50);
bst.print();
A20 -> 30 -> 40 -> 60 -> 70 -> 80 -> null
B20 -> 30 -> 40 -> 50 -> 60 -> 70 -> 80 -> null
C20 -> 30 -> 40 -> 60 -> 70 -> 80 -> 50 -> null
D30 -> 40 -> 60 -> 70 -> 80 -> null
Attempts:
2 left
💡 Hint
When deleting a node with two children, replace it with the smallest node in the right subtree.
🧠 Conceptual
advanced
1:30remaining
Understanding BST delete operation cases
Which of the following correctly describes the three main cases when deleting a node from a BST?
ANode is left child; node is right child; node is root
BNode is root; node is leaf; node is internal
CNode has no children; node has left child only; node has right child only
DNode is a leaf; node has one child; node has two children
Attempts:
2 left
💡 Hint
Think about how many children the node to delete can have.
🔧 Debug
expert
2:30remaining
Identify the bug in BST delete operation code
Given the following snippet from a BST delete function, what is the bug causing incorrect deletion when the node has two children? ```js _deleteRec(root, data) { if (!root) return root; if (data < root.data) { root.left = this._deleteRec(root.left, data); } else if (data > root.data) { root.right = this._deleteRec(root.right, data); } else { if (!root.left && !root.right) { return null; } else if (!root.left) { return root.right; } else if (!root.right) { return root.left; } else { let minLargerNode = this._minValueNode(root.right); root.data = minLargerNode.data; root.left = this._deleteRec(root.right, minLargerNode.data); } } return root; } ``` What is the bug?
AThe base case should return null instead of root
BThe function does not handle leaf nodes correctly
CThe recursive call after replacing data should update root.right, not root.left
DThe minimum value node is found incorrectly from the left subtree
Attempts:
2 left
💡 Hint
Check which subtree is updated after replacing the node's data.

Practice

(1/5)
1. What happens when you delete a leaf node (a node with no children) from a Binary Search Tree (BST)?
easy
A. The node is simply removed without affecting other nodes.
B. The entire tree is rebuilt from scratch.
C. The node's children replace it in the tree.
D. The root node is always changed.

Solution

  1. Step 1: Understand leaf node deletion

    A leaf node has no children, so removing it does not affect other nodes.
  2. Step 2: Effect on BST structure

    Since no children exist, simply remove the node and update its parent's pointer to null.
  3. Final Answer:

    The node is simply removed without affecting other nodes. -> Option A
  4. Quick Check:

    Leaf node deletion = simple removal [OK]
Hint: Leaf nodes delete by direct removal only [OK]
Common Mistakes:
  • Thinking children replace the node when none exist
  • Assuming root always changes
  • Believing tree rebuild is needed
2. Which of the following is the correct syntax to delete a node with value key from a BST in JavaScript?
easy
A. bst.deleteNode(key);
B. bst.delete(key);
C. bst.remove(key);
D. bst.erase(key);

Solution

  1. Step 1: Identify common BST delete method

    In many JavaScript BST implementations, the method to delete a node is named delete.
  2. Step 2: Check method naming conventions

    Methods like remove, erase, or deleteNode are not standard or common in JavaScript BST examples.
  3. Final Answer:

    bst.delete(key); -> Option B
  4. Quick Check:

    Standard BST delete method = delete() [OK]
Hint: Use bst.delete(key) to remove nodes [OK]
Common Mistakes:
  • Using remove() or erase() which are not standard
  • Confusing delete keyword with method name
  • Using deleteNode() which is uncommon
3. Consider this BST and the delete operation:
    10
   /  \
  5    15
       / \
      12  20
What will be the in-order traversal output after deleting node with value 15?
medium
A. [5, 10, 12, 15, 20]
B. [5, 10, 20]
C. [5, 10, 12, 20]
D. [5, 10, 15, 20]

Solution

  1. Step 1: Identify node to delete and its children

    Node 15 has two children: 12 (left) and 20 (right).
  2. Step 2: Replace node 15 with its in-order successor

    The in-order successor is the smallest node in right subtree, which is 20. Replace 15 with 20 and delete original 20 node.
  3. Step 3: Perform in-order traversal

    In-order traversal visits nodes in ascending order: 5, 10, 12, 20.
  4. Final Answer:

    [5, 10, 12, 20] -> Option C
  5. Quick Check:

    In-order after deleting 15 = [5, 10, 12, 20] [OK]
Hint: Replace deleted node with in-order successor [OK]
Common Mistakes:
  • Removing 15 without replacement
  • Using in-order predecessor instead of successor
  • Forgetting to delete the successor node
4. The following code attempts to delete a node from a BST but causes an error. What is the main issue?
function deleteNode(root, key) {
  if (!root) return null;
  if (key < root.val) root.left = deleteNode(root.left, key);
  else if (key > root.val) root.right = deleteNode(root.right, key);
  else {
    if (!root.left) return root.right;
    else if (!root.right) return root.left;
    let minNode = findMin(root.right);
    root.val = minNode.val;
    // Missing deletion of minNode here
  }
  return root;
}

function findMin(node) {
  while (node.left) node = node.left;
  return node;
}
medium
A. The function does not handle leaf nodes correctly.
B. The base case for recursion is missing.
C. The function uses incorrect comparison operators.
D. The code does not delete the in-order successor node after copying its value.

Solution

  1. Step 1: Analyze deletion of node with two children

    When deleting a node with two children, we replace its value with the in-order successor's value.
  2. Step 2: Check if in-order successor node is deleted

    The code copies the successor's value but does not delete the successor node itself, causing duplicate values and incorrect tree.
  3. Final Answer:

    The code does not delete the in-order successor node after copying its value. -> Option D
  4. Quick Check:

    Must delete successor node after value copy [OK]
Hint: Always delete successor node after copying value [OK]
Common Mistakes:
  • Forgetting to delete the in-order successor node
  • Missing base case (though present here)
  • Incorrectly handling leaf nodes
  • Using wrong comparison operators
5. You have a BST with nodes: 8, 3, 10, 1, 6, 14, 4, 7, 13.
After deleting node 3, what is the correct in-order traversal of the BST?
hard
A. [1, 4, 6, 7, 8, 10, 13, 14]
B. [1, 3, 4, 6, 7, 8, 10, 13, 14]
C. [1, 6, 7, 8, 10, 13, 14]
D. [1, 4, 7, 8, 10, 13, 14]

Solution

  1. Step 1: Identify node 3 and its children

    Node 3 has two children: 1 (left) and 6 (right). 6 has children 4 and 7.
  2. Step 2: Delete node 3 by replacing with in-order successor

    The in-order successor of 3 is 4 (smallest in right subtree). Replace 3 with 4, then delete original 4 node.
  3. Step 3: Perform in-order traversal after deletion

    In-order traversal visits nodes in ascending order: 1, 4, 6, 7, 8, 10, 13, 14.
  4. Final Answer:

    [1, 4, 6, 7, 8, 10, 13, 14] -> Option A
  5. Quick Check:

    In-order after deleting 3 = [1, 4, 6, 7, 8, 10, 13, 14] [OK]
Hint: Replace deleted node with in-order successor, then traverse [OK]
Common Mistakes:
  • Forgetting to delete the in-order successor node
  • Using in-order predecessor instead
  • Skipping nodes in traversal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard