Skip to content

Binary heap

storing

Level 2 of 3

Also called Priority queue and Min-heap.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

The heap drawn was made by putting in 40, 25, 33, 18 and 29; its letters show the order each number came in. Put in 12. What is the row then?

123451825334029d18b25c33a40e29
the answer, and why

the answer is A

12 goes in at place 6, under 33. It is smaller, so they swap. Now under 18, it is smaller again, so they swap. At the top, it stays.

  1. step 18 of 24

    182533402912

    Put 12 at the end of the row, in place 6.

    comparisons: 0

  2. step 20 of 24

    182512402933

    Swap them: 12 climbs above 33.

    comparisons: 1

  3. step 22 of 24

    122518402933

    Swap them: 12 climbs above 18.

    comparisons: 2

  4. step 23 of 24

    122518402933

    12 is at the top, so it stays.

    comparisons: 2

12 put at place 6 climbs above 33, then above 18, to the top. 2 comparisons.

if you chose another

  • That stops after one swap, but 12 is still smaller than 18 above it.
  • That leaves 12 where it was put, under 33, which is bigger.
  • That is the whole row in order. A heap only swaps 12 up its way to the top.

question 2

How many comparisons do these operations make on an empty heap? 16, 9, 21, 4, then one take.

the answer, and why

the answer is C

16 compares nothing. 9 compares with 16 and climbs to the top. 21 compares with 9 and stays. 4 climbs past 16 and 9, two. The take moves 16 to the top, which meets 9 and 21, two more. 16 swaps below 9, has nothing under it, and stops.

operationcomparisons
put 160
put 91
put 211
put 42
take2
in all6
Each operation of 16, 9, 21, 4 and a take, in comparisons.

if you chose another

  • That counts the puts only. The take compares too.
  • That counts a comparison each time a number is at the top, but nothing is above it to compare with.
  • That counts one comparison for the take. 16 meets both children of the top.

question 3

Each list puts five numbers into an empty heap. Which makes the fewest comparisons?

the answer, and why

the answer is D

Smallest first, each number is no smaller than the one above it, so it stays after one comparison. That costs 0, 1, 1, 1 and 1, 4 in all. The others cost 6, 5 and 5.

putabcd
1st0000
2nd1111
3rd1111
4th1221
5th1212
in all4655
Each list's comparisons, put by put.

if you chose another

  • Biggest first, each number climbs all the way to the top, one comparison a level. It costs 6.
  • 33 climbs over 61, and 48 climbs over 61, then stops under 33. It costs 5.
  • 33 climbs over 61, and 48 climbs over 75, then stops under 33. It costs 5.

question 4

Which is true of putting nn numbers into an empty heap, then taking them all out?

the answer, and why

the answer is D

No nn puts compare more than n log⁡2 nn \,\log_2\, n, and no nn takes more than 2n log⁡2 n2n \,\log_2\, n, as section 4 of the learn page adds up.

this tests best, worst and average, on the learn page

if you chose another

  • From 3 numbers on, equal numbers cost 3n−63n - 6, which is Θ(n)\Theta(n).
  • A take grows with the log, about 15 comparisons at 1,000 numbers and 22 at 10,000. A put stays near 2.2 or 2.3.
  • At 6, 7 and 8 numbers other orders cost more, as the table in section 3 of the learn page shows.

pen and paper

question 5about 10 minutes

Ten numbers arrive one at a time. When all ten are in, you take them all out, smallest first.

  1. How many comparisons does an unsorted row make?
  2. Between what two counts must a binary heap fall, by section 4 of the learn page?
  3. Do these settle which way is cheaper at 10 numbers? At a million?
a hint

An unsorted row's take compares its way through every number left. A heap costs at least 3n−63n - 6 and at most 3n log⁡2 n3n \,\log_2\, n.

a worked answer

