GATE Computer ScienceVery hardGATE CS

A max-heap is stored in A[1..13] = [100, 60, 90, 50, 40, 85, 80, 20, 10, 30, 35, 70, 75]. The key at position 8 (the value 20) is deleted by moving the last element into position 8, and then the heap property is restored by sifting up or down as needed. What is the heap afterwards (positions 1 to 12)?

  1. A. [100, 60, 90, 75, 40, 85, 80, 50, 10, 30, 35, 70]
  2. B. [100, 60, 90, 50, 40, 85, 80, 75, 10, 30, 35, 70]
  3. C. [100, 60, 90, 50, 40, 85, 80, 10, 30, 35, 70, 75]
  4. D. [100, 75, 90, 60, 40, 85, 80, 50, 10, 30, 35, 70]

Correct answer: D. [100, 75, 90, 60, 40, 85, 80, 50, 10, 30, 35, 70]

The last element 75 goes to position 8; its parent (position 4) holds 50 < 75, so it must sift up: 75 swaps with 50 and then with 60 (position 2), stopping below 100. The value 60 ends at position 4 and 50 at position 8. Sifting down only would leave the heap invalid.

More Programming and Data Structures (GATE) questions

Exams that ask this topic