Loading lessons...
Heaps & Heap-Based Priority Queues
Heaps & Heap-Based Priority Queues
A Binary Heap is a Complete Binary Tree stored compactly in a 1D array (with zero wasted gaps) that satisfies the Heap Order Property:
- Max-Heap: Every parent ≥ its children (global maximum at root
A[0]). - Min-Heap: Every parent ≤ its children (global minimum at root
A[0]).
Max-Heap Operations
1. Reheap Up (Insertion - O(log n))
- Append the new key at the end of the array (next available leaf slot
n). - Reheap Up: Compare with
parent = (i - 1) / 2. Whilei > 0andA[i] > A[parent](for Max-Heap), swap and seti = parent.
- Worked Trace: Insert
38into Max-Heap[42, 32, 35, 19, 23, 12]: - Place
38at index 6 (right child of35at index 2). 38 > 35→ swap →[42, 32, 38, 19, 23, 12, 35].- Compare with root
42:38 < 42→ stop!
2. Reheap Down (Extract Max/Min - O(log n))
- Save
A[0](the max/min), move the last leafA[n - 1]toA[0], and decrementn. - Reheap Down: Swap downward with the larger child (Max-Heap) or smaller child (Min-Heap) until heap order is restored.
- Worked Trace: Deleting
42from[42, 32, 35, 19, 23, 12]: - Move
12to root →[12, 32, 35, 19, 23]. - Larger child of
12is35(index 2) → swap →[35, 32, 12, 19, 23]. 12is now a leaf → stop!
C
void reheapDown(int a[], int n, int i) {
while (2 * i + 1 < n) {
int largest = 2 * i + 1;
if (largest + 1 < n && a[largest + 1] > a[largest])
largest++;
if (a[i] >= a[largest]) break;
int t = a[i]; a[i] = a[largest]; a[largest] = t;
i = largest;
}
}3. Build-Heap & Priority Queue Applications
- Floyd's Bottom-Up Build-Heap: Calling
reheapDownfrom the last non-leaf node(n/2 - 1)down to0builds a heap in O(n) time (faster than n insertions which take O(n log n) because half the nodes are leaves at height 0!). - Min-Heap Priority Queue: In OS task scheduling, tasks with the smallest deadline/priority number sit at
A[0]for O(1) peek and O(log n) dispatch; also powers Dijkstra's shortest path and Prim's MST.
Index Rules for an Array Heap
| Relation | 0-based array | 1-based array |
|---|---|---|
| Parent of i | ⌊(i − 1)/2⌋ | ⌊i/2⌋ |
| Left child of i | 2i + 1 | 2i |
| Right child of i | 2i + 2 | 2i + 1 |
| Sibling of i | i + 1 if i is a left child (odd), i − 1 if right child (even) | i + 1 if i is even, i − 1 if i is odd |
| Leaves | indices ⌊n/2⌋ … n − 1 | ⌊n/2⌋ + 1 … n |
Worked Example: Build a Max Heap by Repeated Insertion
Insert 34, 45, 42, 56, 78, 4, 99, 32, 55, 90, 88 (each new key is added at the end and reheaped up):
| Insert | Swaps during reheap-up | Heap array after insertion |
|---|---|---|
| 34 | - | 34 |
| 45 | 45↔34 | 45 34 |
| 42 | - | 45 34 42 |
| 56 | 56↔34, 56↔45 | 56 45 42 34 |
| 78 | 78↔45, 78↔56 | 78 56 42 34 45 |
| 4 | - | 78 56 42 34 45 4 |
| 99 | 99↔42, 99↔78 | 99 56 78 34 45 4 42 |
| 32 | - | 99 56 78 34 45 4 42 32 |
| 55 | 55↔34 | 99 56 78 55 45 4 42 32 34 |
| 90 | 90↔45, 90↔56 | 99 90 78 55 56 4 42 32 34 45 |
| 88 | 88↔56 | 99 90 78 55 88 4 42 32 34 45 56 |
TEXT
99
/ \
90 78
/ \ / \
55 88 4 42
/ \ / \
32 34 45 56Worked Example: Priority Queue of Tasks with a Min Heap
Tasks arrive with priorities (smaller number = more urgent): T1:4, T2:2, T3:6, T4:3, T5:1.
| Operation | Min-heap array (priorities) |
|---|---|
| insert T1(4) | 4 |
| insert T2(2) | 2 4 |
| insert T3(6) | 2 4 6 |
| insert T4(3) | 2 3 6 4 (3 swapped with its parent 4) |
| insert T5(1) | 1 2 6 4 3 (1 moves up twice) |
| deleteMin → T5 | 2 3 6 4 (last key 3 to root, reheap down below 2) |
| deleteMin → T2 | 3 4 6 |
| deleteMin → T4 | 4 6 |
Tasks are served in the order T5, T2, T4, T1, T3 - by priority, not by arrival.
🎯 END-TERM EXAM PREP
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Define Max-Heap and Min-Heap. Explain Reheap Up (Insertion) and Reheap Down (Deletion) with an example. (7.5 Marks)
- Write: Completeness + Heap Order property, 0-based parent/child index formulas,
reheapDown()C code, and worked insert/delete trace. - Q2. Explain how a Heap is used to implement a Priority Queue and state its time complexities. (5 Marks)
- Write: O(log n) insert/extract, O(1) peek, O(n) bottom-up Build-Heap, and comparison with unsorted/sorted array & linked list priority queues.