This is a worked-examples companion to the parent Heap Operations note. There we built the rules; here we run those rules through every kind of situation they can meet, so no exam question surprises you.
If a word here is new — "sift-up", "sift-down", "complete", "parent index" — go re-read the parent note first; this page assumes you have those in hand and only drills them.
Every heap problem is really one of the primitives (sift-up or sift-down) applied in a specific situation. The situations differ along a few axes. Below is every axis, and every value it can take. The examples afterward each fill in one row.
#
Case class
The tricky thing to watch
Example that hits it
A
insert, new key sinks to bottom
key is small → zero swaps, stays at leaf
Ex 1
B
insert, new key climbs to root
key is biggest → full-height climb
Ex 2
C
extract-max (max-heap), misfit sinks fully
root replacement is smallest → full path down
Ex 3
D
extract-max (max-heap), degenerate size
heap has 1 or 2 elements → boundary of index math
Ex 4
E
sift-down reaching a ONE-child parent
during a real extract, the sinking node meets a parent with a left child but no right → don't read out of range
Ex 5
F
decrease-key (min-heap) → sift-up
smaller value must go up
Ex 6
G
decrease-key (min-heap), no-op
new value still ≥ parent → zero swaps
Ex 7
H
increase-key (max-heap) → sift-up
larger value goes up in a max-heap
Ex 8
H′
increase-key (max-heap), no-op
new value still ≤ parent → zero swaps
Ex 8b
K
increase-key (min-heap) → sift-down
bigger value belongs lower in a min-heap
Ex 11
L
decrease-key (max-heap) → sift-down
smaller value belongs lower in a max-heap
Ex 12
I
word problem — real priority queue
mapping a story onto operations
Ex 9
J
exam twist — build-heap ordering
is a given array a valid heap? spot the violation
Ex 10
Reveal-yourself checks:
Which primitive does insert always use?
sift-up (append at the end, climb).
Which primitive does extract always use?
sift-down (move last to root, sink).
The two "zero swaps" cases are
an inserted key already ≤ its parent (max-heap), and a decrease-key whose new value is still ≥ its parent.
decrease-key on a max-heap uses
sift-down (a smaller value sinks toward the leaves).
increase-key on a min-heap uses
sift-down (a larger value sinks toward the leaves).
Figure 1 — Alt text: a max-heap tree of 6 nodes after inserting 3. Root 40 (ink) has children 30 (teal, index 1) and 20 (plum, index 2); 30 has children 10 and 5; 20 has one new child 3 (orange, index 5) drawn at the bottom. A short orange arrow beside node 3 is crossed out and labelled "3 < parent 20, no climb". Each node is tagged with its array index.
Figure 2 — Alt text: a max-heap tree of 6 nodes showing the climb of inserted value 99. The value 99 is drawn as an orange node with two upward orange arrows tracing its path: first from index 5 up to index 2, then from index 2 up to index 0 (the root). Old occupants 20 and 40 are shown shifting down. Labels read "swap 1: 99 > 20" and "swap 2: 99 > 40". Each node is tagged with its array index.
Figure 3 — Alt text: a max-heap tree of 5 nodes after the last element 20 (drawn as the orange root node) has replaced the removed maximum. Root 20 sits above children 30 (teal, left) and 40 (plum, right); 30 has children 10 and 5. An orange arrow points from the root down toward the larger child 40, labelled "sinks to larger child". Each node is tagged with its array index.
Figure 4 — Alt text: a 6-node max-heap tree for [40,10,30,5,8,20]. Root 40 (ink) has children 10 (teal, index 1) and 30 (ink, index 2); node 10 has children 5 (index 3) and 8 (index 4); node 30 has a single real child 20 (plum, index 5) drawn with a solid edge, and a dashed edge to an empty dashed circle marked "—" at index 6, labelled "right child idx 6 >= n (guard r < n fails)". A teal label reads "misfit sinks here and meets a lone left child".
Figure 5 — Alt text: a min-heap tree of 5 nodes showing value 1 climbing after decrease-key. Node at index 3 turns orange (was 12, now 1) and two upward orange arrows trace its path: from index 3 to index 1, then index 1 to index 0 (root). Labels read "1 < 8" and "1 < 2". Displaced values 8 and 2 shift down. Each node tagged with its array index.
Every heap operation is sift-up (insert, or a key-change toward the root end) or sift-down
(extract, repair, or a key-change toward the leaf end), applied over at most one root-to-leaf
path of length log2n — with zero-work and single-child / empty boundary cases you
must guard explicitly with ℓ<n and r<n. The four key-change flavours form a clean 2×2
grid: sift-up for decrease-in-min / increase-in-max, sift-down for increase-in-min /
decrease-in-max.