Skip to main content

GATE CSE 2015Data Structures › Heaps & Priority QueuesVerified

GATE CSE 2015 PYQ: Consider a max heap, represented by the array: 40 , 30 , 20 , 10 , 15 , 16 , 17 , 8 , 4 40, 30, 20, 10, 15,…

Consider a max heap, represented by the array: 40,30,20,10,15,16,17,8,440, 30, 20, 10, 15, 16, 17, 8, 4.

Array index123456789Value4030201015161784\begin{array}{|l|l|}\hline \text{Array index} & \text{1} & \text{2} & \text{3} & \text{4} & \text{5} & \text{6} & \text{7} & \text{8} & \text{9} \\\hline \text{Value} & \text{40} & \text{30} & \text{20} & \text{10} &\text{15} & \text{16} & \text{17} & \text{8} & \text{4} \\\hline \end{array}

Now consider that a value 35 is inserted into this heap. After insertion, the new heap is

  1. A
    40,30,20,10,15,16,17,8,4,3540, 30, 20, 10, 15, 16, 17, 8, 4, 35
  2. B
    40,35,20,10,30,16,17,8,4,1540, 35, 20, 10, 30, 16, 17, 8, 4, 15
    Correct answer
  3. C
    40,30,20,10,35,16,17,8,4,1540, 30, 20, 10, 35, 16, 17, 8, 4, 15
  4. D
    40,35,20,10,15,16,17,8,4,3040, 35, 20, 10, 15, 16, 17, 8, 4, 30

Answer

(B) 40 , 35 , 20 , 10 , 30 , 16 , 17 , 8 , 4 , 15 40, 35, 20, 10, 30, 16, 17, 8, 4, 15 40 , 35 , 20 , 10 , 30 , 16 , 17 , 8 , 4 , 15

Approach

Insert 35 at the next free position (index 10) and perform the standard max-heap insert operation: repeatedly compare it with its parent and swap upward while it is larger than the parent.

Inserting a value into a max-heap requires appending it and then sifting it upward until the parent is at least as large. The value 35 rises from index 10 through index 5 to index 2, producing the array in option B.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.