edited by
31,979 views
79 votes
79 votes

In a min-heap with $n$ elements with the smallest element at the root, the $7^{th}$ smallest element can be found in time

  1. $\Theta (n \log n)$
  2. $\Theta (n)$
  3. $\Theta(\log n)$
  4. $\Theta(1)$
edited by

10 Answers

0 votes
0 votes

1st minimum will always at root of min heap. Hence 0 comparisons.

Now 2nd minimum can be child left child or right child of the root. Hence 1 comparison.

For the 3rd minimum we are assuming that 2nd min is the left child of root. So the 3rd minimum must be in the childs of 2nd min or right child of 1st min(root). Because if 3rd min is found other places than these nodes then min heap property will be violated (try yourself by some examples). So total comparison 2.

Now same way I am assuming the 3rd min is the left child of the 2nd min so the 4th min can be found in the the area in the picture below. So to find 4th min we have to compare 4 nodes means 3 comparisons.

You can try the remaining yourself.

1st min → 0 comp

2nd min → 1 comp

3rd min → 2 comp

4th min → 3 comp

5th min → 4 comp

………

……….

kth  min → k-1 comp

To finnd kth min we need k*(k-1)/2 comp.

So to find 7th min we need 7*(7-1)/2 = 21 comp.

So time needed $\Theta$(1).

Option: D 

 

–2 votes
–2 votes
The search of 7th smallest element is independent on the input size n so it takes constant time for every input n  O(n)
Answer:

Related questions

25 votes
25 votes
3 answers
4
Kathleen asked Sep 16, 2014
23,450 views
Suppose the numbers $7, 5, 1, 8, 3, 6, 0, 9, 4, 2$ are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering o...