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 65, 72, 79, 73, 81, 14 and 63. Then 63 changes to 95. At which place does 95 stop?

123456714726373817965f14b72g63d73e81c79a65
the answer, and why

the answer is B

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

if you chose another

  • That swaps with 79, the first child. A sink swaps with the smaller child, 65.
  • That leaves 95 over 79 and 65, 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 32, 35, 67, 79 and 92, change 92 to 10, then change 79 to 11. How many comparisons does the site count?

the answer, and why

the answer is A

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

if you chose another

  • That counts the choices of climb or sink, 10 against 92 and 11 against 79. 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 11 under 10.

new numbersthe test page's numbers

question 3

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

the answer, and why

the answer is C

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

if you chose another

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

new numbersthe test page's numbers