Chapter 13: Priority Queues and Heaps
Introduction
Learning Outcomes
Define priority queues and max/min heaps; tell a heap apart from a binary search tree.
Store a complete binary tree in a 1-based array: parent i/2, children 2i, 2i+1.
Trace insert_max_heap and delete_max_heap step by step.
Show why insert and delete are O(lg n) and why the best case is Ω(1).
Compare heaps with unsorted/sorted arrays and lists using a cost table.
Explain why bottom-up heap construction (heapify) is O(n); meet the d-ary heap.
Priority Queues
Priority queue: the highest-priority item leaves first
The item removed first is chosen not by arrival order but by the highest or lowest value.
- Max priority queue: the highest value is served first.
- Min priority queue: the lowest value is served first.
Priority Queues
Why do we need priority queues?
Every user pays a flat fee (3 pages or 30, same price). To maximize revenue, serve the shortest job first → a min priority queue.
Among planes waiting to land, the one with the least fuel must go first → a min priority queue.
Real systems work the same way: an operating system, for example, keeps processes in a priority queue.
Priority Queues
Abstract data type: priority queue
/* max priority queue interface */ bool is_empty(void); bool is_full(void); void insert(element item); /* O(lg n) */ bool pop_max(element *out); /* pop + return */ bool peek_max(element *top); /* no removal */
The item goes into the structure together with its priority.
The highest-priority item is returned and removed from the structure.
Heap Structure
A heap lives in an array as a complete binary tree
Index 0 of the array is unused; the root is heap[1].
| Node | Index |
|---|---|
| root | 1 |
| parent of i | i / 2 |
| left child | 2i |
| right child | 2i + 1 |
| last item | n |
In a 0-based array: parent (i−1)/2, children 2i+1, 2i+2.
Heap Structure
A heap means a complete binary tree, not a search tree
- Complete binary tree: every level is full, and the last level fills from left to right.
- In a search tree left < root < right; in a heap only parent ≥ child (no order between left and right).
Heap Structure
Definition: max heap and min heap
Max heap: a complete binary tree in which every node's key is no smaller than its children's.
Min heap: a complete binary tree in which every node's key is no larger than its children's.
These definitions let us implement max/min priority queues.
Heap Structure
Quick check: which is a max heap?
Is heap[k/2] ≥ heap[k] for every k ≥ 2? Only parent–child pairs matter; no order is required between siblings or cousins.
Answer
- (a) Yes: 50≥30, 50≥40, 30≥10, 30≥20, 40≥35.
- (b) No: 35 at index 4 is larger than its parent 30.
- (c) Yes: complete, and every parent ≥ child (it does not matter that 1 and 2 are smaller than 8).
Heap Structure
Definitions in C: array, counter and macros
#define MAX_ELEMENTS 200 /* max items + 1 */ #define HEAP_FULL(n) ((n) == MAX_ELEMENTS - 1) #define HEAP_EMPTY(n) (!(n)) typedef struct { int x; /* priority (key) */ /* other fields */ } element; element heap[MAX_ELEMENTS]; /* [0] unused */ int n = 0; /* item count */
Indices 1..199 are used; the heap is full when n == 199.
(n) prevents precedence bugs in calls like HEAP_EMPTY(a+b).
element heap[MAX_ELEMENTS - 1] (199 items, indices 0..198). Before the heap counts as full, n can reach 199 and heap[199] writes past the end; the size must be MAX_ELEMENTS.Insertion
Inserting into a max heap: take the new slot, then sift
Start: 14, 12, 7, 10, 8, 6. We will insert 15, 109, 107, 3, 15 in turn.
First a new slot opens: the first free position of the complete tree (7).
15 > 7: parent 7 moves down; the hole rises to position 3.
15 > 14: parent 14 moves down; the hole rises to position 1.
15 is written at position 1. The heap property holds again.
Insertion
insert_max_heap: shifting, not swapping
void insert_max_heap(element item) { if (HEAP_FULL(n)) { heap_full(); /* error message */ return; } int i = ++n; while (i != 1) { /* i == 1: at root */ if (item.x <= heap[i / 2].x) break; heap[i] = heap[i / 2]; /* move parent down */ i /= 2; /* go up a level */ } heap[i] = item; }
No swaps: larger ancestors shift down one level each, and the item is written once at the end (a swap costs 3 assignments, a shift 1).
i != 1: the root has no parent. When item.x <= parent, the slot is found.
heap_full() is an error-message routine; we return without inserting.
Insertion · run the code step by step
Code trace: insert_max_heap(15)
insert(15) is called; the heap holds 6 items: 14, 12, 7, 10, 8, 6.
HEAP_FULL(6) is false: 6 ≠ 199, there is room.
i = ++n = 7: the first free position of the complete tree. The slot is certainly there, but first we compare with the parent; item is not written yet.
i = 7 ≠ 1: not at the root yet; enter the loop.
15 ≤ 7? No. The new item is larger than its parent: the parent must move down.
heap[7] = heap[3]: the parent (7) shifted down one level. No swap; the hole (orange) moved up.
i /= 2 → i = 3: move to the parent above.
i = 3 ≠ 1: not at the root yet; enter the loop.
15 ≤ 14? No. The new item is larger than its parent: the parent must move down.
heap[3] = heap[1]: the parent (14) shifted down one level. No swap; the hole (orange) moved up.
i /= 2 → i = 1: move to the parent above.
i == 1: reached the root; the loop ends.
heap[1] = item: 15 is in place. Heap: 15, 12, 14, 10, 8, 6, 7.
void insert_max_heap(element item) { if (HEAP_FULL(n)) { heap_full(); /* error message */ return; } int i = ++n; while (i != 1) { /* i == 1: at root */ if (item.x <= heap[i / 2].x) break; heap[i] = heap[i / 2]; /* move parent down */ i /= 2; /* go up a level */ } heap[i] = item; }
Insertion · run the code step by step
Code trace: insert_max_heap(109)
insert(109): larger than every item. Worst case: it will climb to the root.
HEAP_FULL(7) is false: 7 ≠ 199, there is room.
i = ++n = 8: the first free position of the complete tree. The slot is certainly there, but first we compare with the parent; item is not written yet.
i = 8 ≠ 1: not at the root yet; enter the loop.
109 ≤ 10? No. The new item is larger than its parent: the parent must move down.
heap[8] = heap[4]: the parent (10) shifted down one level. No swap; the hole (orange) moved up.
i /= 2 → i = 4: move to the parent above.
i = 4 ≠ 1: not at the root yet; enter the loop.
109 ≤ 12? No. The new item is larger than its parent: the parent must move down.
heap[4] = heap[2]: the parent (12) shifted down one level. No swap; the hole (orange) moved up.
i /= 2 → i = 2: move to the parent above.
i = 2 ≠ 1: not at the root yet; enter the loop.
109 ≤ 15? No. The new item is larger than its parent: the parent must move down.
heap[2] = heap[1]: the parent (15) shifted down one level. No swap; the hole (orange) moved up.
i /= 2 → i = 1: move to the parent above.
i == 1: reached the root; the loop ends.
heap[1] = item: 109 is in place. Heap: 109, 15, 14, 12, 8, 6, 7, 10.
void insert_max_heap(element item) { if (HEAP_FULL(n)) { heap_full(); /* error message */ return; } int i = ++n; while (i != 1) { /* i == 1: at root */ if (item.x <= heap[i / 2].x) break; heap[i] = heap[i / 2]; /* move parent down */ i /= 2; /* go up a level */ } heap[i] = item; }
Insertion
Successive insertions: +107, +3, +15
Now: 109, 15, 14, 12, 8, 6, 7, 10. Next come 107, 3 and 15.
First a new slot opens: the first free position of the complete tree (9).
107 > 12: parent 12 moves down; the hole rises to position 4.
107 > 15: parent 15 moves down; the hole rises to position 2.
107 ≤ 109: the parent is large enough; stop.
107 is written at position 2. The heap property holds again.
First a new slot opens: the first free position of the complete tree (10).
3 ≤ 8: the parent is large enough; stop.
3 is written at position 10. The heap property holds again.
First a new slot opens: the first free position of the complete tree (11).
15 > 8: parent 8 moves down; the hole rises to position 5.
15 ≤ 107: the parent is large enough; stop.
15 is written at position 5. The heap property holds again.
Inserting 3 needs no shifts at all (best case, Ω(1)); 109 climbed all the way to the root (worst case, O(lg n)).
Insertion
Insertion cost: at most the height of the tree
| Case | Example | Shifts | Cost |
|---|---|---|---|
| Best | inserting 3 | 0 | Ω(1) |
| Worst | inserting 109 | the height | O(lg n) |
| General | — | 0 … ⌊lg n⌋ | O(lg n) |
Deletion
Deleting from a max heap: take the root, place the last item
Data: 30, 15, 28, 11, 14, 3, 6, 10, 9. The first item out is 30 at the root.
The root (30) is taken. The last item 9 is removed and becomes the candidate for the hole at the root.
The larger of the two children (28) is larger than temp (9) → moved up.
9 settles at position 3.
At each step three values matter: the two children and the last item. The largest goes to the slot i points to; i marks the slot to fill, j the children.
Deletion
delete_max_heap: the corrected version
bool delete_max_heap(element *out) { if (HEAP_EMPTY(n)) return false; *out = heap[1]; /* root is returned */ element temp = heap[n--]; /* find its slot */ int i = 1, j = 2; while (j <= n) { if (j < n && heap[j].x < heap[j + 1].x) j++; if (temp.x >= heap[j].x) break; heap[i] = heap[j]; /* move child up */ i = j; j *= 2; } heap[i] = temp; return true; }
Writing return 0; for an empty heap is a compile error (an integer does not convert to a struct).
The suggested solution: return a bool (success/failure) and pass the removed item back through a pointer parameter.
j < n?It guarantees the right child exists before heap[j+1] is read.
Deletion · run the code step by step
Code trace: delete_max_heap (30)
The call: the heap holds 9 items; the root is 30. The removed item goes into e.
HEAP_EMPTY(9) is false; there is an item to remove.
*out = heap[1] = 30: by definition the first item out is the first item of the array.
temp = heap[n--] = 9, n = 8. The last item is removed so the tree stays complete; we search for its slot starting at the root.
i = 1 (candidate slot for temp), j = 2 (i's left child).
j = 2 ≤ n = 8: i has a child; enter the loop.
The right child is larger (15 < 28) → j++, j = 3.
9 ≥ 28? No: the larger child must move up.
heap[1] = heap[3]: 28 moved up one level; the hole sank to position 3.
i = 3, j = 6: continue from the moved value's old slot.
j = 6 ≤ n = 8: i has a child; enter the loop.
The right child is larger (3 < 6) → j++, j = 7.
9 ≥ 6? Yes: temp is ≥ both children → break.
heap[3] = temp (9). Heap: 28, 15, 9, 11, 14, 3, 6, 10.
return true; the caller gets 30 in out. The path has ≤ ⌊lg n⌋ levels → O(lg n).
bool delete_max_heap(element *out) { if (HEAP_EMPTY(n)) return false; *out = heap[1]; /* root is returned */ element temp = heap[n--]; /* find its slot */ int i = 1, j = 2; while (j <= n) { if (j < n && heap[j].x < heap[j + 1].x) j++; if (temp.x >= heap[j].x) break; heap[i] = heap[j]; /* move child up */ i = j; j *= 2; } heap[i] = temp; return true; }
Deletion · run the code step by step
Code trace: the second delete_max_heap (28)
The second call: 28, 15, 9, 11, 14, 3, 6, 10. This time the last item (10) sinks all the way to a leaf.
HEAP_EMPTY(8) is false; there is an item to remove.
*out = heap[1] = 28: by definition the first item out is the first item of the array.
temp = heap[n--] = 10, n = 7. The last item is removed so the tree stays complete; we search for its slot starting at the root.
i = 1 (candidate slot for temp), j = 2 (i's left child).
j = 2 ≤ n = 7: i has a child; enter the loop.
Left child (15) ≥ right child (9): j stays 2.
10 ≥ 15? No: the larger child must move up.
heap[1] = heap[2]: 15 moved up one level; the hole sank to position 2.
i = 2, j = 4: continue from the moved value's old slot.
j = 4 ≤ n = 7: i has a child; enter the loop.
The right child is larger (11 < 14) → j++, j = 5.
10 ≥ 14? No: the larger child must move up.
heap[2] = heap[5]: 14 moved up one level; the hole sank to position 5.
i = 5, j = 10: continue from the moved value's old slot.
j = 10 > n = 7: i is a leaf; leave the loop.
heap[5] = temp (10). Heap: 15, 14, 9, 11, 10, 3, 6.
return true; the caller gets 28 in out. The path has ≤ ⌊lg n⌋ levels → O(lg n).
bool delete_max_heap(element *out) { if (HEAP_EMPTY(n)) return false; *out = heap[1]; /* root is returned */ element temp = heap[n--]; /* find its slot */ int i = 1, j = 2; while (j <= n) { if (j < n && heap[j].x < heap[j + 1].x) j++; if (temp.x >= heap[j].x) break; heap[i] = heap[j]; /* move child up */ i = j; j *= 2; } heap[i] = temp; return true; }
Deletion
Delete until the heap is empty: items come out in decreasing order
Now: 15, 14, 9, 11, 10, 3, 6. Removed so far: 30, 28.
15 removed; the last item 6 settled at position 4. Output order: 30, 28, 15
14 removed; the last item 3 settled at position 5. Output order: 30, 28, 15, 14
11 removed; the last item 3 settled at position 4. Output order: 30, 28, 15, 14, 11
10 removed; the last item 3 settled at position 3. Output order: 30, 28, 15, 14, 11, 10
9 removed; the last item 3 settled at position 2. Output order: 30, 28, 15, 14, 11, 10, 9
6 removed; the last item 3 settled at position 1. Output order: 30, 28, 15, 14, 11, 10, 9, 6
3 removed; 3 was the last item, and the heap is empty. Output order: 30, 28, 15, 14, 11, 10, 9, 6, 3
Deletion
Deletion cost and the journey of the last item
In the 1st deletion, 9 settled at position 3 without sinking three levels.
In the 2nd deletion, 10 sank to a leaf (position 5): the worst case.
The loop goes down one level per iteration: at most ⌊lg n⌋ iterations, 2 comparisons each → O(lg n).
Deleting n times yields the items in decreasing order: this is the idea behind heap sort.
Deletion
Writing the routines better: a struct instead of globals
typedef struct { element *a; int n, capacity; } Heap; Heap *heap_create(int capacity) { Heap *h = malloc(sizeof *h); if (h == NULL) return NULL; h->a = malloc((capacity + 1) * sizeof *h->a); if (h->a == NULL) { free(h); return NULL; } h->n = 0; h->capacity = capacity; return h; } void heap_destroy(Heap *h) { if (h) { free(h->a); free(h); } }
Report success with TRUE/FALSE; pass the item and n by reference.
The array and n live in a struct: there can be several heaps; malloc is always checked.
Deletion
The min versions: rename and flip the comparisons
void insert_min_heap(element item) { /* ... same full check ... */ int i = ++n; while (i != 1) { if (item.x >= heap[i / 2].x) break; /* >= ! */ heap[i] = heap[i / 2]; i /= 2; } heap[i] = item; }
| Where | Max | Min |
|---|---|---|
| insert: stop | item <= parent | item >= parent |
| delete: child | larger | smaller |
| delete: stop | temp >= [j] | temp <= [j] |
Deletion
When priority changes over time: the planes' fuel
Each plane burns fuel at a different rate: fuel levels must be updated every second by an interrupt.
An item whose priority rises moves up, one whose priority falls moves down: O(lg n). Position k is kept in an index table.
/* priority of the item at k rose: sift it up */ void increase_key(int k, int key) { element e = heap[k]; e.x = key; while (k != 1 && e.x > heap[k / 2].x) { heap[k] = heap[k / 2]; k /= 2; } heap[k] = e; }
Comparison
Why a heap? Wouldn't a simple array or list do?
| # | Structure | Insert | Delete (max) | Why |
|---|---|---|---|---|
| 1 | Unsorted array | Θ(1) | Θ(n) | Append at the end; finding the max scans everything. |
| 2 | Unsorted list | Θ(1) | Θ(n) | Insert at the front; still a full scan. |
| 3 | Sorted array (ascending) | O(n) | Θ(1) | binsearch O(lg n) + n shifts; the max is at the end. |
| 4 | Sorted linked list (descending) | O(n) | Θ(1) | Find the slot; delete from the front, update the head. |
| 5 | Max heap | O(lg n) | O(lg n) | Just one root–leaf path. |
When best (Ω) and worst (O) cases have the same order, the bound is tight and written with Θ. Without binsearch, insertion into a sorted array is Θ(n), not just O(n).
Comparison
Let's put numbers into the cost table
| # | Insert | Delete |
|---|---|---|
| 1 · unsorted array | 1 | 2,000,000 |
| 2 · unsorted list | 1 | 2,000,000 |
| 3 · sorted array | 2,000,000 | 1 |
| 4 · sorted list | 2,000,000 | 1 |
| 5 · max heap | ≈ 21 | ≈ 21 |
Assume one instruction = 1 second; lg 2,000,000 ≈ 20.9 (rounded to 21).
Comparison
“Who will notice my algorithm is bad?”
Comparison
Going further: binomial and Fibonacci heaps
| Operation | Binary heap | Binomial heap | Fibonacci heap* |
|---|---|---|---|
| insert | O(lg n) | O(lg n) | O(1) |
| find min | O(1) | O(lg n) | O(1) |
| delete min | O(lg n) | O(lg n) | O(lg n) |
| decrease key | O(lg n) | O(lg n) | O(1) |
| merge two heaps | O(n) | O(lg n) | O(1) |
* Fibonacci heap times are amortized (averaged over a sequence of operations) (CLRS). They give a theoretical gain in algorithms such as Dijkstra and Prim that do many “decrease key” operations.
Advanced Topics
Heapify: turning an array into a heap in place
void build_heap(element a[], int n) { for (int k = n / 2; k >= 1; k--) { element temp = a[k]; int i = k, j = 2 * k; while (j <= n) { if (j < n && a[j].x < a[j + 1].x) j++; if (temp.x >= a[j].x) break; a[i] = a[j]; i = j; j *= 2; } a[i] = temp; } }
Leaves (k > n/2) are already one-item heaps. From k = n/2 up to the root, each node sifts down: a root whose two children are heaps gets fixed.
n calls to insert take O(n lg n); bottom-up construction takes O(n).
Identical to the loop inside delete_max_heap (adjust in Horowitz).
Heapify · run the code step by step
Code trace: build_heap — k = 3 and k = 2
Input: 5, 3, 8, 9, 1, 12, 7 (not a heap yet). Start at n/2 = 3.
k = 3: fix the subtree rooted at 3. Green nodes are already roots of heaps (leaves and those fixed earlier).
temp = a[3] = 8; i = 3, j = 6. The same sift-down as in deletion begins.
Children 12 and 7: the larger is a[6] = 12.
8 < 12 → a[3] = a[6]; the hole sinks to position 6.
i = 6, j = 12: continue from the hole's new position.
j = 12 > n: i = 6 is a leaf; the loop ends.
a[6] = temp (8). The subtree rooted at 3 is now a heap: 5, 3, 12, 9, 1, 8, 7.
k = 2: fix the subtree rooted at 2. Green nodes are already roots of heaps (leaves and those fixed earlier).
temp = a[2] = 3; i = 2, j = 4. The same sift-down as in deletion begins.
Children 9 and 1: the larger is a[4] = 9.
3 < 9 → a[2] = a[4]; the hole sinks to position 4.
i = 4, j = 8: continue from the hole's new position.
j = 8 > n: i = 4 is a leaf; the loop ends.
a[4] = temp (3). The subtree rooted at 2 is now a heap: 5, 9, 12, 3, 1, 8, 7.
void build_heap(element a[], int n) { for (int k = n / 2; k >= 1; k--) { element temp = a[k]; int i = k, j = 2 * k; while (j <= n) { if (j < n && a[j].x < a[j + 1].x) j++; if (temp.x >= a[j].x) break; a[i] = a[j]; i = j; j *= 2; } a[i] = temp; } }
Heapify · run the code step by step
Code trace: build_heap — k = 1
k = 1: fix the subtree rooted at 1. Green nodes are already roots of heaps (leaves and those fixed earlier).
temp = a[1] = 5; i = 1, j = 2. The same sift-down as in deletion begins.
Children 9 and 12: the larger is a[3] = 12.
5 < 12 → a[1] = a[3]; the hole sinks to position 3.
i = 3, j = 6: continue from the hole's new position.
Children 8 and 7: the larger is a[6] = 8.
5 < 8 → a[3] = a[6]; the hole sinks to position 6.
i = 6, j = 12: continue from the hole's new position.
j = 12 > n: i = 6 is a leaf; the loop ends.
a[6] = temp (5). The subtree rooted at 1 is now a heap: 12, 9, 8, 3, 1, 5, 7.
k = 0: the loop is done. The whole array is a max heap: 12, 9, 8, 3, 1, 5, 7.
void build_heap(element a[], int n) { for (int k = n / 2; k >= 1; k--) { element temp = a[k]; int i = k, j = 2 * k; while (j <= n) { if (j < n && a[j].x < a[j + 1].x) j++; if (temp.x >= a[j].x) break; a[i] = a[j]; i = j; j *= 2; } a[i] = temp; } }
Advanced Topics
Why is heapify O(n)?
Half the nodes are leaves and never move; a quarter move at most 1 level, an eighth at most 2… Total work:
Σh≥0 ⌈n / 2h+1⌉ · O(h) = O(n · Σ h/2h) = O(2n) = O(n)
Advanced Topics
d-ary heap: every node has d children
| Binary (d = 2) | d-ary (0-based) | |
|---|---|---|
| parent | (i−1)/2 | (i−1)/d |
| children | 2i+1, 2i+2 | d·i+1 … d·i+d |
| height | ≈ lg n | ≈ logd n |
| insert / increase key | O(lg n) | O(logd n) — faster |
| delete root | O(lg n) | O(d · logd n) — compares d children |
With many inserts/decrease-keys and few deletes (e.g. Dijkstra on dense graphs), choosing d > 2 pays off; a shallow tree is cache-friendly.
Advanced Topics
Where are priority queues used?
Scheduling processes and interrupts, timer events.
Dijkstra's shortest paths and Prim's MST: picking the nearest vertex.
Build a heap, remove the root n times: O(n lg n), in place.
Events are pulled from a min heap by timestamp.
A min heap of size k (a max heap for the smallest): O(n lg k) on streaming data.
Repeatedly merges the two smallest frequencies.
Lab · C code step by step
Lab: Max Heap
Summary
Complexity summary (binary heap with n items)
| Operation | Time | Extra space | Note |
|---|---|---|---|
| peek at the max | O(1) | O(1) | heap[1] |
| insert | Ω(1) … O(lg n) | O(1) | sift up |
| delete the max | O(lg n) | O(1) | sift down |
| change a priority | O(lg n) | O(1) | if the position is known |
| bottom-up construction | O(n) | O(1) | in place |
| building by n inserts | O(n lg n) | O(1) | |
| search for an arbitrary item | O(n) | O(1) | a heap is not a search structure |
Summary
Common mistakes
i/2, 2i) and 0-based ((i−1)/2, 2i+1) formulas.heap[199] writes out of bounds.return 0; in a routine that returns an element (compile error).j < n test and reading heap[j+1] (outside the heap; outside the array in build_heap).Summary
Exam-style question: insert, insert, … delete, delete
heap[1..6] = 14, 12, 7, 10, 8, 6 we insert 15, 109, 107, 3, 15 in order; then delete_max_heap is called twice.(i) What does the array hold after the insertions? (ii) Which items are removed, and what is the final array? (iii) Which insertion shifted least, and which most?
Solution
109, 107, 14, 15, 15, 6, 7, 10, 12, 3, 8(n = 11).- Removed: 109 and 107; final array
15, 15, 14, 12, 3, 6, 7, 10, 8(n = 9). - Inserting 3 shifted nothing (Ω(1)); 109 made 3 shifts up to the root (O(lg n)).
Summary
Exercises
- Insert 20, 5, 30, 10, 40 into an empty max heap; write the array after each insertion.
- Write
delete_max_heapfor a min heap. Which lines change? - At most how many shifts does an insertion make in a heap of n = 1000 items?
- In a 1-based heap, at which indices are the parent and the children of node 13?
- If successive
inserts are applied to5, 3, 8, 9, 1, 12, 7instead of build_heap, is the result the same? - Where can the smallest item of a heap be? What does finding it cost?
Show answers
20→20, 5→30, 5, 20→30, 10, 20, 5→40, 30, 20, 5, 10.- Child choice
[j] > [j+1], stopping conditiontemp.x <= heap[j].x; the name isdelete_min_heap. - ⌈lg 1001⌉ = 10 levels → at most 9 shifts (⌊lg 1000⌋ = 9).
- Parent 13/2 = 6; children 26 and 27 (if n ≥ 27).
- No; successive insertion gives
12, 8, 9, 3, 1, 5, 7. Both are valid heaps, but they differ. - If the keys are distinct, only among the leaves (index > n/2); about n/2 leaves are scanned → O(n).
Summary
Chapter summary
The first item out has the highest (max) or the lowest (min) value.
Complete binary tree + parent ≥ child. In a 1-based array the parent is i/2, the children 2i and 2i+1.
Open the first free slot, shift larger ancestors down: O(lg n).
Take the root, find a slot for the last item starting at the root, move larger children up: O(lg n).
Other structures push one operation to Θ(n); a heap keeps both logarithmic.
Heapify in O(n), d-ary heaps, binomial and Fibonacci heaps.
Summary
References and next steps
- E. Horowitz, S. Sahni, S. Anderson-Freed, Fundamentals of Data Structures in C, Section 5.6 (Heaps), Chapter 7 (Heap Sort).
- T. H. Cormen et al., Introduction to Algorithms (CLRS), Chapter 6 (Heapsort, priority queues) and the chapter on Fibonacci heaps.
- R. Sedgewick, Algorithms in C, Parts 1–4, Chapter 9 (Priority Queues and Heapsort).