Question
Consider a max heap, represented by the array:
| Array Index | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
| Value | 40 | 30 | 20 | 10 | 15 | 16 | 17 | 8 | 4 |
Now consider that a value is inserted into this heap. After insertion, the new heap is:
Official answer and solution
- Official answer
- B
- From
- Official GATE 2015 answer key
Solution
Simulate standard binary max-heap insertion at the end of the array followed by heapify-up (bubble-up).
The initial 1-indexed heap array is:
with size .
-
Append the new element at index :
-
Compare with its parent at ():
Since , swap and : -
Compare new with its parent at ():
Since , swap and : -
Compare new with its parent at ():
Since , the max-heap property is satisfied and insertion terminates.
The resulting array is:
This matches option B.
B
Stopping bubble-up after only one swap, failing to compare with the grandparent node at index 2.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2015 Computer Science and Information Technology (CS) Session 1
- Question
- 42 of 65, printed as CS Q.32
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.