GATE CS 2014 · Set-2 · Arrays & Matrix Representation

MCQ · 1 mark · Problem PROG-DSTR-0153

A priority queue is implemented as a Max-Heap. Initially, it has 5 elements. The level-order traversal of the heap is: 10, 8, 5, 3, 2. Two new elements 1 and 7 are inserted into the heap in that order. The level-order traversal of the heap after the insertion of the elements is:
  1. A
    10, 8, 7, 3, 2, 1, 5
  2. B
    10, 8, 7, 2, 3, 1, 5
  3. C
    10, 8, 7, 1, 2, 3, 5
  4. D
    10, 8, 7, 5, 3, 2, 1

Solve it on GATE DA practice →More Basic Data Structures problems

Show the worked solution\[\boxed{\text{Insert at the next free position, then swap upwards while larger than the parent}}\]Start: \(10, 8, 5, 3, 2\). Insert \(1\) at position \(6\); its parent \(5\) is larger, so it stays: \(10, 8, 5, 3, 2, 1\).Insert \(7\) at position \(7\); its parent is \(5\), so they swap; the new parent \(10\) is larger, so it stops: \(10, 8, 7, 3, 2, 1, 5\).Answer: A

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com