GATE Computer ScienceVery hardGATE CS (PYQ pattern)

For a BST built from distinct keys (deletion of a node with two children uses its inorder successor), consider: I. Inserting a new key and then deleting that same key always restores the original tree exactly. II. Deleting x and then y always gives the same tree as deleting y and then x. III. Inserting the keys in the tree's preorder sequence into an empty BST rebuilds the same tree. IV. Inserting the keys in the reverse of the tree's postorder sequence into an empty BST rebuilds the same tree. Which statements are true?

  1. A. I and III only
  2. B. I, III and IV only
  3. C. I, II and III only
  4. D. II and IV only

Correct answer: B. I, III and IV only

I: a new key is inserted as a leaf, and removing that leaf restores the tree. II fails: with the successor rule the order of two deletions can change which node is copied up (checked by brute force). III: preorder visits every parent before its descendants, so insertion rebuilds the same shape. IV: the reverse of postorder still lists every node before all of its descendants (root, then right part, then left part), so the same BST is rebuilt; only the order of insertion between independent subtrees changes.

More Programming and Data Structures (GATE) questions

Exams that ask this topic