Data Structures & AlgorithmsHardGATE CS · Amazon
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 form a max-heap. The resulting array is:
- A. [9, 6, 8, 5, 2, 1, 7, 3, 4]
- B. [9, 6, 8, 5, 4, 1, 7, 2, 3]
- C. [9, 7, 8, 4, 6, 1, 2, 3, 5]
- D. [9, 6, 7, 5, 4, 1, 8, 2, 3]
Correct answer: A. [9, 6, 8, 5, 2, 1, 7, 3, 4]
Sift down indices 3, 2, 1, 0 in turn: 4 swaps with 5; 9 at index 2 stays; 2 swaps with 6; finally 7 swaps with 9 and then with 8, ending at index 6. The one-by-one insertion method gives a different heap, [9, 6, 8, 5, 4, 1, 7, 2, 3].
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
- 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…
- A directed graph has the vertices A, B, C, D, E, F, G, H, I and the arcs A→E, B→E, B→F, D→C, E→B, E→C, G→E, G→I, H→B, H→F, I→C, I→H. What…
- An undirected connected graph has the vertices A–J and the edges A–H, B–E, C–I, C–J, D–E, E–G, E–I, F–I, G–J, H–I, H–J, I–J. How many…
- A graph has the vertices A–F and the edges A–B, A–C, B–C, B–D, C–D, C–E, D–E, D–F, E–F, A–F. How many distinct spanning trees does it have?