Skip to content

Changing a number in a heap

storing

Level 2 of 3

Also called Decrease-key, Increase-key, Change-key and Priority update.

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 changing a number in a heap's own run on the numbers shown.

choose one

question 1

The heap drawn was made by putting in 32, 8, 85, 66, 19, 10 and 15. Then 10 changes to 86. At which place does 86 stop?

12345678191066328515b8e19f10d66a32c85g15
the answer, and why

the answer is A

10 stands at place 3, over 85 and 15. 86 is bigger than both, so it swaps with the smaller, 15, down to place 7, which has no child.

if you chose another

  • That swaps with 85, the first child. A sink swaps with the smaller child, 15.
  • That leaves 86 over 85 and 15, both smaller than it.
  • Place 2 is not below place 3. Place 3's children are places 6 and 7.

new numbersthe test page's numbers

question 2

On an empty heap, put 24, 34, 48, 58 and 81, change 81 to 5, then change 58 to 18. How many comparisons does the site count?

the answer, and why

the answer is C

The puts compare 4 times. 5 climbs from place 5 to the top, 2 comparisons. 18 climbs from place 4 above 24, then stops under 5, 2 more.

if you chose another

  • That counts the choices of climb or sink, 5 against 81 and 18 against 58. They compare new with old, so the site leaves them out.
  • That counts the puts only. The changes climb too.
  • That leaves out the comparison that stops 18 under 5.

new numbersthe test page's numbers

question 3

Each list puts 11 to 17 into an empty heap, smallest first, then makes one change; 17->80 means change 17 to 80. Which list makes the most comparisons?

the answer, and why

the answer is C

With 11, 12, 13, 14, 15, 16, 17, 12->88, the heap makes 8 comparisons, the most of the four.

if you chose another

  • That list makes 6 comparisons, fewer than 8.
  • That list makes 6 comparisons, fewer than 8.
  • That list makes 6 comparisons, fewer than 8.

new numbersthe test page's numbers