GATE / CS ARCHIVE

GATE CSE 2014 SET 2 / QUESTION 12

GATE CSE 2014 Set 2 Q12

Data Structures · Heaps & Priority Queues · Full GATE CSE 2014 Set 2 paper

GATE 2014 Set 2, Q12 1 mark · MCQ
Report issue

Heaps & Priority Queues

Question 12, GATE 2014: A max-heap initially has level order 10,8,5,3,2. Insert 1 and then 7. What is the new level order? A 10,8,7,3,2,1,5; B 10,8,7,2,3,1,5; C 10,8,7,1,2,3,5; D 10,8,7,5,3,2,1.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Step through it (5 steps)

1. Set up the reasoning. Represent the heap as a one-based array. Append each item at the first available position to preserve the complete-tree shape, then compare it only with its ancestors while restoring max-heap order.

2. Derive the result. Appending1 puts it below 5 and requires no swap. Appending7 puts it below 5, so exchange 7 and 5; 7 is below root10 and stops. The resulting level order is 10, 8, 7, 3, 2, 1, 5, A.

3. Check the interpretation. Heap insertion does not sort an entire level. Inserting 7 affects the path through its parent 5; it does not exchange the existing siblings 3 and 2. This distinguishes A from other arrays that might still satisfy some heap inequalities.

Original paper, page 32 · Answer key