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?
- A. 60, 18, 55, 31, 26, 95, 69, 65, 78
- B. 65, 26, 18, 60, 55, 31, 95, 69, 78
- C. 95, 60, 55, 18, 31, 26, 69, 65, 78
- 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.
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
- A 4 × 4 grid has rows 1 5 2 1 / 1 1 5 4 / 7 4 6 2 / 1 6 3 8. Moving only right or down from the top-left to the bottom-right cell, what is…
- How many calls of g (including the first) are made when g(7) is evaluated, and what value is returned? c int g(int n) { if (n <= 2) return…
- The words trie, tried, trip, try, tree are inserted into an empty trie (one node per character, shared prefixes share nodes, plus a root)…
- In the grid 21112 / 11110 / 10111 / 11111 (rows top to bottom; 2 = rotten, 1 = fresh, 0 = empty), every minute each rotten orange rots its…
- An undirected graph has edges A–E, A–H, B–F, C–E, C–G, D–G, D–H, E–F, E–G, F–H. BFS starts at A and always scans neighbours in…
- A tree on vertices 1–10 has edges 2–1, 3–2, 4–1, 5–4, 6–1, 7–3, 8–7, 9–5, 10–9. What is the size of a minimum DOMINATING set (every vertex…