Data Structures & AlgorithmsVery hardICPC regional · GATE CS top band
Keys are inserted in the order 48 55 26 32 38 into an empty binary search tree. Then the key 48 (which has two children) is deleted by replacing it with its INORDER SUCCESSOR, and the successor node is removed from the right subtree. What is the preorder traversal (root, left, right) of the tree afterwards? Give the keys separated by spaces.
- A. 48 26 32 38 55
- B. 55 26 32 38 55
- C. 38 26 32 55
- D. 55 26 32 38
Correct answer: D. 55 26 32 38
The successor (smallest key of the right subtree) replaces 48, so the preorder becomes 55 26 32 38. Using the predecessor instead gives a different shape, and forgetting to remove the successor node leaves a duplicate.
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
- Words ['b', 'abbb', 'b', 'ca', 'b', 'bac'] are inserted in this order into an empty trie with one node per character, shared prefixes…
- Directed weighted graph on vertices A, B, C, D, E, F with edges listed in this exact order: F→D (3); F→B (3); D→E (1); D→B (2); A→C (7)…
- Tree with vertices 0 to 7, root 0, edges 0–1, 1–2, 0–3, 3–4, 2–5, 5–6, 1–7 (parent–child). Vertex weights (vertex:weight) 0:10, 1:14…
- Distinct keys [10, 21, 12, 26, 19] are inserted, in the order given by the sequence [12, 21, 19, 10, 26], into an empty binary search…
- A chain of 5 matrices M1 to M5 has dimensions given by the sequence p = [20, 17, 5, 11, 19, 20], where Mi has size p[i−1] × p[i]…
- Connected undirected weighted graph on vertices A, B, C, D, E, F with edges A–E (3), A–F (5), B–D (1), B–E (2), C–F (20), D–E (17), D–F…