The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from binary heap's own run on the numbers shown.
choose one
1question 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?
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.
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.
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.