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 72, 31, 77, 42 and 36; its letters show the order each number came in. Put in 5. What is the row then?

123453136777242b31e36c77a72d42
the answer, and why

the answer is D

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

if you chose another

  • That stops after one swap, but 5 is still smaller than 31 above it.
  • That leaves 5 where it was put, under 77, which is bigger.
  • That is the whole row in order. A heap only swaps 5 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? 68, 41, 78, 39, then one take.

the answer, and why

the answer is A

68 compares nothing. 41 compares with 68 and climbs to the top. 78 compares with 41 and stays. 39 climbs past 68 and 41, two. The take moves 68 to the top, which meets 41 and 78, two more. 68 swaps below 41, 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. 68 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 C

Putting in 25, 5, 51, 80, 88 makes 4 comparisons, the fewest of the four.

if you chose another

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

new numbersthe test page's numbers