Skip to lesson content
navan✳THE BUILD LABDSA
0/66 complete
BUILD LAB / DSA/ Trees and hierarchy
EXPERIMENT 33 / 66

Remove a key, preserve the order.

Handle leaf, one-child, and two-child removal without losing the search invariant.

BY NAVAN SAMI◷ 10 MIN READCOREALGORITHM

01 Model the idea

BST deletion has three structural cases. A leaf can disappear; a one-child node can be replaced by its child; a two-child node can take its in-order successor’s key, then remove that successor from the right subtree. The successor is the smallest key in the right subtree, so it preserves the ordering relationship when replacing the deleted key.

THE THING THAT MUST STAY TRUE

All remaining left keys stay smaller and right keys stay larger.

02 State the strategy

Read the steps independently of syntax. Explain what each step preserves before translating it into code.

the_strategy.txt
FIND the key by BST comparisons
REPLACE a zero/one-child node directly
FOR two children: copy successor and delete its old occurrence

03 Make it concrete

The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.

bst_deletion.py
class Node:
    def __init__(self, value, left=None, right=None): self.value, self.left, self.right = value, left, right
def delete(node, key):
    if node is None: return None
    if key < node.value: node.left = delete(node.left, key)
    elif key > node.value: node.right = delete(node.right, key)
    else:
        if node.left is None: return node.right
        if node.right is None: return node.left
        successor = node.right
        while successor.left: successor = successor.left
        node.value = successor.value
        node.right = delete(node.right, successor.value)
    return node
def inorder(node):
    return inorder(node.left) + [node.value] + inorder(node.right) if node else []
root = Node(4, Node(2), Node(6, Node(5), Node(7)))
root = delete(root, 4)
print(inorder(root))
↳ EXPECTED OUTPUT
[2, 5, 6, 7]
  1. 1

    The recursive return is the possibly changed subtree root. Storing it reconnects each parent correctly.

  2. 2

    The two-child case copies the successor value, then deletes its old occurrence so the key is not duplicated.

  3. 3

    Deletion is proportional to tree height in this unbalanced representation. AVL deletion needs additional rebalancing and is not implemented by this ordinary BST routine.

04 Count the cost

TIME

O(h) deletion; demonstration traversal can add copying cost

SPACE

O(h) deletion stack; O(n) tree storage

Bounds refer to the shown implementation and exclude printed output unless stated. n is input size; V and E are graph vertices and edges.

05 Your next move

A SMALL MISSION

Test deleting a missing key, a leaf, and a root with one child using the implementation above.

Reveal one possible solution +
one_possible_solution.py
class Node:
    def __init__(self, value, left=None, right=None): self.value, self.left, self.right = value, left, right
def delete(node, key):
    if not node: return None
    if key < node.value: node.left = delete(node.left, key)
    elif key > node.value: node.right = delete(node.right, key)
    elif not node.left: return node.right
    elif not node.right: return node.left
    else:
        successor = node.right
        while successor.left: successor = successor.left
        node.value = successor.value; node.right = delete(node.right, successor.value)
    return node
assert delete(Node(1), 1) is None
assert delete(Node(2, Node(1)), 2).value == 1
assert delete(Node(2), 9).value == 2
print("Deletion cases passed")

06 Check your reasoning

QUICK SIGNAL CHECK

Where is a two-child node’s in-order successor?

ANOTHER PATTERN IN YOUR TOOLKIT.

Understand it. Then own it.

Can you explain the invariant, trace an edge case, and defend the cost model? Log the experiment when the reasoning makes sense.

Built with curiosity. By Navan Sami. ↗Patterns over panic. One step at a time.