Data Structures | Heap | Question 8

4

In a min-heap with n elements with the smallest element at the root, the 7th smallest element can be found in time
a) \theta(n log n)
b) \theta(n)
c) \theta(log n)
d) \theta(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) a
(B) b
(C) c
(D) d


Answer: (D)

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 \theta(1) 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).

Quiz of this Question

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



4 Average Difficulty : 4/5.0
Based on 1 vote(s)