Data Structures & AlgorithmsHardGATE CS · Product-company placement
Keys 17, 35, 21, 81, 58, 30, 62, 57, 82, 93, 61 are inserted in that order into an empty BST. Then 58 is deleted and after that 35 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. 17, 30, 21, 81, 57, 62, 61, 82, 93
- B. 17, 57, 21, 30, 81, 61, 62, 82, 93
- C. 17, 21, 81, 30, 62, 57, 61, 82, 93
- D. 17, 21, 30, 81, 61, 57, 62, 82, 93
Correct answer: B. 17, 57, 21, 30, 81, 61, 62, 82, 93
Each deletion of a two-child node copies the smallest key of its right subtree into it and removes that key below, giving preorder 17, 57, 21, 30, 81, 61, 62, 82, 93. 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
- Using parts 1, 2, 3 (each any number of times), in how many ways can 7 be written as an ORDERED sum (so 1 + 2 and 2 + 1 count separately)?
- For the string ABRACADABRA, what are the lengths of the longest palindromic SUBSTRING and the longest palindromic SUBSEQUENCE?
- Heap sort (ascending) is run on [20, 37, 49, 16, 22, 10, 31, 33]: build a max-heap bottom-up, then repeatedly swap the root with the last…
- What is the length of the longest BITONIC subsequence (strictly increasing then strictly decreasing; either part may be empty) of [20, 21…
- 0/1 knapsack with capacity 17: items (weight, value) = (9, 21), (6, 19), (3, 9), (7, 13), (3, 27). What is the maximum total value?
- Coins of denominations 1, 6, 9, 10 are available in unlimited supply. What is the minimum number of coins needed to make 33, and how many…