The row's takes compare 9, then 8, down to 0, which makes 45. The heap makes at least 24, from 3n−63n - 6 at 10, and at most about 99.7, from 3n log⁡2 n3n \,\log_2\, n at 10. 45 lies between, so at 10 numbers they do not settle it. At a million the row makes 499,999,500,000, and the heap at most about 59.8 million: the heap is over 8,000 times cheaper.

comparisons10 numbersa million
unsorted row45about 500 billion
heap at least24about 3 million
heap at mostabout 99.7about 59.8 million
All in, then all out.

this tests how it compares, on the learn page

what a full answer includes
  • 45 for the unsorted row.
  • At least 24 and at most about 99.7.
  • No at 10; yes at a million.

question 6about 10 minutes

A heap holds 15 numbers.

  1. At most how many comparisons can one more put make?
  2. At most how many can one take make?
  3. An unsorted row holds the same 15 numbers. How many comparisons does its take make?
a hint

After the put the heap holds 16 numbers, and after the take 14. A heap of mm numbers is ⌊log⁡2 m⌋\lfloor \log_2\, m \rfloor high.

a worked answer

After the put the heap holds 16. Halving 16 reaches 1 in 4 steps, so the new place is 4 levels below the top, and a climb costs at most one comparison a level: 4. The take leaves 14, between 8 and 16, so the heap is 3 high: at most 3 rounds of 2, which is 6. It is reached: put in 1 to 15 in order and take one, and 15 sinks from place 1 to 8, places 1, 2 and 4 each with two children. The unsorted row compares the first number with each of the other 14: 14.

