recategorized by
5,010 views
6 votes
6 votes
In a min-heap, the next largest element of a particular element can be found in ___ time.

A) O(1)

B) O(log n)

C) O(n)
recategorized by

2 Answers

Best answer
8 votes
8 votes

The key thing to notice here is : we are using binary heap instead of BST as the concerned data structure .

And we know :

The only requirement in binary heap is that the tree should be almost complete binary tree and parent value should be less than both left and right children values. However there is no relation left and right children as in the case of BST we have ..Thus ordering among the same level nodes dont matter here as long as the min heap property is satisfied w.r.t. their parent nodes.

So say we are considering the leaf nodes of a tree of height 3. So we can have 8 nodes in leaf level.And let all leaf nodes are occupied ..Let the key values be distributed such that we have key values : [8 - 15] in the leaf level..

So now wht can happen in worst case is that 8 is in the left most extreme and  9 in the rightmost extreme as we can have such valid minheap.The node having 8 can be in the next to rightmost extreme and 9 in the rightmost extreme . So any permutation amongst the key values of a level in the tree is possible given that keys for a specific level has been specified to form minheap.

Thus in worst case we may end up searching all elements in the level . In last level we have (n/2) nodes maximum . So maximum of (n/2 - 1) nodes are to be compared for next maximum.

Hence O(n) should be required complexity.

selected by
0 votes
0 votes

In heap we can do it by built heap method i.e. O(n).

  • Now, as it is a particular element, I think pointer is not given, in that case search the whole tree take O(n).
  • When node is found find it's parent or we can say it's root, in in O(1) time by index.  Suppose node is x[i], then finding root will be
  • if(i%2==0) parent=x[i/2];
    else parent=x[(i-1)/2];
  • When root is found, delete root and apply heapify with build heap precedure O(n)
  • Compare root with left child and right child in O(1) time
  • Among left and right child , which one is minimum will be the ans(As it is a min heap)

Here complexity O(n)+O(n)=O(n)

edited by
Answer:

Related questions

0 votes
0 votes
1 answer
1
saurav raghaw asked Dec 22, 2018
695 views
The time complexity of the most efficient algorithm to determine whether an arbitrary array of size ‘n’, is min-heap or not?(A) O(log n)(B) O(n)(C) O(n logn)(D) O(1)
1 votes
1 votes
1 answer
2
gmrishikumar asked Dec 1, 2018
2,311 views
What is the time complexity to find the Kth largest element in a Min-Heap? Or equivalently, What is the time complexity to find Kth smallest element in Max-Heap?
8 votes
8 votes
3 answers
3
Kapil asked Sep 4, 2016
3,983 views
In a min-heap with n elements1). The 7th smallest element can be found in time, if duplicates are allowed ?2). The 7th distinct smallest element can be found in time, I...
11 votes
11 votes
5 answers
4
Vikrant Singh asked Dec 28, 2014
3,711 views
What is the complexity of finding $50^{th}$ smallest element in an already constructed binary min-heap?$\Theta(1)$$\Theta (\log n)$$\Theta (n)$$\Theta (n \log n)$