KonkurStudy
همه کارت‌ها

Deleting a Node with Two Children from a BST

To delete a node with two children, overwrite its key with its in-order predecessor (max of the left subtree) or successor (min of the right subtree), then delete that easier node.

کارت الگوریتم کنکور ارشد علوم کامپیوتر

اهمیت
۰/۵

Deleting from a binary search tree has three cases, distinguished by how many children the target node xx has:

Case Action
0 children (leaf) Unlink xx directly.
1 child Splice xx out: connect its parent to its single child.
2 children You cannot just remove xx — both subtrees would be orphaned. Replace xx's key with a neighbouring key, then delete that node instead.

The two-children rule

The only key that may take xx's place without breaking the BST ordering is an immediate in-order neighbour of xx. There are exactly two choices:

  • the in-order predecessor — the largest key in the left subtree, found by going left once and then right as far as possible (the rightmost node of the left subtree); or
  • the in-order successor — the smallest key in the right subtree, found by going right once and then left as far as possible (the leftmost node of the right subtree).

Why these work: the predecessor is the value just below xx in sorted order, so every key in the left subtree stays ≤\le it and every key in the right subtree stays >> it — the BST invariant is preserved. Crucially, both the predecessor and the successor have at most one child (the predecessor has no right child; the successor has no left child), so once you copy the key up, removing the original node falls into the easy 0- or 1-child case.

How to apply it

  1. Locate the node xx to delete and confirm it has two children.
  2. Pick a strategy — say, the in-order successor ss = leftmost node of xx's right subtree.
  3. Copy ss's key into xx (overwrite xx's key; structure of the tree is unchanged so far).
  4. Recursively delete ss from the right subtree. Since ss is leftmost, it has no left child, so this is a 0- or 1-child deletion.

The whole operation walks one root-to-leaf path to find xx and another to find the predecessor/successor, so it costs O(h)O(h) time, where hh is the height (Θ(log⁡n)\Theta(\log n) if balanced, Θ(n)\Theta(n) in the worst case).

Worked mini-example

Delete the root 5050 from:

        50
       /  \
     30    70
    /  \   /  \
  20   40 60   80
  • Predecessor route: largest key in the left subtree = rightmost of {20,30,40}\{20,30,40\} = 4040. Replace 5050 with 4040, then delete the old 4040 (a leaf).
  • Successor route: smallest key in the right subtree = leftmost of {60,70,80}\{60,70,80\} = 6060. Replace 5050 with 6060, then delete the old 6060 (a leaf).

Either replacement keeps the tree a valid BST. So the candidates that can replace the deleted root are exactly the predecessor (4040) and the successor (6060) — no other node qualifies.

Exam trick: when a question asks "which element can replace the deleted (two-child) node?", the answer is always one of just two nodes: the rightmost node of the left subtree (predecessor) or the leftmost node of the right subtree (successor). Any other node in the tree is a distractor. Remember: predecessor = left-then-all-right; successor = right-then-all-left.

فرمول‌های کلیدی

In-order predecessor (left subtree maximum)
pred(x)=max⁡(x.left subtree)=rightmost node reached by x.left→right→⋯\text{pred}(x) = \max(x.\text{left subtree}) = \text{rightmost node reached by } x.\text{left} \to \text{right} \to \cdots

Go left once, then follow right children to the end; this node has no right child.

In-order successor (right subtree minimum)
succ(x)=min⁡(x.right subtree)=leftmost node reached by x.right→left→⋯\text{succ}(x) = \min(x.\text{right subtree}) = \text{leftmost node reached by } x.\text{right} \to \text{left} \to \cdots

Go right once, then follow left children to the end; this node has no left child.

#binary-search-tree#bst-deletion#in-order-successor#in-order-predecessor#tree-operations

سوالات تمرینی