Reputation: 11
a) Will a Min-Heap form an array that is already sorted in ascending order? b) Will a Max-Heap form an array that is already sorted in descending order?
Upvotes: 0
Views: 227
Reputation: 11
No, value of each node will be less than or equal to its children. When it's stored as array, it may not be in ascending or descending order as the condition to be min heap is relative to parent child relationship (local) and will not guarantee increasing or decreasing order. By definition: https://www.geeksforgeeks.org/array-representation-of-binary-heap/ same goes for max heap
Upvotes: 1