• edited by
52,183 views
144 144 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)$

11 Answers

2 2 votes

In a min-heap, smaller numbers stay near the top, and larger numbers sit below them.<!--TgQPHd|||[]-->

  • The 1st smallest element is always at Level 1 (the root)
  • The 2nd smallest element must be right below it, at Level 2
  • The 3rd smallest element will be nearby, at Level 2 or Level 3

Following this pattern, to find the 7th smallest element, you never have to look deeper than Level 7

So the number of nodes from level 1 to level 7 remain constant so the answer is \(\Theta(1)\)

0 0 votes
The question is quite ambiguous.

If the question means

i) it's a min-heap then answer would be O(1), because at worst case we need to check the roots at depth 7, and as we know asymptotic complexity tells the order of growth of time wrt 'n' or input size, here irrespective of n, if it's greater than 7 we only need to check at depth 7 at worst case as I said, even if n is 10, 100, 1000 and so on. So, it's not depending on 'n'.

ii) it's just a binary tree with the least element at the root of the tree then T would be O(nlogn) because at worst case we need to search the entire tree.
0 0 votes

All the answers above are correct and very clear but if you have any confusion, and would like a programming approach,

Think of how you can write a program for it. For example, In a min heap, you know that the 3rd  smallest element would be located at left or right of the root so you can hardcode a function like

min(root->left, root->right)

Similarly you can also hardcode a function for 7th smallest element because YOU KNOW before hand that it is located among the first 7 levels and write a function that runs over some say k elements and finds a minimum.

No matter the size of N, a 1000 elements or a billion elements in that min heap, your code always takes K(constant) amount of time and doesn’t grow proportionally to N.

So answer is Θ(1)

0 0 votes

Answer is option B,​​​​​​

Explanation:-

We know in binary heap the minimum element is always at the root position

Time to get minimum element is O(1).

And TC of deleting min and forming min heap again is O(logn)

Therefore, for finding 7th smallest element we have to delete element from heap 7 times.

Therefore, TC = O(7logn) = O(logn)

–2 –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:
Position:
Show:

Related questions

61 61 votes
5 answers 5 answers
14.7k
14.7k views
Kathleen asked Sep 17, 2014
14,685 views
A program consists of two modules executed sequentially. Let $f_1(t)$ and $f_2(t)$ respectively denote the probability density functions of time taken to execute the two ...
80 80 votes
10 answers 10 answers
31.9k
31.9k views
Kathleen asked Sep 17, 2014
31,870 views
Consider the function $f$ defined below.struct item { int data; struct item * next; }; int f(struct item *p) { return ((p == NULL) || (p->next == NULL)|| ((p->data <= p -...
138 138 votes
17 answers 17 answers
48.9k
48.9k views
Kathleen asked Sep 17, 2014
48,909 views
Let S be a stack of size $n \geq1$. Starting with the empty stack, suppose we push the first n natural numbers in sequence, and then perform $n$ pop operations. Assume th...
94 94 votes
4 answers 4 answers
31.5k
31.5k views
Kathleen asked Sep 17, 2014
31,529 views
A data structure is required for storing a set of integers such that each of the following operations can be done in $O(\log n)$ time, where $n$ is the number of elements...