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 16, 31, 60, 27, 53, 10 and 40. Then 16 changes to 69. At which place does 69 stop?

123456710271631536040f10d27a16b31e53c60g40
the answer, and why

the answer is A

16 stands at place 3, over 60 and 40. 69 is bigger than both, so it swaps with the smaller, 40, down to place 7, which has no child.

if you chose another

  • That swaps with 60, the first child. A sink swaps with the smaller child, 40.
  • That leaves 69 over 60 and 40, 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 49, 55, 58, 77 and 97, change 97 to 28, then change 77 to 43. How many comparisons does the site count?

the answer, and why

the answer is B

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

if you chose another

  • That counts the choices of climb or sink, 28 against 97 and 43 against 77. 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 43 under 28.

new numbersthe test page's numbers

question 3

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

the answer, and why

the answer is B

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

if you chose another

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

new numbersthe test page's numbers