Data Structures & AlgorithmsVery hardTCS Digital · Product companies
Keys 50, 30, 70, 20, 40, 60, 80, 35, 45, 65 are inserted in order into an empty BST. Then 30 is deleted, then 50 is deleted; a node with two children is replaced by its inorder successor. What is the pre-order traversal of the final tree?
- A. 45 20 40 35 70 60 65 80
- B. 50 35 20 40 45 70 60 65 80
- C. 60 35 20 40 45 70 65 80
- D. 60 30 20 40 35 45 70 65 80
Correct answer: C. 60 35 20 40 45 70 65 80
Deleting 30 puts its successor 35 in its place (35 was 40's left child and disappears from there). Deleting 50 puts its successor 60 at the root, and 60's right child 65 moves up to take its place under 70, giving 60 35 20 40 45 70 65 80.
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 array [3, 9, 2, 1, 4, 5, 8] is converted to a max-heap by Floyd's bottom-up method, sifting down from the last parent to the root…
- Keys 50, 30, 70, 20, 40, 60, 80, 35, 45, 65, 36 are inserted in this order into an empty binary search tree. The post-order traversal of…
- Keys 40, 20, 10, 25, 30, 22, 50, 35, 45, 60, 55, 65 are inserted one by one into an initially empty AVL tree. The pre-order traversal of…
- Floyd's bottom-up build-heap (sifting down from the last internal node to the root) is applied to the array [7, 2, 9, 4, 6, 1, 8, 3, 5] to…
- A DAG on vertices 1..6 has edges 1→3, 2→3, 3→4, 3→5, 2→6 and 5→6. How many distinct topological orderings does it have?
- A weighted digraph has edges S→A (2), S→B (5), A→C (4), B→A (−4), B→C (1) and C→T (1). Textbook Dijkstra (a vertex's distance is final…