Bird
Raised Fist0
Data Structures Theoryknowledge~6 mins

Deletion in BST in Data Structures Theory - Full Explanation

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Imagine you have a sorted collection of items arranged in a tree shape, and you want to remove one item without messing up the order. Deletion in a Binary Search Tree (BST) solves this problem by carefully removing the item while keeping the tree organized.
Explanation
Deleting a Leaf Node
When the node to delete has no children, it is called a leaf node. Removing it is simple because it does not affect other nodes. You just remove the node and update its parent's pointer to null.
Deleting a leaf node is straightforward since it has no children to worry about.
Deleting a Node with One Child
If the node to delete has only one child, you remove the node and connect its parent directly to that child. This keeps the tree connected and maintains the BST order.
For nodes with one child, link the parent to the child to keep the tree intact.
Deleting a Node with Two Children
This is the trickiest case. You find the node's successor (the smallest node in its right subtree) or predecessor (the largest node in its left subtree). Replace the node's value with the successor's or predecessor's value, then delete that successor or predecessor node, which will have at most one child.
Replace the node with its successor or predecessor to maintain BST order before deleting.
Real World Analogy

Imagine a bookshelf arranged by book titles in alphabetical order. Removing a book with no neighbors is easy—just take it out. If a book has one neighbor, you slide the neighbor over to fill the gap. If a book is between two others, you replace it with the next book in order and then remove that next book from its original spot.

Deleting a Leaf Node → Taking out a book at the end of the shelf with no books next to it
Deleting a Node with One Child → Sliding a neighboring book over to fill the empty spot
Deleting a Node with Two Children → Replacing a book with the next book in alphabetical order, then removing that next book from its place
Diagram
Diagram
       ┌─────┐
       │ 50  │
       └──┬──┘
          │
    ┌─────┴─────┐
    │           │
 ┌──┴──┐     ┌──┴──┐
 │ 30  │     │ 70  │
 └──┬──┘     └──┬──┘
    │           │
  ┌─┴─┐       ┌─┴─┐
  │ 20 │       │ 80 │
  └────┘       └────┘

Deleting node 30 (with one child 20):

       ┌─────┐
       │ 50  │
       └──┬──┘
          │
    ┌─────┴─────┐
    │           │
  ┌─┴─┐       ┌─┴─┐
  │ 20 │       │ 70 │
  └────┘       └──┬──┘
                   │
                 ┌─┴─┐
                 │ 80 │
                 └────┘
This diagram shows a BST before and after deleting a node with one child, illustrating how the child replaces the deleted node.
Key Facts
Leaf Node DeletionRemoving a node with no children by simply disconnecting it from its parent.
Single Child DeletionRemoving a node with one child by linking its parent directly to that child.
Two Children DeletionReplacing the node with its in-order successor or predecessor before deleting.
In-order SuccessorThe smallest node in the right subtree of a node.
In-order PredecessorThe largest node in the left subtree of a node.
Code Example
Data Structures Theory
class Node:
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None

def min_value_node(node):
    current = node
    while current.left is not None:
        current = current.left
    return current

def delete_node(root, key):
    if root is None:
        return root
    if key < root.key:
        root.left = delete_node(root.left, key)
    elif key > root.key:
        root.right = delete_node(root.right, key)
    else:
        if root.left is None:
            return root.right
        elif root.right is None:
            return root.left
        temp = min_value_node(root.right)
        root.key = temp.key
        root.right = delete_node(root.right, temp.key)
    return root

def inorder(root):
    return inorder(root.left) + [root.key] + inorder(root.right) if root else []

# Build tree
root = Node(50)
root.left = Node(30)
root.right = Node(70)
root.left.left = Node(20)
root.right.right = Node(80)

print("Before deletion:", inorder(root))
root = delete_node(root, 30)
print("After deleting 30:", inorder(root))
OutputSuccess
Common Confusions
Thinking you can just remove any node without adjusting the tree.
Thinking you can just remove any node without adjusting the tree. Removing a node without reconnecting its children breaks the BST structure and order.
Always using the in-order successor for replacement.
Always using the in-order successor for replacement. You can use either the in-order successor or predecessor; both maintain BST properties.
Summary
Deleting nodes in a BST requires careful handling to keep the tree ordered and connected.
Leaf nodes are easiest to delete, while nodes with two children need replacement by successor or predecessor.
Understanding these cases helps maintain efficient search and update operations in BSTs.

Practice

(1/5)
1. Which of the following is NOT a case handled during deletion in a Binary Search Tree (BST)?
easy
A. Deleting a node with two children
B. Deleting a node with one child
C. Deleting a node with three children
D. Deleting a node with no children (leaf node)

