GATE Computer ScienceVery hardGATE CS (PYQ pattern)

In the BST built by inserting 40, 20, 60, 10, 30, 50, 70, 25, 35, 45, 55, 65, 75, 22 in order, what are the inorder successor of 35, the inorder predecessor of 40 and the inorder successor of 22, written in that order?

  1. A. 40, 35, 25
  2. B. 40, 35, 30
  3. C. 45, 35, 25
  4. D. 40, 30, 25

Correct answer: A. 40, 35, 25

35 is the largest key in the left subtree of 30, so its successor is the nearest ancestor of which it is in the left subtree: 40. The predecessor of 40 is the rightmost node of its left subtree: 35. 22 has no right child; its successor is its nearest ancestor reached from the left: 25.

More Programming and Data Structures (GATE) questions

Exams that ask this topic