retagged by
30,553 views
64 64 votes

In a binary max heap containing $n$ numbers, the smallest element can be found in time 

  1.  $O(n)$
  2.  $O(\log n)$
  3.  $O(\log \log n)$
  4.  $O(1)$

6 Answers

Best answer
68 68 votes
  1. $O(n)$

    In a max heap, the smallest element is always present at a leaf node. Heap being a complete binary tree, there can be up to $\frac{n}{2}$ leaf nodes and to examine all of them we would need $O(n)$ time.
edited by
7 7 votes

The smallest element in a max heap would always be in the last level. => Search all leafs.

No. of leafs = No. of internal nodes + 1.

In an asymptotic sense, we can say No. of leafs = No. of internal nodes. If total nodes = n, leafs = $O(\frac{n}{2})$ = Internal nodes.

And, $O(\frac{n}{2}) = O(n)$

So, Option A.

5 5 votes
Convert the heap into array and do linear search that comes O(n).
1 1 vote

🔍 Where’s the Smallest Element?

  • In a complete binary tree (which a heap is), at least half the nodes are leaves.

  • So the smallest element is guaranteed to be among the last ⌊n/2⌋ nodes.

  • You have no ordering among leaves, so you must scan them all.

✅ Correct Answer: A. O(n)

  • You need to check all leaves to find the smallest.

  • That’s Θ(n) in the worst case, because you might have to inspect every leaf.

0 0 votes

In a max heap, minimum value will be at the leaf nodes. Hence we will have to run a for loop from n/2 to n and check sequentially.

The time complexity of one for loop from n/2 to n will be O(n).

Hence option A. 

0 0 votes
In n element max heap, smallest element is one of the leaf.. in array representation of max heap, leaves index start from floor(n/2)+1 to n. And there will be max n/2 elements present at last level (height 0).

 

Just do a for loop from floor(n/2)+1 to n

In which keep track of smallest element

There will be O((n/2)-1) = O(n) comparisons ( we need only 1 element to find)..

Answer =A
Answer:
Position:
Show:

Related questions

31 31 votes
2 answers 2 answers
12.5k
12.5k views
Arjun asked Nov 27, 2016
12,511 views
Statement for Linked Answer Questions 76 & 77: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 re...
40 40 votes
3 answers 3 answers
7.9k
7.9k views
Rucha Shelke asked Sep 26, 2014
7,906 views
Statement for Linked Answer Questions 76 & 77: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 re...
52 52 votes
3 answers 3 answers
14.1k
14.1k views
Ishrat Jahan asked Nov 1, 2014
14,085 views
An array $X$ of $n$ distinct integers is interpreted as a complete binary tree. The index of the first element of the array is $0$. If only the root node does not satisfy...
25 25 votes
1 answers 1 answer
9.6k
9.6k views
Ishrat Jahan asked Oct 31, 2014
9,594 views
Which of the following sequences of array elements forms a heap?$\{23, 17, 14, 6, 13, 10, 1, 12, 7, 5\}$$\{23, 17, 14, 6, 13, 10, 1, 5, 7, 12\}$$\{23, 17, 14, 7, 13, 10, ...