Data Structures & AlgorithmsHardGATE CS · Product-company placement

Keys 54, 17, 39, 66, 11, 36, 83, 51, 94, 10, 76 are inserted in that order into an empty BST. Then 39 is deleted and after that 83 is deleted; a node with two children is replaced by its inorder SUCCESSOR (the successor node is then removed from the right subtree). What is the preorder traversal of the final tree?

  1. A. 54, 17, 11, 10, 36, 51, 66, 76, 94
  2. B. 54, 17, 11, 10, 36, 51, 66, 94, 76
  3. C. 54, 17, 11, 10, 51, 36, 66, 94, 76
  4. D. 54, 17, 11, 10, 51, 36, 66, 76, 94

Correct answer: C. 54, 17, 11, 10, 51, 36, 66, 94, 76

Each deletion of a two-child node copies the smallest key of its right subtree into it and removes that key below, giving preorder 54, 17, 11, 10, 51, 36, 66, 94, 76. Using the predecessor, or rebuilding the tree from the remaining keys in insertion order, gives a different shape.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic