GATE Computer ScienceHardGATE CS (PYQ pattern)

A max-heap is built from [50, 30, 40, 10, 20, 35, 25, 5, 8, 15, 12] by bottom-up heapify. Two delete-max operations are then performed (each moves the last element to the root and sifts down). What is the resulting array and what is the number of swaps used by the two deletions?

  1. A. 35 30 25 10 20 12 15 5 8; 5 swaps
  2. B. 8 5 15 12 20 10 25 30 35; 4 swaps
  3. C. 35 30 25 20 15 12 10 8 5; 4 swaps
  4. D. 35 30 25 10 20 12 15 5 8; 4 swaps

Correct answer: D. 35 30 25 10 20 12 15 5 8; 4 swaps

The initial heap is 50 30 40 10 20 35 25 5 8 15 12. Each deletion replaces the root by the last leaf and sifts it down, picking the larger child each time.

More Programming and Data Structures (GATE) questions

Exams that ask this topic