Data Structures & AlgorithmsHardGATE CS
A binary max-heap is stored in a 0-indexed array [57, 47, 54, 14, 15, 6, 21]. Perform, in order: delete-max, insert 10, delete-max, insert 25. (Insert appends and sifts up; delete-max moves the last element to the root and sifts it down, swapping with the larger child.) What is the final array?
- A. [21, 14, 54, 10, 15, 6, 25]
- B. [47, 10, 25, 14, 15, 6, 21]
- C. [47, 15, 25, 14, 10, 6, 21]
- D. [47, 25, 21, 14, 6, 15, 10]
Correct answer: C. [47, 15, 25, 14, 10, 6, 21]
Sift-down must compare BOTH children and keep going until the heap property holds; doing that at every step gives [47, 15, 25, 14, 10, 6, 21]. Swapping with the left child whenever it beats the parent, or stopping after one swap, leaves a different (often invalid) array.
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 13, 30, 35, 39, 54 are searched with frequencies 8, 1, 9, 9, 12 respectively (only successful searches). The cost of a BST is Σ…
- A min-heap holds 63 distinct keys in a 0-indexed array (a full tree with 6 levels). At how many different indices can the 5th-smallest key…
- Keys 20, 94, 97, 28, 21, 78, 37, 93, 79, 36 are inserted into an empty AVL tree (giving preorder 78, 28, 21, 20, 37, 36, 93, 79, 94, 97)…
- Dijkstra's algorithm runs from A on the directed graph A→B (14), A→C (11), A→D (8), A→E (1), A→F (15), B→C (6), B→E (2), B→F (5), C→D (9)…
- Bellman–Ford runs from A on the directed graph whose edges are relaxed in EVERY pass in this fixed order: E→D (9), A→B (6), F→A (-3), F→D…
- Prim's algorithm starts at D on the weighted undirected graph A–D (23), A–F (5), A–G (28), B–C (17), B–E (15), B–G (2), C–E (21), C–G…