Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2
Keys are inserted in the order 12 31 77 11 65 57 into an empty binary search tree. Then the key 12 (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. 31 11 31 77 65 57
- B. 31 11 77 65 57
- C. 11 31 77 65 57
- D. 12 11 31 77 65 57
Correct answer: B. 31 11 77 65 57
The successor (smallest key of the right subtree) replaces 12, so the preorder becomes 31 11 77 65 57. 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
- Distinct words ['aa', 'ab', 'bb', 'ca', 'aab'] are inserted into an empty trie (one node per character, shared prefixes share nodes, and a…
- Keys are inserted in the order 11 88 20 15 45 into an empty binary search tree. Then the key 20 (which has two children) is deleted by…
- Consider the recursive function def f(n): if n < 2: return n; return f(n-1) + f(n-2). Evaluate f(6) and count every invocation of f…
- Words ['bdc', 'dd', 'dba', 'ddc', 'ccb'] (each may be used at most once). A chain is a sequence of these words in which the first letter…
- Array A = [2, 3, 3, 4, 2, 2, 3, 5, 2]. An increasing subsequence is chosen by INDEX positions i1 < i2 < … with A[i1] < A[i2] < … (strictly…
- A trie keeps a counter at each node, and every word of the list W = ['ab', 'cc', 'ba', 'ca', 'ac', 'ba', 'cba', 'bba'] is inserted once…