Skip to content

Binary heap

storing

Level 2 of 3

Also called Priority queue and Min-heap.

fresh numbers

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

question 1

The heap drawn was made by putting in 85, 28, 14, 10 and 65; its letters show the order each number came in. Put in 6. What is the row then?

123451014288565d10c14b28a85e65
the answer, and why

the answer is C

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

if you chose another

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

new numbersthe test page's numbers

question 2

How many comparisons do these operations make on an empty heap? 81, 51, 83, 18, then one take.

the answer, and why

the answer is C

81 compares nothing. 51 compares with 81 and climbs to the top. 83 compares with 51 and stays. 18 climbs past 81 and 51, two. The take moves 81 to the top, which meets 51 and 83, two more. 81 swaps below 51, has nothing under it, and stops.

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. 81 meets both children of the top.

new numbersthe test page's numbers

question 3

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

the answer, and why

the answer is A

Putting in 42, 47, 67, 72, 23 makes 5 comparisons, the fewest of the four.

if you chose another

  • That list makes 6 comparisons, more than 5.
  • That list makes 6 comparisons, more than 5.
  • That list makes 6 comparisons, more than 5.

new numbersthe test page's numbers