# Heap

Please wait while the activity loads.

If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled.

If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled.

Question 1 |

What is the time complexity of Build Heap operation. Build Heap is used to build a max(or min) binary heap from a given array. Build Heap is used in Heap Sort as a first step for sorting.

O(nLogn) | |

O(n^2) | |

O(Logn) | |

O(n) |

**Heap**

**Discuss it**

Question 1 Explanation:

Following is algorithm for building a Heap of an input array A.

BUILD-HEAP(A) heapsize := size(A); for i := floor(heapsize/2) downto 1 do HEAPIFY(A, i); end for ENDAlthough the worst case complexity looks like O(nLogn), upper bound of time complexity is O(n). See following links for the proof of time complexity. http://www.dgp.utoronto.ca/people/JamesStewart/378notes/08buildheap/

Question 2 |

Suppose we are sorting an array of eight integers using heapsort, and we have just finished some heapify (either maxheapify or minheapify) operations. The array now looks like this:
16 14 15 10 12 27 28
How many heapify operations have been performed on root of heap?

1 | |

2 | |

3 or 4 | |

5 or 6 |

**Sorting**

**Heap**

**HeapSort**

**Discuss it**

Question 2 Explanation:

In Heapsort, we first build a heap, then we do following operations till the heap size becomes 1.
a) Swap the root with last element
b) Call heapify for root
c) reduce the heap size by 1.
In this question, it is given that heapify has been called few times and we see that last two elements in given array are the 2 maximum elements in array. So situation is clear, it is maxheapify whic has been called 2 times.

Question 3 |

A max-heap is a heap where the value of each parent is greater than or equal to the values of its children. Which of the following is a max-heap? (GATE CS 2011)

A | |

B | |

C | |

D |

**Heap**

**Discuss it**

Question 3 Explanation:

See http://www.geeksforgeeks.org/data-structures-and-algorithms-set-26/ for explanation.

Question 4 |

A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. A 3-ary heap can be represented by an array as follows: The root is stored in the first location, a[0], nodes in the next level, from left to right, is stored from a[1] to a[3]. The nodes from the second level of the tree from left to right are stored from a[4] location onward. An item x can be inserted into a 3-ary heap containing n items by placing x in the location a[n] and pushing it up the tree to satisfy the heap property.
Which one of the following is a valid sequence of elements in an array representing 3-ary max heap?

1, 3, 5, 6, 8, 9 | |

9, 6, 3, 1, 8, 5 | |

9, 3, 6, 8, 5, 1 | |

9, 5, 6, 8, 3, 1 |

**Heap**

**Discuss it**

Question 4 Explanation:

Following 3-ary Max Heap can be constructed from sequence given option (D)

9 / | \ / | \ 5 6 8 / | / | 3 1

Question 5 |

Suppose the elements 7, 2, 10 and 4 are inserted, in that order, into the valid 3- ary max heap found in the above question, Which one of the following is the sequence of items in the array representing the resultant heap?

10, 7, 9, 8, 3, 1, 5, 2, 6, 4 | |

10, 9, 8, 7, 6, 5, 4, 3, 2, 1 | |

10, 9, 4, 5, 7, 6, 8, 2, 1, 3 | |

10, 8, 6, 9, 7, 2, 3, 4, 1, 5 |

**Heap**

**Discuss it**

Question 5 Explanation:

After insertion of 7

9 / | \ / | \ 7 6 8 / | \ / | \ 3 1 5After insertion of 2

9 / | \ / | \ 7 6 8 / | \ / / | \ / 3 1 5 2After insertion of 10

10 / | \ / | \ 7 9 8 / | \ / | / | \ / | 3 1 5 2 6After insertion of 4

10 / | \ / | \ 7 9 8 / | \ / | \ / | \ / | \ 3 1 5 2 6 4

Question 6 |

Consider a binary max-heap implemented using an array. Which one of the following array represents a binary max-heap? (GATE CS 2009)

25,12,16,13,10,8,14 | |

25,12,16,13,10,8,14 | |

25,14,16,13,10,8,12 | |

25,14,12,13,10,8,16 |

**Heap**

**Discuss it**

Question 6 Explanation:

A tree is max-heap if data at every node in the tree is greater than or equal to it’s children’ s data.
In array representation of heap tree, a node at index i has its left child at index 2i + 1 and right child at index 2i + 2.

25 / \ / \ 14 16 / \ / \ / \ / \ 13 10 8 12

Question 7 |

What is the content of the array after two delete operations on the correct answer to the previous question?

14,13,12,10,8 | |

14,12,13,8,10 | |

14,13,8,12,10 | |

14,13,12,8,10 |

**Heap**

**Discuss it**

Question 7 Explanation:

For Heap trees, deletion of a node includes following two operations.
1) Replace the root with last element on the last level.
2) Starting from root, heapify the complete tree from top to bottom..
Let us delete the two nodes one by one:
1) Deletion of 25:
Replace 25 with 12

12 / \ / \ 14 16 / \ / / \ / 13 10 8Since heap property is violated for root (16 is greater than 12), make 16 as root of the tree.

16 / \ / \ 14 12 / \ / / \ / 13 10 82) Deletion of 16: Replace 16 with 8

8 / \ / \ 14 12 / \ / \ 13 10Heapify from root to bottom.

14 / \ / \ 8 12 / \ / \ 13 10 14 / \ / \ 13 12 / \ / \ 8 10

Question 8 |

We have a binary heap on n elements and wish to insert n more elements (not necessarily one after another) into this heap. The total time required for this is
(A) (logn)
(B) (n)
(C) (nlogn)
(D) (n^2)

A | |

B | |

C | |

D |

**Heap**

**Discuss it**

Question 8 Explanation:

We can reduce the problem to Build Heap for 2n elements. Time taken for build heap is O(n)

Question 9 |

In a min-heap with n elements with the smallest element at the root, the 7th smallest element can be found in time
a) (n log n)
b) (n)
c) (log n)
d) (1)
The question was not clear in original GATE exam. For clarity, assume that there are no duplicates in Min-Heap and accessing heap elements below root is allowed.

a | |

b | |

c | |

d |

**Heap**

**Discuss it**

Question 9 Explanation:

The 7th smallest element must be in first 7 levels. Total number of nodes in any Binary Heap in first 7 levels is at most 1 + 2 + 4 + 8 + 16 + 32 + 64 which is a constant. Therefore we can always find 7th smallest element in time.
If Min-Heap is allowed to have duplicates, then time complexity becomes Θ(Log n).
Also, if Min-Heap doesn't allow directly accessing elements below root and supports only extract-min() operation, then also time complexity becomes Θ(Log n).

Question 10 |

In a binary max heap containing n numbers, the smallest element can be found in time (GATE CS 2006)

0(n) | |

O(logn) | |

0(loglogn) | |

0(1) |

**Heap**

**Discuss it**

Question 10 Explanation:

In a max heap, the smallest element is always present at a leaf node. So we need to check for all leaf nodes for the minimum value. Worst case complexity will be O(n)

12 / \ / \ 8 7 / \ / \ / \ / \ 2 3 4 5

There are 26 questions to complete.