In a min-heap with elements with the smallest element at the root, the smallest element can be found in time
View worked solution
Answer: D
In a min-heap, the smallest element is always at the root (level 0). The second smallest element must be one of the children of the root (level 1).
More generally, the smallest element in a min-heap must reside within the first levels of the heap. This is because any path from the root to a node is in non-decreasing order, so an element cannot be smaller than its ancestors.
For :
- The smallest element must be located within the top levels of the min-heap.
- The maximum number of nodes in the first levels of a binary tree is .
- Since is a constant that does not depend on the total number of elements , we only need to search a constant number of elements to find the smallest element.
Therefore, the time complexity to find the smallest element is .
Free practice · Your answers on this page are not saved.
