Data Structures & AlgorithmsVery hardICPC regional · GATE CS top band
Keys are inserted in the order 78 11 62 12 89 into an empty binary search tree. Then the key 78 (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. 89 11 62 12
- B. 89 11 62 12 89
- C. 78 11 62 12 89
- D. 62 11 12 89
Correct answer: A. 89 11 62 12
The successor (smallest key of the right subtree) replaces 78, so the preorder becomes 89 11 62 12. 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
- Complete undirected graph on vertices A to F with symmetric edge weights given row by row (row i, column j = weight of edge between…
- Undirected weighted graph on vertices A, B, C, D, E, F; its edges, in this listed order, are: A–E (2), D–F (6), B–C (8), C–D (4), A–B (7)…
- Array A = [4, 3, 9, 3, 3, 9, 7] (indices 0 to 6). Count the non-empty subsets of positions (each position used at most once, subsets not…
- Distinct words ['aa', 'ab', 'ca', 'c', 'bb'] are inserted into an empty trie (one node per character, shared prefixes share nodes, and a…
- Undirected weighted graph on vertices A, B, C, D, E with edges A–B (1), A–C (1), A–E (1), B–C (2), B–D (3), C–E (1), D–E (3). How many…
- Directed weighted graph on vertices A, B, C, D, E with edges (u→v, w): C→D (8); E→C (4); D→C (8); C→E (6); D→A (4); B→E (5). Let d(x, y)…