Data Structures · Heaps and Priority Queues
Priority Queue and the Heap Property
Overview
A priority queue is a collection from which you always want the most important element next: the smallest key, or the largest key. A binary heap is the standard way to build one.
It is a complete binary tree in which every node obeys one ordering rule, the heap property. This lesson fixes the vocabulary; the next lessons turn it into index arithmetic, insertion, deletion, building a heap and heap sort.
Key concepts
- Priority queue (ADT). A set of items, each with a key. Operations:
insert(x),find-max(orfind-min),extract-max(orextract-min, which finds and removes it). Optional:increase-key/decrease-key,delete. - Complete binary tree. Every level is full except possibly the last, and the last level is filled from the left with no gaps. Because of this shape, a heap with nodes is stored in an array with no pointers.
- Max-heap property. For every node other than the root, . The largest key is therefore at the root.
- Min-heap property. For every node other than the root, . The smallest key is at the root.
- The property relates a node only to its parent and children. It says nothing about two siblings, or about a node and its cousin. A heap is not sorted and is not a binary search tree.
- Duplicate keys are allowed; the property uses or , not strict inequality.
How it works
Two conditions must hold together for an array to be a heap:
- Shape: the tree drawn from the array in level order is complete (this is automatic: any array of items fills positions left to right).
- Order: at every node the heap property holds with respect to its children.
To check whether a given array is a max-heap, test every internal node: is it its left child and (if present) its right child? You do not compare anything else.
Is A = [50, 30, 40, 10, 20, 35, 25] a max-heap? (1-indexed)
index: 1 2 3 4 5 6 7
tree: 50
/ \
30 40
/ \ / \
10 20 35 25
50>=30,40 ok; 30>=10,20 ok; 40>=35,25 ok -> yes, a max-heap.
Formulas
- Max-heap: for all .
- Min-heap: for all .
- Priority queue operation costs on a binary heap with items:
find-max,insert,extract-max.
Worked examples
Example 1: is it a min-heap? .
- ✓. ✓. ✓. So it is a min-heap. Note (siblings) and : that is allowed, the property never compares siblings.
Example 2: find the violation. as a max-heap.
- Node 25 (index 2) has children 10 and 28. , so the property fails at index 2. The array is not a max-heap, even though the root is the maximum.
Example 3: heap is not a BST. In the max-heap the left child of the root is 30 and the right child is 40, but the right subtree also contains 35 and 25, which are smaller than 30. A BST would forbid that. So you cannot binary-search a heap.
Example 4: priority queue as a heap. Jobs with priorities 4, 9, 2 are inserted into a max-heap. extract-max returns 9 first, then 4, then 2: the order of priority, not the order of arrival.
Comparison
| Structure | find-max | insert | extract-max | sorted order | search a key |
|---|---|---|---|---|---|
| Unsorted array | no | ||||
| Sorted array | (remove from the end) | yes | |||
| Binary max-heap | no | ||||
| Balanced BST | yes (in-order) |
A heap is the best structure when you only ever need the extreme element repeatedly.
Revision summary
- Heap = complete binary tree + heap property. Either one alone is not enough.
- The property is parent-versus-child only. Siblings and cousins are unordered.
- The root of a max-heap is the maximum; the root of a min-heap is the minimum.
The minimum of a max-heap is somewhere among the leaves. - A heap does not support fast search, fast successor, or sorted traversal.
- A "max-priority queue" and a "min-priority queue" differ only in the direction of the comparison.
Previous-year exam insights
- A frequent question: "Which array is a max-heap?" Check every internal node against both children; one failing node rules the array out.
- Typical true/false traps: "a heap is a BST" (false); "the smallest element of a max-heap is a leaf" (true); "the second largest element of a max-heap is a child of the root" (true).
- A heap with nodes is a complete binary tree, so its height is (edges), which is why insertion and deletion are .