Let H be a primary min-heap consisting of n elements implemented as an array. What is the worst case time complexity of an optimal algorithm to find the maximum element in H?
Let H be a primary min-heap consisting of n elements implemented as an array. What is the worst case time complexity of an optimal algorithm to find the maximum element in H? Correct Answer θ(n)
Key Points
- A min-heap is a complete binary tree data structure in which the parent node has a value smaller than its children node.
- In a min-heap, leaf nodes or the last level contains the maximum values of the array.
Explanation:
If we use the brute force technique, it will take O(n) time to find the largest element in a binary min-heap.
If we use the property of min-heap:
The min-heap property requires that the parent node be lesser than its child node(s). Due to this, we can conclude that a non-leaf node cannot be the maximum element as its child node has a lower value.
So we can narrow down our search space to only leaf nodes. In a min-heap having n elements, there is ceil(n/2) leaf nodes. The time and space complexity remains O(n) as a constant factor of 1/2 does not affect the asymptotic complexity.
Additional Information
In a min-heap, minimum element takes θ(1).