Data Structures & AlgorithmsHardGATE CS · Product-company placement

Keys 33, 11, 72, 19, 76, 71, 31, 74, 40, 37, 45 are inserted in that order into an empty BST. Then 33 is deleted and after that 37 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. 40, 11, 19, 31, 72, 71, 45, 76, 74
  2. B. 31, 11, 19, 72, 71, 40, 45, 76, 74
  3. C. 11, 72, 19, 71, 31, 40, 45, 76, 74
  4. D. 11, 19, 31, 72, 71, 40, 45, 76, 74

Correct answer: A. 40, 11, 19, 31, 72, 71, 45, 76, 74

Each deletion of a two-child node copies the smallest key of its right subtree into it and removes that key below, giving preorder 40, 11, 19, 31, 72, 71, 45, 76, 74. 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