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?

  1. A. [35, 27, 49, 13, 19, 17, 40, 7, 9]
  2. B. [49, 35, 40, 27, 19, 17, 7, 9, 13]
  3. C. [49, 35, 7, 27, 19, 17, 40, 9, 13]
  4. 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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic