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?

  1. A. [21, 14, 54, 10, 15, 6, 25]
  2. B. [47, 10, 25, 14, 15, 6, 21]
  3. C. [47, 15, 25, 14, 10, 6, 21]
  4. 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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic