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 80, 87, 89, 79, 17, 58 and 61. Then 58 changes to 95. At which place does 95 stop?

123456717795887808961e17d79f58b87a80c89g61
the answer, and why

the answer is A

58 stands at place 3, over 89 and 61. 95 is bigger than both, so it swaps with the smaller, 61, down to place 7, which has no child.

if you chose another

  • That swaps with 89, the first child. A sink swaps with the smaller child, 61.
  • That leaves 95 over 89 and 61, 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 21, 83, 89, 94 and 96, change 96 to 7, then change 94 to 19. How many comparisons does the site count?

the answer, and why

the answer is C

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

if you chose another

  • That counts the choices of climb or sink, 7 against 96 and 19 against 94. 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 19 under 7.

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->69 means change 14 to 69. Which list makes the most comparisons?

the answer, and why

the answer is D

With 11, 12, 13, 14, 15, 16, 17, 16->5, 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