Skip to content

Removing the minimum

Remove the smallest key of a min-heap by moving the last key to the root and sifting it down, tracing it on an array, and give its cost as O(log n).

It opens on

4, the smallest key, is taken from the top of this heap. To keep the tree complete, does 5, its smaller child, or 15, the last key in the array, fill the empty top?

Why it matters

A priority queue is only useful if the smallest item can also be taken out quickly, and removal is where a heap earns its place: the next job to run, the nearest unvisited city or the best offer comes out in about log n steps while new items keep going in.

Good to know first

  • Binary heaps and insertion: a min-heap is a complete tree stored in an array with each parent at most its children, and push appends and sifts up in O(log n)
  • Trees and binary trees: the height of a complete tree of n nodes is ⌊log₂ n⌋
  • Big-O, Big-Omega and Big-Theta: O(log n) grows far slower than n

Step by step

  1. Fill the hole at the top

    Remove the minimum of a min-heap by moving the last key to the root and swapping it down with its smaller child, and say why it is the last key and the smaller child.

    The array is now 15, 5, 6, 9, 12, 8, 7. The children of 15 are 5 and 6, and the smaller is 5, so 15 swaps with 5 and the array is 5, 15, 6, 9, 12, 8, 7. Now 5 is the root.

    A common mix-up

    The smaller child 5 should simply move up to fill the hole.

    In fact: That leaves a hole at index 1, and the holes would move down the tree, leaving a shape that is not complete; the last key fills the hole instead.

  2. The code, traced

    Read and trace sift_down and pop_min on an array, including the case where a node has only a left child and the case where the heap has one key.

    Run it on 15, 5, 6, 9, 12, 8, 7 with i equal to 0. Here c is 1, and the right child 6 is not smaller than 5, so c stays 1. 15 is more than 5, so they swap and i becomes 1. Then c is 3, the right child 12 is not smaller than 9, so 15 swaps with 9 and i becomes 3.

    A common mix-up

    sift_down needs only a comparison with the left child.

    In fact: The right child may be smaller, and the test c + 1 < n then c += 1 chooses the smaller one; a run that always took the left child left 5, 6, 2, 9, 7, 8, which breaks the rule.

  3. The cost of a pop

    State the cost of a pop as O(log n) from the height, show that unlike an insert it is typically near the worst case, and compare the heap with a list as a priority queue.

    A common mix-up

    Pop is as cheap as push on random data.

    In fact: a push stops early because a random key is usually large enough, but a pop moves a leaf's key to the root and it sinks almost the whole height.

  4. The priority queue in Python

    Use heapq.heappush and heapq.heappop as a priority queue of (priority, task) pairs, say what order ties come out in, and give the costs of the operations.

    The loop pops while the list is not empty. The first pop is (1, 'deploy'), then (1, 'fix server'), then (2, 'reply email'), then (3, 'write report'), and last (5, 'lunch'), which the run printed in that order.

    the output

    A common mix-up

    Heapq keeps the list sorted.

    In fact: It keeps a heap, so the list is only ordered from parent to child, and only the first item is guaranteed to be the smallest.

Try it

Pop from the min-heap 1, 3, 2, 7, 4. What does the pop return, and what is the array afterwards?Answer

1, and the array is 2, 3, 4, 7

Hint: 4 goes to the root and swaps with the smaller child, 2

What does pop_min return on 3, 6, 4, 9, 7, 5, 8, and what is the array afterwards?Answer

3 and the array 4, 6, 5, 9, 7, 8

Hint: 8 goes to the root, swaps with 4, and then swaps with its only child, 5

A min-heap has 100 keys and the minimum is popped. What is the most swaps?Answer

6

Hint: 99 keys remain, and 2⁶ = 64 is at most 99, but 2⁷ = 128 is more

Tasks (2, "b"), (1, "z"), (1, "a") are pushed in that order and popped until empty. What is the order of the pops?Answer

(1, 'a'), (1, 'z'), (2, 'b')

Hint: Compare the priorities first, then the names

Why must 15 swap with the smaller child, 5, and not with 6?Answer

If 6 rose to the top it would sit above the child 5, a parent larger than its child, and the order would break; the run printed 6, 5, 15, 9, 12, 8, 7 for that swap, which is not a heap.

Removing the minimum

30 minutes every month. No card.