Data Structures & AlgorithmsHardGATE CS
A binary max-heap is stored in a 0-indexed array [57, 27, 49, 9, 19, 17, 40]. Perform, in order: insert 7, delete-max, insert 13, insert 35. (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. [35, 27, 49, 13, 19, 17, 40, 7, 9]
- B. [49, 35, 40, 27, 19, 17, 7, 9, 13]
- C. [49, 35, 7, 27, 19, 17, 40, 9, 13]
- D. [49, 35, 40, 17, 19, 27, 7, 9, 13]
Correct answer: B. [49, 35, 40, 27, 19, 17, 7, 9, 13]
Sift-down must compare BOTH children and keep going until the heap property holds; doing that at every step gives [49, 35, 40, 27, 19, 17, 7, 9, 13]. 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
- Floyd–Warshall runs on vertices 1–4 with directed edges 1→3 (2), 2→1 (7), 2→3 (4), 3→1 (3), 3→4 (8), 4→1 (7), 4→2 (1). After the…
- A tree on vertices 1–8 has edges 2–1, 3–1, 4–2, 5–2, 6–4, 7–5, 8–3. What is the sum of the distances (in edges) over all unordered pairs…
- Keys 64, 95, 22, 60, 55, 18, 69, 31, 78, 65, 26 are inserted in that order into an empty BST. Then 64 is deleted and after that 22 is…
- A 4 × 4 grid has rows 1 5 2 1 / 1 1 5 4 / 7 4 6 2 / 1 6 3 8. Moving only right or down from the top-left to the bottom-right cell, what is…
- How many calls of g (including the first) are made when g(7) is evaluated, and what value is returned? c int g(int n) { if (n <= 2) return…
- The words trie, tried, trip, try, tree are inserted into an empty trie (one node per character, shared prefixes share nodes, plus a root)…