Skip to content
Official PYQ · 2005

GATE CS 2005 Q34 — 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: D

To find the level-order traversal after inserting the elements, we perform the standard binary heap insertion operations step-by-step.

Step 1: Initial Max-Heap

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

       10
      /  \
     8    5
    / \
   3   2

Step 2: Insert 1

  1. Insert 1 at the next available position in the complete binary tree (as the left child of 5):
           10
          /  \
         8    5
        / \  /
       3   2 1
      
  2. Compare 1 with its parent 5. Since 1≤51 \le 5, the max-heap property is satisfied, and no swap is required.

Step 3: Insert 7

  1. Insert 7 at the next available position (as the right child of 5):
           10
          /  \
         8    5
        / \  / \
       3   2 1  7
      
  2. Compare 7 with its parent 5. Since 7>57 > 5, we swap them to restore the max-heap property:
           10
          /  \
         8    7
        / \  / \
       3   2 1  5
      
  3. Compare 7 with its new parent 10. Since 7≤107 \le 10, the heap property is satisfied, and no further swaps are needed.

Conclusion

The final level-order traversal of the heap is obtained by reading the tree 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 D.

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

Published
Report a correction
Keep exploringMore Binary Heaps questions