WalkthroughDifficulty: Beginner3 min

Push and Pop With Sift-Up and Sift-Down

Push adds at the end and climbs; pop moves the last item to the root and sinks.

A min-heap adds an item by placing it in the one free position and swapping it upward, and removes its smallest item by moving the last item to the root and swapping it downward. This card covers both operations, called sift-up and sift-down, traced one swap at a time on a small heap stored as an array.

A min-heap is a complete binary tree, filled level by level from the left, in which every parent is no larger than its children. In the array, the parent of index i is at (i - 1) // 2 and its children are at 2i + 1 and 2i + 2. Both operations restore the shape first, which is always possible in one move, and then repair heap order. The item that moved can only be out of order along one path, so the repair walks that path and stops as soon as the order holds. The heap starts as the array 1 3 5 7 4 8.

The starting heap 1 3 5 7 4 8, with the free position at index 6
  • 1, index 0

    • 3, index 1

      • 7, index 3

      • 4, index 4

    • 5, index 2

      • 8, index 5

      • null, index 6, free

Pushing 2, then popping the smallest item, one swap at a time

Push 2: place it in the free position

2 goes to index 6, the first free position, which is the right child of 5. The array is 1 3 5 7 4 8 2. The tree is still complete, but 2 is smaller than its parent, so heap order is broken on the path from index 6 to the root.

Sift 2 up until its parent is no larger

The parent of index 6 is at (6 - 1) // 2, index 2, which holds 5. 2 is smaller, so the two swap and the array becomes 1 3 2 7 4 8 5. Now 2 is at index 2, and its parent at index 0 holds 1. 2 is not smaller than 1, so the sift stops after one swap.

The heap after pushing 2: 1 3 2 7 4 8 5
  • 1, index 0

    • 3, index 1

      • 7, index 3

      • 4, index 4

    • 2, index 2, climbed one level

      • 8, index 5

      • 5, index 6, swapped down

Pop: take the root and move the last item into its place

The smallest item, 1, is at index 0, so pop returns it. The last item, 5 at index 6, moves to index 0, and the array shrinks by one to 5 3 2 7 4 8. The tree is complete again, but 5 is larger than both of its children.

Sift 5 down towards its smaller child

The children of index 0 are 3 at index 1 and 2 at index 2. 5 swaps with the smaller one, 2, because the child that moves up becomes the parent of its sibling, and only the smaller child is no larger than the other. The array is 2 3 5 7 4 8.

Stop when no child is smaller

5 is now at index 2, and its one child, at index 5, holds 8. 5 is not larger than 8, so the sift stops. The heap is 2 3 5 7 4 8, with the new smallest item at the root.

The heap after popping 1: 2 3 5 7 4 8
  • 2, index 0, the new smallest

    • 3, index 1

      • 7, index 3

      • 4, index 4

    • 5, index 2, sank one level

      • 8, index 5

      • null, index 6, free again

A run of both operations, printing the array after each swap, produced the same arrays: 1 3 2 7 4 8 5 after the push, and 2 3 5 7 4 8 after the pop.

Each operation does at most one swap per level, and a complete tree of n items has about log2 n levels, so push and pop both cost O(log n). The step most often done wrong is the choice of child in sift-down. Swapping 5 with the left child out of habit would give 3 5 2 7 4 8, which puts 3 above 2 at the root and breaks heap order. Sift-up never has that choice, because every node below the root has exactly one parent.