Search code examples
heap

Is a sorted array a min-heap? What's the minimum value of a max-heap?


I have studied min-heaps and max-heaps, and I have a couple of questions:

  1. Is a sorted array a min-heap?
  2. What is the minimum value of a max-heap?

Solution

  • An array sorted from lowest to highest is a min-heap when using the array-based heap implementation. The min-heap property that the parent node value is less than or equal to it's child nodes (2i + 1 and 2i + 2, using zero-based arrays) holds for all nodes that have children.

    The minimum value of a max heap is in one of the leaf nodes, but you don't know which. Since the minimum node cannot, by definition, have any child nodes, it must be a leaf. The heap property, however, does not specify how leaf nodes compare with each other, only with their parent.