GATE / CS ARCHIVE

GATE CSE 1996 / QUESTION 4(a)

Test a binary-search-tree probe sequence

Data Structures · Binary Search & Balanced Trees · Full GATE CSE 1996 paper

GATE 1996, Q4(a) Descriptive
Report issue

Binary Search & Balanced Trees

Question 4(a), GATE 1996: A BST is searched for key 43. Determine whether the probe sequence 61, 52, 14, 17, 40, 43 can occur, and explain. GFG instead prints 11 in place of 17 at the fourth probe.
Question 4(a), GATE 1996: A BST is searched for key 43. Determine whether the probe sequence 61, 52, 14, 17, 40, 43 can occur, and explain. GFG instead prints 11 in place of 17 at the fourth probe.
Tap to enlarge
Answer & solution

Answer: Impossible (no official key was published)

A search path obeys every ancestor constraint, not merely the immediately preceding comparison. Maintain an open interval of permissible keys, initially (−∞,+∞). When a probe is below 43, searching right raises the lower bound to that probe. When a probe is above 43, searching left lowers the upper bound.

After 61, every later probe must lie in (−∞,61).
After 52, every later probe must lie in (−∞,52).
After 14, every later probe must lie in (14,52).
After 17, every later probe must lie in (17,52).
After 40, every later probe must lie in (40,52).
The final probe is 43, so the search succeeds.

All probes meet the bounds. A witnessing BST can be built as a chain with these keys, taking the left or right child according to each comparison with 43. Thus the bound test proves existence, rather than just failing to find an error.

With 17 the sequence is valid. With 11 it is invalid, because after visiting 14 the search for 43 must stay in the right subtree of 14, whose keys exceed 14. The 11 variant therefore has the opposite answer.

Original paper, page 7