numberlog× 2+ 11644833422211100
16 halved down to 1 takes 4 steps, 8, 4, 2 and 1, so place 16 is 4 levels below the top.
At most how many comparisons can nn takes make? A ceiling, from 3 numbers on never reached.
  1. the move: the last moves up

    the line:
    a take that leaves s numbersmoves the last to place 1\begin{gathered} \text{a take that leaves } s \text{ numbers} \\ \text{moves the last to place 1} \end{gathered}

    why it is allowed: The top, the smallest, goes. The last number takes its place, so the row keeps no gap. Here ss counts the numbers left.

  2. the move: price a round

    the line:
    a round at place pcompares its children,2p and 2p+1, thenstays or sinks a level\begin{gathered} \text{a round at place } p \\ \text{compares its children,} \\ 2p \text{ and } 2p + 1 \text{, then} \\ \text{stays or sinks a level} \end{gathered}
    1. step 18 of 26

      712735465268

      14 is out. Move the last number, 71, to the top.

      comparisons: 0

    2. step 21 of 26

      277135465268

      Swap them: 71 sinks below 27.

      comparisons: 2

    3. step 24 of 26

      274635715268

      Swap them: 71 sinks below 46.

      comparisons: 4

    4. step 25 of 26

      274635715268

      Nothing is below 71, so it stays.

      comparisons: 4

    One take from 14, 27, 35, 46, 52, 68, 71. 71 moves up and sinks below 27, then 46. 4 comparisons.
    roundcomparesthen
    127 with 71, 35 with 27swap 71 with 27
    246 with 71, 52 with 46swap 71 with 46
    no roundno child below 71it stays
    Each round of that take, in comparisons. Two rounds of 2, then no round.

    why it is allowed: Place pp's children are 2p2p and 2p+12p + 1, as section 1 of the learn page reads the row. Each is compared with the smaller so far, the number itself first. In the picture, 27 with 71, then 35 with 27. The number swaps with a smaller child.

  3. the move: count the rounds

    the line:
    at most as many roundsas the heap is high\begin{gathered} \text{at most as many rounds} \\ \text{as the heap is high} \end{gathered}

    why it is allowed: A round needs a child below, so the last level holds none. In the picture, the heap of 6 is 2 high, and 71 sinks 2 rounds.

  4. the move: price the rounds

    the line:
    at most 2 comparisonsa round, so at mosttwice the height\begin{gathered} \text{at most 2 comparisons} \\ \text{a round, so at most} \\ \text{twice the height} \end{gathered}

    why it is allowed: By line 2, a round compares at most the 2 children. In the picture, 2 rounds make 4.

  5. the move: add up the takes

    the line:
    D=2×the heightof a heap of 1+…+2×that of n−1\begin{gathered} D = 2 \times \text{the height} \\ \text{of a heap of 1} + \dots \\ + 2 \times \text{that of } n - 1 \end{gathered}
    12341234
    In a heap of 4 the top has 2 children, place 2 only 1. So a take leaving 4 makes at most 2 and 1, 3, not line 4's 4.
    a take leavingheightline 4 allowsthe most it makes
    4243
    3122
    2121
    1000
    in all86
    The most each take of 5 makes, over every order of 1 to 5. The take leaving 2 always makes 1 where line 4 allows 2, so from 3 numbers on the ceiling is never reached.

    why it is allowed: DD adds up line 4 for the takes leaving n−1n - 1 down to 1, so the takes compare at most DD. The take leaving none compares nothing.

  6. the move: name the heights

    the line:
    D=2⌊log⁡2 1⌋+…+2⌊log⁡2(n−1)⌋\begin{gathered} D = 2\lfloor \log_2\, 1 \rfloor + \dots \\ + 2\lfloor \log_2 (n - 1) \rfloor \end{gathered}
    where does this come from?
    question 3 of the page trees and their wordsA tree of nn places kept in a row. What is its height?
    1. the move: the last place is the deepest

      the line:
      place n lies onthe last level\begin{gathered} \text{place } n \text{ lies on} \\ \text{the last level} \end{gathered}

      why it is allowed: The row fills the levels in order, so its last place, place nn, is on the last level. So the height is the depth of place nn.

    2. the move: each step up halves

      the line:
      a step up halves the place,rounding down\begin{gathered} \text{a step up halves the place,} \\ \text{rounding down} \end{gathered}
      13223451678910012345678910
      From place 10 up to the root, place 1, each step halving and rounding down, 10, 5, 2, 1, the steps counted beside them. Three steps, so place 10 has depth 3.

      why it is allowed: By question 2 of the page trees and their words, the parent of place ii is place ⌊i/2⌋\lfloor i / 2 \rfloor. From place 10 the steps up go 5, 2, 1.

    3. the move: count the steps

      the line:
      H=halvings, rounding down,that take n to 1\begin{gathered} H = \text{halvings, rounding down,} \\ \text{that take } n \text{ to 1} \end{gathered}

      why it is allowed: Let HH be the height. By lines 1 and 2 it is the steps from place nn up to place 1, each one halving, rounding down.

    4. the move: use halving and logarithms

      the line:
      H=⌊log⁡2 n⌋H = \lfloor \log_2\, n \rfloor
      where does this come from?
      question 2 of the page halving and logarithmsHalve nn, rounding down each time. How many halvings reach 1?
      1. the move: name the log rounded down

        the line:
        K=⌊log⁡2 n⌋K = \lfloor \log_2\, n \rfloor
        numberlog× 2+ 14283164113.46352.322211100
        Rounding down, 11 halves to 5, 2 and 1. In blue, each log rounded down: 3, 2, 1, 0, one less at each halving.

        why it is allowed: Here nn is the number halved, from 2 up; for 11, KK is 3.

      2. the move: rounding down never adds

        the line:
        ⌊log⁡2 n⌋≤log⁡2 n\lfloor \log_2\, n \rfloor \le \log_2\, n

        why it is allowed: For 11, 3 is at most 3.46.

      3. the move: rounding down takes off less than 1

        the line:
         log⁡2 n<⌊log⁡2 n⌋+1\begin{gathered} \,\log_2\, n \\ < \lfloor \log_2\, n \rfloor + 1 \end{gathered}

        why it is allowed: For 11, 4 is above 3.46.

      4. the move: raise 2 to each side of line 2

        the line:
        2⌊log⁡2 n⌋≤2log⁡2 n2^{\lfloor \log_2\, n \rfloor} \le 2^{\log_2\, n}

        why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.

      5. the move: 2 to the log of n is n

        the line:
        2⌊log⁡2 n⌋≤n2^{\lfloor \log_2\, n \rfloor} \le n

        why it is allowed: By what a log is. For 11, 8 is at most 11.

      6. the move: raise 2 to each side of line 3

        the line:
        2log⁡2 n<2⌊log⁡2 n⌋+12^{\log_2\, n} < 2^{\lfloor \log_2\, n \rfloor + 1}

        why it is allowed: As in line 4. For 11, about 11 is below 16.

      7. the move: 2 to the log of n is n

        the line:
        n<2⌊log⁡2 n⌋+1n < 2^{\lfloor \log_2\, n \rfloor + 1}

        why it is allowed: As in line 5. For 11, 11 is below 16.

      8. the move: halve both sides of line 5

        the line:
        2⌊log⁡2 n⌋/2≤n/22^{\lfloor \log_2\, n \rfloor} / 2 \le n / 2

        why it is allowed: For 11, 4 is at most 5.5, which is not whole.

      9. the move: write the half as a power

        the line:
        2⌊log⁡2 n⌋−1≤n/22^{\lfloor \log_2\, n \rfloor - 1} \le n / 2

        why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.

      10. the move: round the half down

        the line:
        2⌊log⁡2 n⌋−1≤⌊n/2⌋2^{\lfloor \log_2\, n \rfloor - 1} \le \lfloor n / 2 \rfloor

        why it is allowed: ⌊n/2⌋\lfloor n / 2 \rfloor is the half rounded down, 5 for 11. A whole number at most 5.5 is at most 5. The left side, 4 for 11, is whole, as KK is at least 1 for nn from 2 up.

      11. the move: halve both sides of line 7

        the line:
        n/2<2⌊log⁡2 n⌋+1/2n / 2 < 2^{\lfloor \log_2\, n \rfloor + 1} / 2

        why it is allowed: For 11, 5.5 is below 8.

      12. the move: write the half as a power

        the line:
        n/2<2⌊log⁡2 n⌋n / 2 < 2^{\lfloor \log_2\, n \rfloor}

        why it is allowed: As in line 9. Half of 16 is 8.

      13. the move: round the half down

        the line:
        ⌊n/2⌋<2⌊log⁡2 n⌋\lfloor n / 2 \rfloor < 2^{\lfloor \log_2\, n \rfloor}

        why it is allowed: Rounding down never adds. For 11, 5 is below 8.

      14. the move: take the log of line 10

        the line:
         log⁡2 2⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \,\log_2\, 2^{\lfloor \log_2\, n \rfloor - 1} \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

        why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.

      15. the move: the log of a power of 2 is the power

        the line:
        ⌊log⁡2 n⌋−1≤log⁡2 ⌊n/2⌋\begin{gathered} \lfloor \log_2\, n \rfloor - 1 \\ \le \log_2\, \lfloor n / 2 \rfloor \end{gathered}

        why it is allowed: For 11, the log of 4 is 2, at most about 2.32.

      16. the move: take the log of line 13

        the line:
         log⁡2 ⌊n/2⌋<log⁡2 2⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \log_2\, 2^{\lfloor \log_2\, n \rfloor} \end{gathered}

        why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.

      17. the move: the log of a power of 2 is the power

        the line:
         log⁡2 ⌊n/2⌋<⌊log⁡2 n⌋\begin{gathered} \,\log_2\, \lfloor n / 2 \rfloor \\ < \lfloor \log_2\, n \rfloor \end{gathered}

        why it is allowed: For 11, about 2.32 is below 3.

      18. the move: round the log down

        the line:
        ⌊log⁡2 ⌊n/2⌋⌋=⌊log⁡2 n⌋−1\begin{gathered} \lfloor \log_2\, \lfloor n / 2 \rfloor \rfloor \\ = \lfloor \log_2\, n \rfloor - 1 \end{gathered}
        the numberat leastbelowits log rounded down
        118163
        5482
        2241
        1120
        Each halving takes 1 off the log rounded down.

        why it is allowed: By lines 15 and 17, the log of the half is at least one less than KK and below KK, so it rounds down to one less. For 11, about 2.32 rounds down to 2.

      19. the move: use line 18 again and again

        the line:
        after K halvings,rounding down,the log roundeddown is 0\begin{gathered} \text{after } K \text{ halvings,} \\ \text{rounding down,} \\ \text{the log rounded} \\ \text{down is 0} \end{gathered}

        why it is allowed: While the log rounded down is 1 or more, the number is at least 2 to the power 1, which is 2. So line 18 takes 1 off each halving, down to 0 after KK. For 11, 3, 2, 1, 0.

      20. the move: a log rounded down to 0 belongs to 1

        the line:
        K halvings,rounding down,bring n to 1\begin{gathered} K \text{ halvings,} \\ \text{rounding down,} \\ \text{bring } n \text{ to 1} \end{gathered}

        why it is allowed: Such a log is at least 0 and below 1, so its number is at least 1 and below 2. It is 1.

      the whole page: halving and logarithms

      why it is allowed: The page on halving shows that halving nn, rounding down, reaches 1 after ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor halvings. For 10, the biggest power of 2 not past it is 8, three 2s multiplied, so 3 halvings, as in the picture. A tree of 1 place needs none, and the log of 1 is 0.

    5. the move: read it

      the line:
      10 places: height 31,000 places: height 9\begin{gathered} 10 \text{ places: height 3} \\ 1{,}000 \text{ places: height 9} \end{gathered}

      why it is allowed: 10 lies between 8 and 16, so its log rounded down is 3. 1,000 lies between 512 and 1,024, so 9. Doubling the places adds 1 to the height. 20 places end at place 20, the first child of place 10 by question 2 of the page trees and their words, one level below where 10 places end.

    the whole page: trees and their words

    why it is allowed: The box under this line shows a heap of ss numbers is ⌊log⁡2 s⌋\lfloor \log_2\, s \rfloor high. For 5, 2 times (0 + 1 + 1 + 2) is 8.

  7. the move: one take

    the line:
    so a take leaving scompares at most 2⌊log⁡2 s⌋\begin{gathered} \text{so a take leaving } s \\ \text{compares at most } 2\lfloor \log_2\, s \rfloor \end{gathered}

    why it is allowed: One term of line 6. For 6 left, twice 2 is 4.

  8. the move: a ceiling

    the line:
    D≤2(n−1)⌊log⁡2 n⌋D \le 2(n - 1)\lfloor \log_2\, n \rfloor

    why it is allowed: Line 6 has n−1n - 1 terms, each at most twice ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor, as a smaller heap is no taller.

  9. the move: drop the rounding

    the line:
    D≤2(n−1) log⁡2 nD \le 2(n - 1) \,\log_2\, n

    why it is allowed: Rounding down makes no bigger.

  10. the move: round n−1n - 1 up to nn

    the line:
    D≤2n log⁡2 nD \le 2n \,\log_2\, n

    why it is allowed: n−1n - 1 is less than nn.

  11. the move: read it as a bound

    the line:
    D∈O(n log⁡2 n)D \in O(n \,\log_2\, n)

    why it is allowed: By line 10, with the fixed multiple 2.

try it with a small nn: at n=5n = 5, line 5 gives D=8D = 8, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 4, one comparison for each of the 4 levels above place 16.
  • 6, three rounds of 2.
  • 14 for the unsorted row.