GATE Computer ScienceModerateGATE CS

The keys 3, 9, 6, 12, 1, 15, 8 are inserted one after another into an initially empty max-heap (each insertion places the key at the end and sifts it up). The array representation of the final heap is:

  1. A. 15 9 12 3 1 6 8
  2. B. 15 12 9 3 1 6 8
  3. C. 15 9 12 1 3 6 8
  4. D. 15 12 8 9 1 3 6

Correct answer: A. 15 9 12 3 1 6 8

Insert 12: it swaps with 3 and then with 9, so the array is 12 9 6 3. Insert 1 stays at index 4. Insert 15 swaps with 6 and then 12, giving 15 9 12 3 1 6. Finally 8 sits at index 6 below 12, so no swap occurs.

More Programming and Data Structures (GATE) questions

Exams that ask this topic