## Heapify(): maintain the heap property

- Given: a node i in the heap with children l and r
- Given: two subtrees rooted at l and r, assumed to be heaps
- Action: let the value of the parent node “float down” so subtree at i satisfies the heap property
- If A[i] < A[l] or A[i] < A[r], swap A[i] with the largest of A[l] and A[r]
- Recurse on that subtree

- Running time: O(h), h = height of heap = O(lg n)

Previous slide | Next slide | Back to first slide | View graphic version |