Chapter 13: Priority Queues and Heaps
Multiple Choice · Priority Queue
Question 1: Choosing the Right Structure
An operating system wants to run the shortest ready job each time. Jobs arrive often, and so do requests for "give me the shortest". Which structure fits best?
B: Both insertion and "take the shortest" cost O(lg n). A favors the wrong end. In a sorted array the position is found in O(lg n), but shifting the items takes O(n); in an unsorted list insertion is O(1), but finding the shortest job is Θ(n).
Multiple Choice · Heap Structure
Question 2: Index Arithmetic
In a heap of 50 items stored in a 1-based array (heap[1..50]), which statement about node 25 is correct?
A: The parent is 25 / 2 = 12 (integer division). The children would be 2 · 25 = 50 and 51, but 51 > n, so only the left child exists. A complete binary tree has at most one node with a single child: node n / 2 when n is even.
Multiple Choice · Heap Structure
Question 3: Which Is a Max Heap?
Which of the following arrays (1-based, left to right heap[1..7]) is a max heap?
D: For every k ≥ 2, heap[k/2] ≥ heap[k]: 90 ≥ 70, 80; 70 ≥ 20, 60; 80 ≥ 75, 10. In A, the 75 at index 4 is larger than its parent 70; in B, the 85 at index 5 is larger than its parent 80; in C, the 85 at index 6 is larger than its parent 80.
Multiple Choice · Heap Structure
Question 4: The Macro Without Parentheses
The macro is defined without parentheses. What does the following code print?
#define HEAP_EMPTY(n) !n int n = 5, k = 3; printf("%d\n", HEAP_EMPTY(n - k));
C: The preprocessor substitutes the text as it is: the expression becomes !n - k. The ! applies first: !5 = 0, then 0 − 3 = −3. Since −3 is nonzero, a condition would call the heap "empty" even though it holds 2 items. With the correct definition (!(n)) it would print 0.
Output Question · Insertion
Question 5: Successive Insertions
12, 7, 19, 3, 15, 25 are inserted, in this order, into an empty max heap with insert_max_heap. What is the array after each insertion? How many shifts (heap[i] = heap[i / 2]) are made in total?
Show answer
| inserted | shifts | heap[1..n] |
|---|---|---|
| 12 | 0 | 12 |
| 7 | 0 | 12 7 |
| 19 | 1 (12) | 19 7 12 |
| 3 | 0 | 19 7 12 3 |
| 15 | 1 (7) | 19 15 12 3 7 |
| 25 | 2 (12, 19) | 25 15 19 3 7 12 |
In total 4 shifts. 25 is the worst case: it climbs up to the root, ⌊lg 6⌋ = 2 levels.
Multiple Choice · Insertion
Question 6: Swap or Shift?
During insertion the new item climbs k levels. How many item assignments are made in total by the version that swaps with the parent at every step (3 assignments per swap) and by the shifting version from the slides?
C: A swap makes 3 assignments per level through a temporary variable: 3k. Shifting writes the parent down once per level (k assignments) and writes the new item once at the end: k + 1.
Step-by-Step Trace · Insertion
Question 7: insert_max_heap(55) Step by Step
insert_max_heap(55) is applied to the max heap heap[1..7] = 60 45 50 20 40 48 30. How does the hole (?) move, and what is the final array? (The number under a node is its index.)
The initial heap.
Not full; i = ++n = 8: the first free position of the complete tree. item is not written yet.
55 ≤ 20? No → 20 moves down: heap[8] = heap[4].
i /= 2 → 4: the hole is one level higher.
55 ≤ 45? No → 45 moves down: heap[4] = heap[2]; i = 2.
55 ≤ 60 → break: the place is found.
heap[2] = 55. The array: 60 55 50 45 40 48 30 20. 2 shifts + 1 write.
void insert_max_heap(element item) { if (HEAP_FULL(n)) { heap_full(); return; } int i = ++n; while (i != 1) { if (item.x <= heap[i / 2].x) break; heap[i] = heap[i / 2]; i /= 2; } heap[i] = item; }
Spot the Bug · Insertion
Question 8: 0-Based Array, 1-Based Formula
Insertion into a max heap whose root is stored in heap[0] is written as below. What is the array after inserting 1, 4, 5, 6, 2, 3, 7 into an empty heap? Is the heap property preserved? What is the bug?
element heap[16]; /* root is heap[0] */ int n = 0; void insert(element item) { int i = n++; while (i != 0) { if (item.x <= heap[i / 2].x) break; heap[i] = heap[i / 2]; i /= 2; } heap[i] = item; }
Show answer
| inserted | heap[0..n−1] |
|---|---|
| 1, 4, 5 | 5 4 1 |
| 6 | 6 5 1 4 |
| 2 | 6 5 2 4 1 |
| 3 | 6 5 3 4 1 2 |
| 7 | 7 6 3 5 1 2 4 |
The final array is not a heap: the child of the 3 at index 2 is the 4 at index 6, and 3 < 4.
In a 0-based array the parent is (i − 1) / 2. i / 2 gives the right answer for odd i but the next node for even i. 7 follows the path 6 → 3 → 1 → 0 and pushes the 4 below the 3; the right path is 6 → 2 → 0. With (i − 1) / 2 in every insertion the result would be 7 5 6 1 2 3 4.
Step-by-Step Trace · Deletion
Question 9: delete_max_heap() Step by Step
delete_max_heap(&e) is applied to the heap 60 55 50 45 40 48 30 20 produced by the previous insertion question. Which items move up, and what is the final array?
The root (60) is taken; the last item 20 is removed and a place is searched for it from the root. n = 7.
The children are 55 and 50: the larger one is j = 2. 20 ≥ 55? No.
55 moves up; the hole goes down to position 2.
The children are 45 and 40: the larger is 45; 20 ≥ 45? No → 45 moves up, the hole goes to position 4.
j = 8 > n = 7: i is a leaf. heap[4] = 20. The array: 55 45 50 20 40 48 30; e = 60.
bool delete_max_heap(element *out) { if (HEAP_EMPTY(n)) return false; *out = heap[1]; element temp = heap[n--]; 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]; i = j; j *= 2; } heap[i] = temp; return true; }
Spot the Bug · Deletion
Question 10: Choosing the Wrong Child
In delete_max_heap the comparison on the child-selection line is written the wrong way round. What happens if this routine is applied twice to the heap 60 55 50 45 40 48 30 20?
if (j < n && heap[j].x > heap[j + 1].x) j++; /* wrong: smaller child */ if (temp.x >= heap[j].x) break; heap[i] = heap[j]; i = j; j *= 2;
Show answer
- 1st call: 60 is correctly returned. At the root the children are 55 and 50; the smaller one (50) is chosen and moves up. Then, among the children at 50's old place, the smaller one, 30, is chosen. The array: 50 55 30 45 40 48 20.
- The heap is broken: the root 50 is smaller than its child 55; the 30 at index 3 is smaller than its child 48.
- The 2nd call returns the root, 50, although the largest item is 55. The priority queue gives the wrong item.
Fix: heap[j].x < heap[j + 1].x. The largest of the three values (two children, temp) must move up.
Multiple Choice · Deletion
Question 11: The Item at Index 4
In a max heap with distinct values and at least 8 items, which statement about the item at index 4 is certainly true?
A: A heap guarantees only the parent ≥ child relation: heap[4] < heap[2] < heap[1]. Siblings are not ordered; the item at index 4 may be the third largest or the seventh largest. Since n ≥ 8, index 8 is its child, so it is not a leaf either.
Coding Question · Deletion
Question 12: Decreasing a Priority
Write the opposite of the slides' increase_key: void decrease_key(int k, int key) lowers the priority of the item at position k of a max heap to key (a smaller value) and repairs the heap. What is the array after decrease_key(2, 10) on the heap 60 55 50 45 40 48 30 20?
Show answer
void decrease_key(int k, int key) { element e = heap[k]; e.x = key; int j = 2 * k; while (j <= n) { /* shift down */ if (j < n && heap[j].x < heap[j + 1].x) j++; /* larger child */ if (e.x >= heap[j].x) break; heap[k] = heap[j]; /* move the child up */ k = j; j *= 2; } heap[k] = e; }
A decreased priority can only create a violation below it, so the shift-down of deletion is started from k: O(lg n). In the example, 45 and 20 each move up one level and 10 lands at position 8 → 60 45 50 20 40 48 30 10.
Classic Question · Comparison
Question 13: Sorted List or Heap?
A priority queue holds 1,048,576 (220) items. 1000 insertions and 1000 "take the largest" operations will be performed. About how many steps does this take in the worst case (a) in a linked list sorted from largest to smallest, (b) in a max heap? (One comparison or shift is one step.)
Show answer
| 1 insertion | 1 removal | total (1000 + 1000) | |
|---|---|---|---|
| sorted list | ≈ 1,048,576 (the place is searched) | 1 (from the front) | ≈ 1.05 billion |
| heap | ≤ 20 levels | ≤ 20 levels × 2 comp. | ≈ 20,000 + 40,000 = 60,000 |
lg 220 = 20, and n grows to at most 1,049,576: the height does not change. The heap does about 17,000 times less work. Even if removals were far fewer than insertions, the insertions would dominate the cost of the sorted list.
Multiple Choice · Comparison
Question 14: The Cost Table
Which statement about the structures that implement a priority queue is false?
B is false: a heap keeps only the parent–child order; the sought value could be in either subtree, so in the worst case the whole array is scanned: O(n). A heap is not a search structure. In C the largest item is at the end of the array; only n is decremented.
Step-by-Step Trace · Advanced Topics
Question 15: build_heap Step by Step
build_heap(a, 8) is applied to the array a[1..8] = 4 10 3 5 1 8 9 2. What happens for each k, what is the final array, and how many moves (a[i] = a[j]) are made?
Not a heap yet. The leaves (5–8) are already one-item heaps; we start from k = n / 2 = 4.
k = 4: a single child, 2 (j = 8 = n, no right child). 5 ≥ 2 → it stays in place.
k = 3: the children are 8 and 9 → the larger is 9 (j = 7). 3 < 9 → 9 moves up.
j = 14 > n: a[7] = 3. The subtree rooted at 3 is now a heap.
k = 2: the children are 5 and 1; 10 ≥ 5 → no change.
k = 1: the children are 10 and 9 → 10; 4 < 10 → 10 goes to the root.
i = 2: the children are 5 and 1 → 5; 4 < 5 → 5 moves up.
i = 4: a single child, 2; 4 ≥ 2 → a[4] = 4. The result is 10 5 9 4 1 8 3 2, with 3 moves.
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; } }
Classic Question · Advanced Topics
Question 16: Heapify on an Ascending Array
build_heap is applied to the (ascending) array a[1..15] = 1, 2, …, 15. How many moves are made? How does this number relate to the upper bound on the "total number of levels the items can sink" for a complete tree of 15 nodes? How many moves would there be if the array were given in descending order (15, 14, …, 1)?
Show answer
- In the ascending array every internal node is the smallest of its own subtree and sinks all the way to a leaf: 4 nodes (k = 4..7) sink 1 level each, 2 nodes (k = 2, 3) sink 2 each, the root sinks 3 levels → 0 · 8 + 1 · 4 + 2 · 2 + 3 · 1 = 11 moves.
- This is the upper bound itself: the ascending array is the worst input for build_heap. Since the bound is Σ h · n / 2h+1 < n, construction is O(n).
- The result: 15 11 14 9 10 13 7 8 4 2 5 12 6 3 1.
- The descending array is already a max heap: at every k the first comparison hits
break→ 0 moves.
Multiple Choice · Advanced Topics
Question 17: Indices in a 4-ary Heap
In a 4-ary heap (every node has 4 children) stored in a 0-based array, at which indices are the parent and the children of the node at index 10?
D: In a d-ary heap (0-based) the parent is (i − 1) / d = 9 / 4 = 2; the children are d · i + 1 … d · i + d = 41 … 44. The tree is shallower (height ≈ log₄ n), so insertion is faster; deletion, however, compares 4 children at every level.