Solution

  1. Step 1: Understand BST node children possibilities

    In a BST, each node can have zero, one, or two children. There is no case of three children because BST nodes have at most two children.
  2. Step 2: Identify valid deletion cases

    Deletion handles nodes with zero, one, or two children. Three children is impossible, so it is not a valid case.
  3. Final Answer:

    Deleting a node with three children -> Option C
  4. Quick Check:

    BST nodes have max two children = Deleting node with three children [OK]
Hint: Remember BST nodes have max two children only [OK]
Common Mistakes:
  • Thinking a node can have more than two children
  • Confusing deletion cases with other tree types
  • Ignoring the leaf node deletion case
2. Which of the following is the correct step to delete a node with two children in a BST?
easy
A. Replace the node with its parent node
B. Replace the node with its inorder predecessor (largest in left subtree)
C. Replace the node with any leaf node
D. Replace the node with its inorder successor (smallest in right subtree)

Solution

  1. Step 1: Identify deletion method for two children

    When deleting a node with two children, the standard method is to replace it with its inorder successor, which is the smallest node in its right subtree.
  2. Step 2: Confirm replacement correctness

    The inorder successor maintains BST properties after replacement, unlike arbitrary leaf or parent nodes.
  3. Final Answer:

    Replace the node with its inorder successor (smallest in right subtree) -> Option D
  4. Quick Check:

    Two children deletion uses inorder successor = Replace the node with its inorder successor (smallest in right subtree) [OK]
Hint: Use inorder successor for two-child node deletion [OK]
Common Mistakes:
  • Using inorder predecessor instead of successor
  • Replacing with any leaf node
  • Replacing with parent node
3. Consider the BST below:
      15
     /  \
    10  20
   /    / \
  8    17 25

If we delete node 20, what will be the new right child of 15?
medium
A. 17
B. 25
C. 20
D. 15

Solution

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

    Node 20 has two children: 17 (left) and 25 (right).
  2. Step 2: Find inorder successor of node 20

    The inorder successor is the smallest node in the right subtree of 20, which is 25. But since 25 has no left child, 25 is the successor.
  3. Step 3: Replace node 20 with its inorder successor

    Replace 20 with 25. Then remove original 25 node (which is a leaf). The new right child of 15 becomes 25.
  4. Final Answer:

    25 -> Option B
  5. Quick Check:

    Inorder successor of 20 is 25 = 25 [OK]
Hint: Replace two-child node with inorder successor (smallest right) [OK]
Common Mistakes:
  • Choosing 17 as successor instead of 25
  • Not updating parent's child pointer
  • Confusing inorder predecessor with successor
4. What is the error in the following deletion logic for a BST node with one child?
if node.left is not None:
    return node.left
else:
    return node.right
medium
A. It incorrectly returns the child without updating parent links
B. It should delete both children instead
C. It only works for leaf nodes
D. It should replace node with inorder successor

Solution

  1. Step 1: Analyze deletion logic for one child

    The code returns the child node directly but does not update the parent's pointer to this child, which breaks the BST links.
  2. Step 2: Identify missing link update

    Proper deletion requires updating the parent's child pointer to the returned node to maintain tree structure.
  3. Final Answer:

    It incorrectly returns the child without updating parent links -> Option A
  4. Quick Check:

    Missing parent link update = It incorrectly returns the child without updating parent links [OK]
Hint: Always update parent links when deleting nodes [OK]
Common Mistakes:
  • Ignoring parent pointer updates
  • Deleting both children unnecessarily
  • Using inorder successor for one-child deletion
5. You have a BST where you want to delete the root node which has two children. The inorder successor is the immediate right child with no left child. After replacing the root with the inorder successor, what must you do next to maintain BST properties?
hard
A. Replace the inorder successor with its right child
B. Replace the inorder successor with its left child
C. Do nothing, the tree is already valid
D. Remove the inorder successor node from its original position

Solution

  1. Step 1: Replace root with inorder successor

    The root is replaced by its inorder successor, which is the immediate right child with no left child.
  2. Step 2: Adjust inorder successor's original position

    Since the inorder successor has no left child, replace it with its right child (which may be None) to maintain BST links.
  3. Final Answer:

    Replace the inorder successor with its right child -> Option A
  4. Quick Check:

    Inorder successor replaced by right child = Replace the inorder successor with its right child [OK]
Hint: Replace successor with its right child after root replacement [OK]
Common Mistakes:
  • Forgetting to remove successor from original spot
  • Replacing successor with left child (which doesn't exist)
  • Assuming no further action needed