Data Structures & AlgorithmsHardInfosys SP · TCS Digital
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. What is the final array?
- A. 9 4 8 1 3 2 5
- B. 9 4 8 1 3 5 2
- C. 9 8 5 4 3 2 1
- D. 9 3 8 1 4 5 2
Correct answer: B. 9 4 8 1 3 5 2
Sift down index 2 (2 vs 5, 8): swap with 8; index 1 (9) stays; root 3 goes below 9, then swaps with 4 to give 9 4 8 1 3 5 2. Inserting the keys one by one instead would give 9 4 8 1 3 2 5.
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
- 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…
- An undirected graph on vertices a-f has edges ab(1), bc(2), ac(2), cd(3), bd(3), de(1), ce(4), ae(4), ef(2), df(2). How many distinct…