GATE / CS ARCHIVE

GATE CSE 2005 / QUESTION 34

Inserting into a max heap

Data Structures · Heaps & Priority Queues · Full GATE CSE 2005 paper

GATE 2005, Q34 2 marks · MCQ
Report issue

Heaps & Priority Queues

Question 34, GATE 2005: Max-heap level order 10, 8, 5, 3, 2 receives 1 then 7. New level order? A10, 8, 7, 5, 3, 2, 1; B10, 8, 7, 2, 3, 1, 5; C10, 8, 7, 1, 2, 3, 5; D10, 8, 7, 3, 2, 1, 5.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (5 steps)

Append 1 as the sixth element; its parent 5 is larger, so no swap occurs. Append 7 as the seventh element; swap it with parent 5, but stop below root 10. The resulting level order is 10, 8, 7, 3, 2, 1, 5, D.

Appending 1 puts it under parent 5 and requires no upward swap. Appending 7 puts it as the other child of 5;7 exceeds 5, so swap them. Its new parent is 10, which is larger, ending the repair. Other nodes remain where they were, yielding 10, 8, 7, 3, 2, 1, 5. A heap is not a sorted array; sorting the level order would unnecessarily rearrange nodes.

Original paper, page 4