GATE | GATE IT 2006 | Question 44

Which of the following sequences of array elements forms a heap?
(A) {23, 17, 14, 6, 13, 10, 1, 12, 7, 5}
(B) {23, 17, 14, 6, 13, 10, 1, 5, 7, 12}
(C) {23, 17, 14, 7, 13, 10, 1, 5, 6, 12}
(D) {23, 17, 14, 7, 13, 10, 1, 12, 5, 7}


Answer: (C)

Explanation:  
When they are asking for heap, by default it’s max heap.

Basic Requirement: Array representation of binary tree

Starting from basics lets first understand heap trees We have 2 types of heap – Min heap and Max heap

In Min heap the parent is always smaller than its children and in Max heap parent is always greater than its children.

heap



Looking at the options we can tell that which tree is Max heap tree. Now consider each option one by one and draw a tree

max-heap

From options it is clear that only option C satisfies the Max heap tree property.

This explanation has been contributed by Parul Sharma.

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.