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?
- A. 40, 11, 19, 31, 72, 71, 45, 76, 74
- B. 31, 11, 19, 72, 71, 40, 45, 76, 74
- C. 11, 72, 19, 71, 31, 40, 45, 76, 74
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Keys 90, 94, 89, 30, 62, 17, 99, 53 inserted in this order build a BST. How many of the 8! orderings of these same 8 keys, inserted into…
- A binary search tree holds exactly the keys 13, 19, 20, 36, 71, 73, 94, 99. Which ONE of the following sequences can be its PREORDER…
- A BST holds integer keys between 1 and 100. Which sequence CANNOT be the sequence of keys examined while searching for 44?
- A rooted tree has these parent→child edges: 56→8, 64→56, 64→24, 64→45, 24→59, 45→41, 8→2. A depth-first search starts at the root and, at…
- A rooted tree has these parent→child edges: 74→13, 74→24, 84→62, 13→75, 13→66, 24→84. A depth-first search starts at the root and, at…
- A rooted tree has these parent→child edges: 72→45, 31→27, 98→31, 27→38, 31→72, 98→71. A depth-first search starts at the root and, at…