Sub topic 4
Sift-Down: Restoring Order One Subtree at a Time
This chapter assumes you know the index formulas for a heap stored in an array (left(i) = 2*i + 1, right(i) = 2*i + 2) and the max-heap property: every parent is at least as large as its children. Sift-down is the single operation that heap construction and heap sort both depend on. If you get it right, the rest of the algorithm is short.
The problem sift-down solves
Suppose the left and right subtrees of some node are already valid max-heaps, but the node itself is too small. One element is out of place, and the rest of the structure is sound. Sift-down repairs this. It swaps the node with its larger child and repeats until the node is at least as large as both children or has no children left.
The precondition matters. Sift-down only fixes the node you start at. It assumes everything below that node already satisfies the heap property. If the subtrees are not heaps, sift-down will not produce one.
Why the larger child
When the node is too small, one of its children has to move up. If you promote the smaller child, the new parent will be smaller than its other child, which breaks the property right away. Promoting the larger child keeps the property true at the current level, so the only possible violation moves down one level.
The implementation
def sift_down(a, start, end):
# Restore the max-heap property for the subtree rooted at start,
# considering only a[0:end].
root = start
while True:
child = 2 * root + 1
if child >= end:
break
if child + 1 < end and a[child + 1] > a[child]:
child += 1
if a[root] >= a[child]:
break
a[root], a[child] = a[child], a[root]
root = child
data = [3, 9, 8, 7, 5, 6]
sift_down(data, 0, len(data))
print(data) # [9, 7, 8, 3, 5, 6]The end parameter treats only a[0:end] as the heap. Heap sort needs this because it will keep sorted elements at the back of the same array, and sift-down must not touch them.
Tracing the example
Starting from [3, 9, 8, 7, 5, 6], only the root 3 is out of place. Both subtrees, rooted at 9 and 8, are already heaps.
| Step | root | children (index: value) | larger | Action | Array after |
|---|---|---|---|---|---|
| 1 | 0 | 1: 9, 2: 8 | 9 | swap | [9, 3, 8, 7, 5, 6] |
| 2 | 1 | 3: 7, 4: 5 | 7 | swap | [9, 7, 8, 3, 5, 6] |
| 3 | 3 | 7 (≥ end) | none | stop | [9, 7, 8, 3, 5, 6] |
Cost
Each pass through the loop moves root down one level, so the loop runs at most once per level of the tree. A complete tree with n nodes has height ⌊log₂ n⌋, which makes sift-down O(log n) time and O(1) extra space.
Common errors
Exercise
Run sift_down([2, 10, 9, 4, 6], 0, 5) by hand. What is the final array, and how many swaps happen?
Show answer
Two swaps. The root 2 swaps with its larger child 10, giving [10, 2, 9, 4, 6]. At index 1, the children are 4 and 6, so 2 swaps with 6, giving [10, 6, 9, 4, 2]. Index 4 has no children, so the loop stops.
Exercise
What does sift_down([1, 5, 3, 9], 0, 3) produce, and why is the 9 never moved?
Show answer
[5, 1, 3, 9]. The root 1 swaps with 5. Index 1's left child would be index 3, which is not less than end = 3, so the loop stops. The element at index 3 lies outside a[0:3], and sift-down ignores it.