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?
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.
step 18 of 24
Put 12 at the end of the row, in place 6.
comparisons: 0
step 20 of 24
Swap them: 12 climbs above 33.
comparisons: 1
step 22 of 24
Swap them: 12 climbs above 18.
comparisons: 2
step 23 of 24
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
BThat stops after one swap, but 12 is still smaller than 18 above it.
CThat leaves 12 where it was put, under 33, which is bigger.
DThat is the whole row in order. A heap only swaps 12 up its way to the top.
2question 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.
operation
comparisons
put 16
0
put 9
1
put 21
1
put 4
2
take
2
in all
6
Each operation of 16, 9, 21, 4 and a take, in comparisons.
if you chose another
AThat counts the puts only. The take compares too.
BThat counts a comparison each time a number is at the top, but nothing is above it to compare with.
DThat counts one comparison for the take. 16 meets both children of the top.
3question 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.
put
a
b
c
d
1st
0
0
0
0
2nd
1
1
1
1
3rd
1
1
1
1
4th
1
2
2
1
5th
1
2
1
2
in all
4
6
5
5
Each list's comparisons, put by put.
if you chose another
ABiggest first, each number climbs all the way to the top, one comparison a level. It costs 6.
B33 climbs over 61, and 48 climbs over 61, then stops under 33. It costs 5.
C33 climbs over 61, and 48 climbs over 75, then stops under 33. It costs 5.
4question 4
Which is true of putting numbers into an empty heap, then taking them all out?
the answer, and why
the answer is D
No puts compare more than , and no takes more than , as section 4 of the learn page adds up.
AFrom 3 numbers on, equal numbers cost , which is .
BA 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.
CAt 6, 7 and 8 numbers other orders cost more, as the table in section 3 of the learn page shows.
pen and paper
5question 5about 10 minutes
Ten numbers arrive one at a time. When all ten are in, you take them all out, smallest first.
How many comparisons does an unsorted row make?
Between what two counts must a binary heap fall, by section 4 of the learn page?
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 and at most .
a worked answer
The row's takes compare 9, then 8, down to 0, which makes 45. The heap makes at least 24, from at 10, and at most about 99.7, from 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.
At most how many comparisons can one more put make?
At most how many can one take make?
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 numbers is 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.
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 takes make? A ceiling, from 3 numbers on never reached.11 of 11 shown
1the move: the last moves up
the line:
why it is allowed: The top, the smallest, goes. The last number takes its place, so the row keeps no gap. Here counts the numbers left.
2the move: price a round
the line:
step 18 of 26
14 is out. Move the last number, 71, to the top.
comparisons: 0
step 21 of 26
Swap them: 71 sinks below 27.
comparisons: 2
step 24 of 26
Swap them: 71 sinks below 46.
comparisons: 4
step 25 of 26
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.
round
compares
then
1
27 with 71, 35 with 27
swap 71 with 27
2
46 with 71, 52 with 46
swap 71 with 46
no round
no child below 71
it stays
Each round of that take, in comparisons. Two rounds of 2, then no round.
why it is allowed: Place 's children are and , 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.
guess it, then
3the move: count the rounds
the line:
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.
guess it, then
4the move: price the rounds
the line:
why it is allowed: By line 2, a round compares at most the 2 children. In the picture, 2 rounds make 4.
guess it, then
5the move: add up the takes
the line:
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 leaving
height
line 4 allows
the most it makes
4
2
4
3
3
1
2
2
2
1
2
1
1
0
0
0
in all
8
6
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: adds up line 4 for the takes leaving down to 1, so the takes compare at most . The take leaving none compares nothing.
guess it, then
6the move: name the heights
the line:
where does this come from?
question 3 of the page trees and their wordsA tree of places kept in a row. What is its height?
1the move: the last place is the deepest
the line:
why it is allowed: The row fills the levels in order, so its last place, place , is on the last level. So the height is the depth of place .
2the move: each step up halves
the line:
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 is place . From place 10 the steps up go 5, 2, 1.
3the move: count the steps
the line:
why it is allowed: Let be the height. By lines 1 and 2 it is the steps from place up to place 1, each one halving, rounding down.
4the move: use halving and logarithms
the line:
where does this come from?
question 2 of the page halving and logarithmsHalve , rounding down each time. How many halvings reach 1?
1the move: name the log rounded down
the line:
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 is the number halved, from 2 up; for 11, is 3.
2the move: rounding down never adds
the line:
why it is allowed: For 11, 3 is at most 3.46.
3the move: rounding down takes off less than 1
the line:
why it is allowed: For 11, 4 is above 3.46.
4the move: raise 2 to each side of line 2
the line:
why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.
5the move: 2 to the log of n is n
the line:
why it is allowed: By what a log is. For 11, 8 is at most 11.
6the move: raise 2 to each side of line 3
the line:
why it is allowed: As in line 4. For 11, about 11 is below 16.
7the move: 2 to the log of n is n
the line:
why it is allowed: As in line 5. For 11, 11 is below 16.
8the move: halve both sides of line 5
the line:
why it is allowed: For 11, 4 is at most 5.5, which is not whole.
9the move: write the half as a power
the line:
why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.
10the move: round the half down
the line:
why it is allowed: 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 is at least 1 for from 2 up.
11the move: halve both sides of line 7
the line:
why it is allowed: For 11, 5.5 is below 8.
12the move: write the half as a power
the line:
why it is allowed: As in line 9. Half of 16 is 8.
13the move: round the half down
the line:
why it is allowed: Rounding down never adds. For 11, 5 is below 8.
14the move: take the log of line 10
the line:
why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.
15the move: the log of a power of 2 is the power
the line:
why it is allowed: For 11, the log of 4 is 2, at most about 2.32.
16the move: take the log of line 13
the line:
why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.
17the move: the log of a power of 2 is the power
the line:
why it is allowed: For 11, about 2.32 is below 3.
18the move: round the log down
the line:
the number
at least
below
its log rounded down
11
8
16
3
5
4
8
2
2
2
4
1
1
1
2
0
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 and below , so it rounds down to one less. For 11, about 2.32 rounds down to 2.
19the move: use line 18 again and again
the line:
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 . For 11, 3, 2, 1, 0.
20the move: a log rounded down to 0 belongs to 1
the line:
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.
why it is allowed: The page on halving shows that halving , rounding down, reaches 1 after 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.
5the move: read it
the line:
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.