سوال ۶۸ کنکور ارشد علوم کامپیوتر ۱۳۹۸
در درخت جستجوی باینری زیر با حذف ریشهیک از عناصر زیر جایگزین میشود؟
پاسخ صحیح: گزینهٔ B — یا
The tree
The BST has root with the structure:
a
/ \
b f
/ \ \
c d g
\ /
e h
That is: has left child and right child ; has children (left) and (right); has right child ; has right child ; and has left child .
Deleting a node with two children
The root has two children, so it cannot simply be unlinked. The standard rule is to overwrite the deleted node's key with either:
- its in-order predecessor — the largest key in the left subtree (rightmost node), or
- its in-order successor — the smallest key in the right subtree (leftmost node).
Either choice preserves the BST ordering, since both are immediate neighbors of the root in the sorted (in-order) sequence.
Find predecessor and successor
In-order predecessor (rightmost of the left subtree rooted at ): follow right pointers as far as possible.
In-order successor (leftmost of the right subtree rooted at ): follow left pointers as far as possible. But has no left child, so the leftmost node is itself.
Conclusion
The root can be replaced by (successor) or (predecessor) — option 2.
Quick sanity check via the in-order (sorted) sequence: . The two values flanking are exactly and .
Pitfall: The successor is the leftmost node of the right subtree, not the deepest node. Here has no left child, so itself is the successor — don't wander down to . Likewise the predecessor is the rightmost node of the left subtree (), not the leftmost ().