Open In App

GATE | GATE-CS-2015 (Set 1) | Question 42

Consider a max heap, represented by the array: 40, 30, 20, 10, 15, 16, 17, 8, 4. Now consider that a value 35 is inserted into this heap. After insertion, the new heap is
(A) 40, 30, 20, 10, 15, 16, 17, 8, 4, 35
(B) 40, 35, 20, 10, 30, 16, 17, 8, 4, 15
(C) 40, 30, 20, 10, 35, 16, 17, 8, 4, 15
(D) 40, 35, 20, 10, 15, 16, 17, 8, 4, 30

Answer: (B)
Explanation: The array 40, 30, 20, 10, 15, 16, 17, 8, 4 represents following heap


         40
       /    \
     30      20
    / \      / \
   10  15  16   17
  / \
 8   4

After insertion of 35, we get



         40
       /    \
     30      20
    / \       / \
   10  15  16   17
  / \  /
 8   4 35  

After swapping 35 with 15 and swapping 35 again
with 30, we get

         40
       /    \
     35      20
    / \       / \
   10  30  16   17
  / \  /
 8   4 15  

Quiz of this Question



Article Tags :