Data Structures & AlgorithmsHardGATE CS · Product-company placement

Keys 64, 95, 22, 60, 55, 18, 69, 31, 78, 65, 26 are inserted in that order into an empty BST. Then 64 is deleted and after that 22 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. 60, 18, 55, 31, 26, 95, 69, 65, 78
  2. B. 65, 26, 18, 60, 55, 31, 95, 69, 78
  3. C. 95, 60, 55, 18, 31, 26, 69, 65, 78
  4. D. 65, 18, 60, 55, 31, 26, 95, 69, 78

Correct answer: B. 65, 26, 18, 60, 55, 31, 95, 69, 78

Each deletion of a two-child node copies the smallest key of its right subtree into it and removes that key below, giving preorder 65, 26, 18, 60, 55, 31, 95, 69, 78. 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