Lesson 2 · Unit 03
Sift up, sift down
Push and pop in O(log n), traced by hand
What you practise
- Push and pop in O(log n), traced
Try a question from this lesson
Pick one
A min-heap is stored as [1, 3, 2, 7, 4, 5]. You append 0 at the end, where it sits under a larger parent. What is the cheapest correct repair?
Correct answer
Swap it with its parent, repeating while it is smaller
Why
Only the path from the new item up to the root can be out of order, so walk that path: at most one swap per level. Swapping straight with the root would drop the old root, 1, into a leaf under the 2.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Push: sift up
To add a value, append it at the end (the tree stays complete), then sift it up: while it is smaller than its parent, swap the two.
It stops at the root or under a parent that is not larger. That is one swap per level at most: O(log n).
def push(h, x):
h.append(x)
i = len(h) - 1
while i > 0:
p = (i - 1) // 2
if h[p] <= h[i]:
break
h[p], h[i] = h[i], h[p]
i = pfunction push(h, x) {
h.push(x);
let i = h.length - 1;
while (i > 0) {
const p = Math.floor((i - 1) / 2);
if (h[p] <= h[i]) break;
[h[p], h[i]] = [h[i], h[p]];
i = p;
}
}function push(h: number[], x: number) {
h.push(x);
let i = h.length - 1;
while (i > 0) {
const p = Math.floor((i - 1) / 2);
if (h[p] <= h[i]) break;
[h[p], h[i]] = [h[i], h[p]];
i = p;
}
}Python noteh[p], h[i] = h[i], h[p] swaps two items in one statement, with no temporary variable.
JavaScript note[h[p], h[i]] = [h[i], h[p]] is a destructuring swap: the right side is built first, then unpacked into the two slots.
TypeScript noteh: number[] is changed in place, and nothing is returned, so TypeScript infers the return type void.
Concept
Pop: sift down
The minimum is at index 0. To remove it, move the last item into the root (the tree stays complete), then sift it down: swap it with its smaller child until neither child is smaller.
This function does the sifting, starting from index i. Here c is the index of the smaller child.
def sift_down(h, i):
n = len(h)
while True:
c = 2 * i + 1
if c + 1 < n and h[c + 1] < h[c]:
c += 1
if c >= n or h[i] <= h[c]:
break
h[i], h[c] = h[c], h[i]
i = cfunction siftDown(h, i) {
const n = h.length;
while (true) {
let c = 2 * i + 1;
if (c + 1 < n && h[c + 1] < h[c]) c++;
if (c >= n || h[i] <= h[c]) break;
[h[i], h[c]] = [h[c], h[i]];
i = c;
}
}function siftDown(
h: number[],
i: number,
): void {
const n = h.length;
while (true) {
let c = 2 * i + 1;
if (c + 1 < n && h[c + 1] < h[c]) c++;
if (c >= n || h[i] <= h[c]) break;
[h[i], h[c]] = [h[c], h[i]];
i = c;
}
}Python notewhile True loops until a break. The loop has two reasons to stop, and both are tested in the middle of the body.
TypeScript noteThe signature is wrapped one parameter per line. : void after the closing bracket says the function returns nothing: it rearranges h in place.
8 more exercises in the app
- 3× type the answer
- 1× fill the blanks
- 1× order the lines
- 1× multiple choice
- 1× true or false
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.