The GATE Grind

GATE 2015 CS (CS3) – Question 30

Programming and Data Structures · Binary Heaps · 1 mark · Multiple choice

Find the minimum number of interchanges needed to convert the level-order array [89,19,50,17,12,15,2,5,7,11,6,9,100] into a max-heap.

  1. 4
  2. 5
  3. 2
  4. 3

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (D) 3

Explanation

Bottom-up repair swaps fifteen with one hundred at indices six and thirteen, then fifty with one hundred at indices three and six, then eighty-nine with one hundred at indices one and three. The result [100,19,89,17,12,50,2,5,7,11,6,9,15] is a heap. One hundred must move to the root and its ancestor violations require these three corrections; the minimum is three, D.

More Binary Heaps questions

All Binary Heaps questions →