Open In App

GATE | GATE-CS-2004 | Question 37

Like Article
Like
Save
Share
Report

The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a Max Heap. The resultant Max Heap is.

tree
(A) a
(B) b
(C) c
(D) d


Answer: (A)

Explanation: A max heap is a complete binary tree in which the value of each non-leaf node is greater than or equal to the values of its children.

For the given case, first insert all the values in a complete binary tree. Then, we apply shifting. What we do is we start with the bottom most non-leaf node. If it is smaller than any (or both) of the child nodes, we swap it with the largest child. Doing the same way, we continue to move upwards the tree until all the non-leaf nodes satisfy the properties of the max heap.

 

So, the first time we make a complete binary tree, we have


             32

       /            \

     15              20

   /    \          /     \

 30      12       25      16

 

Now, we need to swap 15 with 30 and 20 with 25.

 


             32

       /            \

     30              25

   /    \          /     \

 15      12       20      16

 

This is the required max heap, and matches with option A.

So, A is the correct choice.

 

Please comment below if you find anything wrong in the above post.


Quiz of this Question


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads