Seven pushes, seven pops
A heap is a tree stored in an array. Not a tree with an array beside it — the array IS the tree.
There is no code in this step: first how the array is read, then seven pushes and seven pops in a table.
Where the pointers went
In lessons 11 and 12 a node held two pointers and its children were wherever those pointers led. Here there are none, because a child's position is computed:
for the node at index i:
parent (i−1)/2
left 2i+1
right 2i+2
index: 0 1 2 3 4 5 6
value: 12 19 15 41 22 17 33
12
/ \
19 15
/ \ / \
41 22 17 33
The same tree, drawn two ways. The second is simply the first read out level by level.
Cost in pointers: 0 bytes. Lesson 11's node cost 88 bytes and lesson 12's 96, and each sat wherever the allocator put it. Here every element sits contiguously, one after another, and the processor pulls them in batches.
In lesson 11 you measured what scattered pointers cost: the same 5000.5 comparisons and 4.5× the time. Here that same observation runs the other way.
The heap property
No parent is larger than its children.
That is all. Nothing about left versus right — there is NO order between
siblings. Above, 19 > 15, and that is a perfectly valid heap.
Exactly one thing follows: the smallest element is a[0]. Always. With no
search and no comparison. And it knows nothing else — step 6 is about why that is
enough.
How we count
Two numbers per row, written as comparisons / swaps:
- comparison — you looked at two elements and decided which is smaller. The
same number
c.Hit()counts in code; - swap — two elements changed places in the array.
Two operations, and each appears where the property has to be restored:
- push puts the element in the one free slot — the end of the array — and raises it until its parent is no larger. Each level: one comparison with the parent;
- pop removes
a[0]and puts the array's last element in its place, then lowers it. Each level: a comparison between the two children (when both exist), then the smaller one against the element being lowered.
Seven pushes
Into an empty heap, in this order:
19 17 15 41 22 12 33
push cmps / swaps array after the push
19 0 / 0 [19]
17 1 / 1 [17 19]
15 1 / 1 [15 19 17]
41 1 / 0 [15 19 17 41]
22 1 / 0 [15 19 17 41 22]
12 2 / 2 [12 19 15 41 22 17]
33 1 / 0 [12 19 15 41 22 17 33]
total: 7 / 4
Look at the final array. It is the same one you drew above.
Seven pops
Now take everything out, one at a time:
pop cmps / swaps array after the pop
12 3 / 2 [15 19 17 41 22 33]
15 2 / 1 [17 19 33 41 22]
17 3 / 1 [19 22 33 41]
19 2 / 1 [22 41 33]
22 1 / 0 [33 41]
33 0 / 0 [41]
41 0 / 0 []
total: 11 / 5
The first row up close — this is why the last element goes to the root:
pop 12: [12 19 15 41 22 17 33]
33 (the last) to the root: [33 19 15 41 22 17]
level 1: children 19 and 15 → smaller is 15; 15 < 33 → swap
[15 19 33 41 22 17] 2 comparisons
level 2: 33 has one child, 17; 17 < 33 → swap
[15 19 17 41 22 33] 1 comparison
level 3: no children — stop
The array after the pop has no hole and is one shorter: still a complete tree. If you moved one of the children to the root instead, an empty slot would be left somewhere below.
Four observations
They are worth not skipping, because the next steps are built from nothing else.
1. A descending sequence is the worst case. 19 17 15 — every new element is
smaller than all before it, so it rises all the way to the root: one swap each.
By contrast 41 and 22 cost one comparison and zero swaps: the parent was
already smaller, so nothing had to move.
2. The array is not sorted — and what came out was. [12 19 15 …]: 19 sits
before 15. Yet the pops came out 12 15 17 19 22 33 41, strictly ascending. A
heap has no order; it produces one, one element at a time, and pays the
number of levels for each.
3. The cost of a pop falls because the heap shrinks: 3, 2, 3, 2, 1, 0, 0. It is not the number of elements that is paid for but the depth: at most two comparisons per level, and a seven-element heap has three levels.
4. The swaps column totals 9. None of them is in the comparison count. The counter sees comparisons only. That difference returns in step 7.
What next
You now know what a heap does. The next step writes one half of it as Go — the other half arrives already written.