Bird
Raised Fist0
Data Structures Theoryknowledge~15 mins

Deletion in BST in Data Structures Theory - Deep Dive

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
Overview - Deletion in BST
What is it?
Deletion in a Binary Search Tree (BST) is the process of removing a node while keeping the tree's order properties intact. A BST is a tree where each node's left child has a smaller value and the right child has a larger value. Deletion must carefully rearrange nodes so the BST rules still hold after removal. This operation is more complex than insertion or searching because it involves different cases depending on the node's children.
Why it matters
Without a proper deletion method, removing nodes could break the BST structure, making searches incorrect or inefficient. This would defeat the purpose of using a BST, which is to quickly find, add, or remove data. Efficient deletion keeps the tree balanced and ordered, ensuring fast operations in databases, file systems, and many software applications.
Where it fits
Before learning deletion, one should understand BST structure, insertion, and searching. After mastering deletion, learners can explore tree balancing techniques like AVL or Red-Black Trees, which maintain performance by controlling tree height.
Mental Model
Core Idea
Deleting a node in a BST means removing it while rearranging its children so the tree remains ordered and searchable.
Think of it like...
Imagine removing a book from a neatly arranged bookshelf where books are sorted by size. If the book is alone, just take it out. If it has smaller books on one side and bigger on the other, you must shift books around to keep the order intact.
Deletion in BST:

  Node to delete
     /    |    \
  Case 1  Case 2  Case 3
  (Leaf)  (One child) (Two children)

Case 1: Remove node directly.
Case 2: Replace node with its child.
Case 3: Replace node with inorder successor or predecessor.
Build-Up - 7 Steps
1
FoundationUnderstanding BST Structure Basics
šŸ¤”
Concept: Learn what a BST is and how nodes are arranged based on values.
A Binary Search Tree is a tree where each node has at most two children. The left child's value is always less than the node's value, and the right child's value is always greater. This property allows fast searching by deciding to go left or right at each node.
Result
You can quickly find if a value exists by comparing and moving left or right.
Understanding the BST property is essential because deletion must preserve this order to keep the tree useful.
2
FoundationIdentifying Node Types for Deletion
šŸ¤”
Concept: Recognize the three types of nodes that affect deletion: leaf, one child, two children.
Nodes in a BST can be: - Leaf nodes: no children - Nodes with one child: either left or right child - Nodes with two children: both left and right children Each type requires a different deletion approach.
Result
You can classify any node to decide how to delete it.
Knowing node types upfront simplifies choosing the correct deletion method.
3
IntermediateDeleting a Leaf Node Simply
šŸ¤”
Concept: Removing a node with no children is straightforward and does not affect other nodes.
If the node to delete is a leaf, just remove it by setting its parent's pointer to null. No rearrangement is needed because no children depend on it.
Result
The node is removed cleanly without disturbing the BST structure.
This case is the simplest and builds confidence before handling more complex deletions.
4
IntermediateDeleting Node with One Child
šŸ¤”Before reading on: do you think you should replace the node with its child or remove it directly? Commit to your answer.
Concept: When a node has one child, replace it with that child to maintain the BST order.
To delete a node with one child, link the node's parent directly to the node's child. This bypasses the node and keeps the tree connected and ordered.
Result
The node is removed, and its child takes its place, preserving BST properties.
Replacing with the single child keeps the tree structure intact without losing any nodes.
5
IntermediateDeleting Node with Two Children
šŸ¤”Before reading on: do you think you can remove the node directly or must you find a replacement? Commit to your answer.
Concept: For nodes with two children, find a replacement node that preserves BST order, usually the inorder successor or predecessor.
Find the smallest node in the right subtree (inorder successor) or largest in the left subtree (inorder predecessor). Replace the node's value with this replacement, then delete the replacement node, which will have at most one child.
Result
The node is removed, replaced by a value that keeps the BST order, and the tree remains valid.
Using inorder successor/predecessor ensures the BST property holds after deletion.
6
AdvancedHandling Inorder Successor Deletion
šŸ¤”Before reading on: do you think deleting the inorder successor is as complex as the original node? Commit to your answer.
Concept: Deleting the inorder successor is simpler because it has at most one child, reducing the problem to earlier cases.
After copying the inorder successor's value to the node to delete, remove the successor node. Since it has no left child, deletion is either leaf or one-child case, which is simpler.
Result
The BST remains ordered with minimal restructuring.
Breaking down complex deletion into simpler steps avoids complicated tree rearrangements.
7
ExpertBalancing Effects After Deletion
šŸ¤”Before reading on: do you think deletion always keeps the BST balanced? Commit to your answer.
Concept: Deletion can unbalance a BST, affecting search efficiency, which leads to advanced balancing techniques.
Removing nodes may cause the tree to become skewed, increasing height and slowing searches. Balanced BSTs like AVL or Red-Black Trees use rotations after deletion to keep height minimal and operations fast.
Result
Without balancing, worst-case search time can degrade to linear; with balancing, it stays logarithmic.
Understanding deletion's impact on balance is key to designing efficient, reliable tree structures.
Under the Hood
Deletion works by adjusting pointers in the tree nodes. When deleting a node, the algorithm finds the node, then depending on its children, either removes it directly, replaces it with its child, or swaps its value with the inorder successor/predecessor and deletes that node. This pointer manipulation preserves the BST ordering invariant. Internally, the tree nodes are connected by references, and deletion changes these references carefully to avoid losing parts of the tree.
Why designed this way?
The design ensures that after deletion, the BST property remains intact without rebuilding the entire tree. Using the inorder successor or predecessor as replacement is a natural choice because these nodes maintain the sorted order. Alternatives like rebuilding the tree from scratch would be inefficient. This method balances simplicity and correctness.
BST Deletion Flow:

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│ Find node to  │
│ delete        │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
       │
       ā–¼
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│ Node type?    │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
       │
 ā”Œā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”
 │           │
 ā–¼           ā–¼
