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 98, 16, 35, 10 and 14; its letters show the order each number came in. Put in 1. What is the row then?

123451014359816d10e14c35a98b16
the answer, and why

the answer is A

1 goes in at place 6, under 35. 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 1 is still smaller than 10 above it.
  • That leaves 1 where it was put, under 35, which is bigger.
  • That is the whole row in order. A heap only swaps 1 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? 80, 61, 82, 22, then one take.

the answer, and why

the answer is B

80 compares nothing. 61 compares with 80 and climbs to the top. 82 compares with 61 and stays. 22 climbs past 80 and 61, two. The take moves 80 to the top, which meets 61 and 82, two more. 80 swaps below 61, 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. 80 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 D

Putting in 26, 50, 44, 85, 86 makes 4 comparisons, the fewest of the four.

if you chose another

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

new numbersthe test page's numbers