Skip to content
Official PYQ · 2014

GATE CS 2014 Set 2 Q12 — Binary Heaps

Explain with AI

A priority queue is implemented as a Max-Heap. Initially, it has 55 elements. The level-order traversal of the heap is: 10,8,5,3,210, 8, 5, 3, 2. Two new elements 11 and 77 are inserted into the heap in that order. The level-order traversal of the heap after the insertion of the elements is: 

Choose one option
View worked solution

Answer: A

To find the level-order traversal after inserting the elements, we can trace the operations step-by-step on the Max-Heap.

Step 1: Initial Max-Heap

The initial level-order traversal is 10,8,5,3,210, 8, 5, 3, 2. Represented as a complete binary tree:

      10
     /  \
    8    5
   / \
  3   2

Step 2: Insert 11

We insert 11 at the next available position (the left child of 55):

      10
     /  \
    8    5
   / \  /
  3   2 1

Since the parent node 5≥15 \ge 1, the max-heap property is satisfied. No swaps are needed.

Step 3: Insert 77

We insert 77 at the next available position (the right child of 55):

      10
     /  \
    8    5
   / \  / \
  3   2 1  7

Since the parent node 5<75 < 7, we swap 77 with 55 (heapify-up):

      10
     /  \
    8    7
   / \  / \
  3   2 1  5

Now, we compare 77 with its new parent 1010. Since 10≥710 \ge 7, the heapify-up process terminates.

Conclusion

The final level-order traversal of the heap is obtained by reading the nodes level-by-level from left to right:

10,8,7,3,2,1,5\mathbf{10, 8, 7, 3, 2, 1, 5}

This matches Option A.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Binary Heaps questions