Leaf       One child?
 │           │
 ā–¼           ā–¼
Remove     Replace node
node       with child
       
       Two children?
           │
           ā–¼
  Find inorder successor
           │
           ā–¼
  Replace value, delete successor
Myth Busters - 4 Common Misconceptions
Quick: Is deleting a node with two children as simple as deleting a leaf node? Commit to yes or no.
Common Belief:Deleting any node is just removing it directly from the tree.
Tap to reveal reality
Reality:Nodes with two children cannot be removed directly without rearranging because it would break the BST order. Instead, their value is replaced with the inorder successor or predecessor, then that node is deleted.
Why it matters:Direct removal breaks the tree's order, causing incorrect search results and corrupting the data structure.
Quick: Does deleting a node always keep the tree balanced? Commit to yes or no.
Common Belief:Deleting a node never affects the tree's balance or height.
Tap to reveal reality
Reality:Deletion can unbalance the tree, making it taller and slower to search. Balancing operations are needed in some BST variants to maintain efficiency.
Why it matters:Ignoring balance leads to poor performance, turning fast searches into slow linear scans.
Quick: When deleting a node with one child, do you need to find a replacement node? Commit to yes or no.
Common Belief:You must always find a replacement node regardless of children.
Tap to reveal reality
Reality:If the node has only one child, you replace the node directly with its child, no need to find inorder successor or predecessor.
Why it matters:Unnecessarily searching for replacements complicates deletion and wastes time.
Quick: Is the inorder successor always the right child of the node? Commit to yes or no.
Common Belief:The inorder successor is always the immediate right child of the node to delete.
Tap to reveal reality
Reality:The inorder successor is the smallest node in the right subtree, which may be deeper than the immediate right child.
Why it matters:Assuming the successor is the immediate right child can cause incorrect deletion and tree corruption.
Expert Zone
1
When deleting the inorder successor, it often has no left child, simplifying its removal to a leaf or one-child case.
2
Choosing between inorder successor and predecessor can affect tree balance subtly; some implementations pick one consistently for simplicity.
3
In threaded BSTs, deletion must also update thread pointers, adding complexity beyond standard BSTs.
When NOT to use
Standard BST deletion is not ideal when frequent deletions cause imbalance; in such cases, use self-balancing trees like AVL or Red-Black Trees. For very large datasets requiring guaranteed performance, B-Trees or other balanced multi-way trees are better alternatives.
Production Patterns
In databases and file systems, deletion often triggers rebalancing or restructuring to maintain performance. Some systems delay physical deletion (lazy deletion) to optimize batch operations. Also, deletion is combined with concurrency controls to handle multiple users safely.
Connections
AVL Trees
builds-on
Understanding BST deletion is essential before learning AVL Trees, which add rotations after deletion to keep the tree balanced.
Garbage Collection
similar pattern
Both deletion in BST and garbage collection involve safely removing elements without losing references to needed data, ensuring system integrity.
Supply Chain Management
conceptual analogy
Removing a node in BST is like removing a supplier from a chain; you must reroute connections to keep the supply flow uninterrupted, similar to maintaining order in the tree.
Common Pitfalls
#1Removing a node with two children by deleting it directly without replacement.
Wrong approach:Delete node directly and set parent's pointer to null.
Correct approach:Replace node's value with inorder successor's value, then delete the inorder successor node.
Root cause:Misunderstanding that direct removal breaks BST order when two children exist.
#2Failing to update parent's pointer when deleting a node with one child.
Wrong approach:Remove node but leave parent's pointer unchanged, causing dangling reference.
Correct approach:Set parent's pointer to node's single child to maintain tree connectivity.
Root cause:Not recognizing the need to reconnect the child to the parent after deletion.
#3Assuming inorder successor is always the immediate right child and not searching deeper.
Wrong approach:Replace node's value with immediate right child's value without checking for smaller nodes in right subtree.
Correct approach:Find the leftmost node in the right subtree as inorder successor before replacement.
Root cause:Oversimplifying inorder successor definition leading to incorrect replacement.
Key Takeaways
Deletion in a BST must preserve the tree's ordering property to keep searches correct.
There are three deletion cases: leaf node, node with one child, and node with two children, each handled differently.
Replacing a node with two children by its inorder successor or predecessor maintains the BST structure.
Deletion can unbalance a BST, so balanced trees use additional steps to maintain efficiency.
Understanding deletion deeply helps in designing and maintaining efficient data structures and systems.

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