Data Structures & AlgorithmsHardGATE CS · Product-company placement
Keys 14, 86, 32, 49, 38, 76, 17, 91, 35, 81, 16 are inserted in that order into an empty BST. Then 32 is deleted and after that 86 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. 14, 81, 17, 16, 49, 38, 35, 76, 91
- B. 14, 91, 35, 17, 16, 49, 38, 76, 81
- C. 14, 49, 38, 17, 16, 35, 76, 91, 81
- D. 14, 35, 17, 16, 49, 38, 76, 81, 91
Correct answer: B. 14, 91, 35, 17, 16, 49, 38, 76, 81
Each deletion of a two-child node copies the smallest key of its right subtree into it and removes that key below, giving preorder 14, 91, 35, 17, 16, 49, 38, 76, 81. 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
- The weighted undirected graph A–B (2), A–E (1), B–C (6), B–D (1), B–E (2), C–D (6), D–E (3) has repeated weights. How many different…
- What is the edit distance (insert, delete, substitute; each cost 1) between SUBSEQUENT and PLATFORM?
- Dijkstra's algorithm runs from A on the directed graph A→B (4), A→F (8), B→C (1), B→D (10), C→A (3), C→D (8), C→E (6), C→F (6), E→B (1)…
- In the DAG with edges A→E, A→G, B→E, C→A, C→B, C→E, C→F, C→G, D→F, E→F, G→F, how many distinct directed paths are there from C to F, and…
- For the string PROPERTY, what are the lengths of the longest palindromic SUBSTRING and the longest palindromic SUBSEQUENCE?
- DFS runs on the directed graph with adjacency lists A: D; B: CF; C: AEG; D: –; E: ABG; F: D; G: F, starting at A, scanning lists in the…