Sub topic 3
The Max-Heap Property
This chapter assumes you know Python loops and functions and zero-based indexing. It also assumes the index formulas from the previous chapter: for a node at index i, the parent is at (i - 1) // 2, and the children are at 2*i + 1 and 2*i + 2.
What the Property Says
A max-heap is a complete binary tree in which every node's value is greater than or equal to the values of its children. You only ever compare a parent with its own children. Nothing else needs to hold.
Two facts follow from this local rule:
- The maximum sits at the root. Values can only stay the same or decrease as you walk down any path, so no node can beat the root. Index
0always holds the largest element. - Every subtree is also a max-heap. The rule applies at every node, so any node together with everything below it forms a valid heap. Sift-down, covered in the next chapter, depends on this.
A min-heap reverses the comparison: every parent is less than or equal to its children, and the smallest value sits at the root. Heap sort as taught here uses a max-heap.
A Heap Is Not a Sorted Array
The rule says nothing about siblings, and nothing about nodes on different branches. Consider the array [9, 7, 8, 3, 5, 6]:
This is a valid max-heap, but the array is not sorted. The value 6 at index 5 is larger than 3 and 5, which sit earlier in the array. The heap guarantees partial order only. That is why it costs just O(n) to build, and why sorting still needs more work afterward.
| Structure | Guarantee | Where the maximum is |
|---|---|---|
| Sorted array (ascending) | Every element ≤ the next | Last index |
| Max-heap | Every parent ≥ its children | Index 0 |
| Arbitrary array | None | Anywhere |
Checking the Property in Code
Every element except the root has exactly one parent. So you can check the whole heap by asking one question of each non-root element: is it larger than its parent?
def is_max_heap(a):
n = len(a)
for i in range(1, n):
if a[(i - 1) // 2] < a[i]:
return False
return True
print(is_max_heap([9, 7, 8, 3, 5, 6])) # True
print(is_max_heap([3, 7, 8])) # FalseThe check runs in O(n) time and uses O(1) extra space. Empty and single-element arrays pass trivially, which is correct: the loop body never runs.
Common Errors
- Using
i // 2for the parent. That formula belongs to one-indexed heaps. With zero-based arrays it is wrong:i = 2would map to index 1, its sibling, when its real parent is index 0. - Rejecting duplicates. The property uses "greater than or equal". If you write
a[parent] <= a[i]as the failure test, the check wrongly rejects valid heaps such as[5, 5, 5]. - Checking only the root. A large root does not prove the rest of the tree is valid.
[9, 1, 2, 5]has the largest value at the root, but 5 is a child of 1. - Expecting siblings to be ordered. In a valid heap, either child may be the larger one.
Exercises
Exercise
Is [10, 9, 3, 8, 9, 2, 1] a valid max-heap? Check each element against its parent.
Show answer
Yes. Index 1 (9) and index 2 (3) have parent 10. Index 3 (8) and index 4 (9) have parent 9; the 9 at index 4 equals its parent, which is allowed. Index 5 (2) and index 6 (1) have parent 3. No child exceeds its parent.
Exercise
In a max-heap of distinct values, where can the smallest element be?
Show answer
Only in a leaf. Any internal node has at least one child, and that child must be smaller, so the internal node cannot be the minimum. For a heap of n elements, the leaves are at indices n // 2 through n - 1.
Exercise
Modify ismaxheap so it returns the index of the first element that is larger than its parent, or -1 if the array is a valid max-heap.
Show answer
def first_violation(a): for i in range(1, len(a)): if a[(i - 1) // 2] < a[i]: return i return -1 For [9, 1, 2, 5] it returns 3, because 5 is larger than its parent 1 at index 1.