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 64, 49, 62, 72, 18, 25 and 46. Then 25 changes to 89. At which place does 89 stop?

123456718492572646246e18b49f25d72a64c62g46
the answer, and why

the answer is C

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

if you chose another

  • That swaps with 62, the first child. A sink swaps with the smaller child, 46.
  • That leaves 89 over 62 and 46, 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 52, 55, 89, 92 and 97, change 97 to 2, then change 92 to 38. How many comparisons does the site count?

the answer, and why

the answer is D

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

if you chose another

  • That counts the choices of climb or sink, 2 against 97 and 38 against 92. 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 38 under 2.

new numbersthe test page's numbers

question 3

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

the answer, and why

the answer is B

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

if you chose another

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

new numbersthe test page's numbers