Suppose a repair desk must work on the most urgent request next. A normal queue keeps arrival order. A priority queue instead lets us take the request with the highest priority. If requests keep arriving, sorting the whole list after every arrival wastes work. A heap gives us another choice.
Background for this page. A tree connects parent and child nodes without cycles. A heap stores a nearly complete tree and imposes an ordering rule between each parent and its children. Related explanations: Arrays, linked lists, stacks, and queues · Insertion sort: a worked example and its cost.
A max-heap is a nearly complete binary tree in which every parent is at least as large as either child. The root is therefore a maximum. The rest of the tree is not fully sorted: a large value in one branch tells us little about the other branch. This limited promise is what makes updates efficient.
We can store the tree in an array without storing pointers. For a zero-based array, the children of position i are at 2i + 1 and 2i + 2. The parent of a non-root position is at floor((i - 1)/2). For [9, 7, 8, 2, 4, 3], the root 9 has children 7 and 8; each of those parents is also at least as large as its children. The heap property holds even though the array is not sorted.
The tree and array show the same six values. The parent-child position formula lets the array stand in for the links in the picture.
What changes when an item moves
To add an item, place it at the end and move it upward while it is larger than its parent. To remove the maximum, take the root, move the last item to the root, and move it downward until the heap property holds again. The tree’s height is Θ(log n), so either repair takes O(log n) time. Looking at the maximum takes O(1). Building a heap from an existing array can be done in Θ(n) time by repairing parents from the bottom upward; building it through n separate insertions gives a weaker O(n log n) bound.
Heapsort first builds a max-heap. It then swaps the root with the last item in the active heap, shrinks that heap, and repairs the root. The largest item is now in its final place. Repeat until all items are placed. Its time is Θ(n log n) even on a bad input order, and an array implementation can sort with O(1) extra array space. Ordinary heapsort is not stable: equal-key records may change their earlier order.
# Start with an array that has the max-heap property.
while heap_size is greater than 1:
# Put the current maximum at the end.
swap the root with the last active item
# Leave that maximum outside the active heap.
reduce heap_size by 1
# Restore the property from the new root downward.
sift down the root within the active heap
This is a useful distinction: a heap is good at repeatedly finding the next highest-priority item, but it is not a fast way to find an arbitrary item by value. Choose it for the operation it promises.
Remove a maximum step by step
From [9, 7, 8, 2, 4, 3], save 9, move the last item 3 to the root, and shorten the array to [3, 7, 8, 2, 4]. Swap 3 with its larger child 8, giving [8, 7, 3, 2, 4]. Repair stops because 3 has no children. Choosing the larger child keeps the new parent above both children. Other branches keep their previous relationships. Empty removal needs an explicit policy, and equal priorities need a tie breaker if arrival order matters.
Practice. Insert 10 into the heap [9, 7, 8, 2, 4, 3]. Where does it begin, and what swaps restore the heap property?
Answer. It begins at the end: [9, 7, 8, 2, 4, 3, 10]. It swaps with its parent 8, then with 9, giving [10, 7, 9, 2, 4, 3, 8]. Each parent is now at least as large as its children.
Try another case. Can siblings 4 and 8 occur under parent 9?
Reasoning. Yes. A max-heap orders parents against their children. Siblings need no ordering relation.