GATE / CS ARCHIVE

GATE CSE 2015 SET 1 / QUESTION 37

GATE CSE 2015 Set 1 Q37

Data Structures · Heaps & Priority Queues · Full GATE CSE 2015 Set 1 paper

GATE 2015 Set 1, Q37 2 marks · MCQ
Report issue

Heaps & Priority Queues

Question 37, GATE 2015: Insert 35 into the max-heap array (indices 1–9) [40,30,20,10,15,16,17,8,4]. A [40,30,20,10,15,16,17,8,4,35]; B [40,35,20,10,30,16,17,8,4,15]; C [40,30,20,10,35,16,17,8,4,15]; D [40,35,20,10,15,16,17,8,4,30].
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step through it (5 steps)

Preserve the complete-tree shape by inserting at the next free index 10, then repair only its ancestor path. Its parent is floor(10/2)=5, holding 15, so swap. At index 5 the parent is 2, holding 30, so swap again. At index 2 the parent is 1, holding 40, so stop. Option C stops too early and violates 30≥35.

Solution: Append35 at index 10. It exceeds its parent15 at index 5, then 30 at index 2, but not 40 at the root. The array becomes 40, 35, 20, 10, 30, 16, 17, 8, 4, 15, option B.

Original paper, page 14 · Answer key