(a) Where is the minimum element located in a max-heap? How can you compute it, and what is the runtime? (b) Is an array that is sorted in decreasing order a max-heap? What about an array that is sorted in increasing order? (c) List all valid binary max-heaps that store the numbers 1, 2, 3, 4.
Expert Answer
Where is the minimum element located in a max-heap?How can we compute it and what is tuntime?
In the max-heap, we need to traverse each step both left and right sub-trees to search for the minimum element.To say simply we need to traverse alla the elements to find minimum element.
Therefore time complexity is O(n).
Is an array that is sorted in decreasing order a max-heap? What about an array that is sorted in increasing order?
Yes,There can an array that is sorted in decreasing order a max-heap. But it is overkill.That means generally we can heap an array in O(n), but it takes O(n log n) to sort.
Generally we will use a max-heap to sort in ascending order.
List all valid binary max-heqps that store numbers 1,2,3,4
A max-heap isnothing but a binary tree in which the value in each node is greater than or equal to the values of children.
Two max heaps only. They are:
max-heap is 4 3 2 1
4
/
3 2
/
1
other possible max-heap is 4 2 3 1
4
/
2 3
/
1