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, 29, 23, 47, 35, 40 and 31. Then 23 changes to 50. At which place does 50 stop?

123456716292347354031a16b29c23d47e35f40g31
the answer, and why

the answer is B

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

  1. step 16 of 22

    16292347354031

    23 stands in place 3. The heap keeps a note of where each number stands, so this is one look and no comparison.

    comparisons: 0

  2. step 20 of 22

    16293147354050

    Swap them: 50 sinks below 31.

    comparisons: 2

  3. step 21 of 22

    16293147354050

    Nothing is below 50, so it stays.

    comparisons: 2

23 at place 3 becomes 50, sinks below 31, the smaller child, and stops. 2 comparisons.

if you chose another

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

new numbers

question 2

On an empty heap, put 6, 7, 8, 9 and 10, change 10 to 1, then change 9 to 2. How many comparisons does the site count?

the answer, and why

the answer is D

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

  • 627189100

    after the puts; 10 at place 5 becomes 1 and climbs 2 levels

  • 16897

    after that; 9 at place 4 becomes 2, climbs above 6 and stops under 1

The heap after the puts, then after 10 becomes 1. In all, 4 + 2 + 2 = 8.

if you chose another

  • That counts the choices of climb or sink, 1 against 10 and 2 against 9. 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 2 under 1.

new numbers

question 3

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

the answer, and why

the answer is A

With 11->20, the top made 20 sinks two levels, comparing both children at each, 4 comparisons; the others cost 2. With the puts' 6, 11->20 costs 6 + 4 = 10, the others 6 + 2 = 8.

11121314151617111122133144155166177
Places 1, 7, 4, 3 change: 11->20, 17->1, 14->2, 13->30.

if you chose another

  • 17 is at the last place. Made 1, it climbs two levels, one comparison each.
  • 14 is at place 4. Made 2, it climbs two levels too, one comparison each.
  • 13 is at place 3. Made 30, it sinks one level, then stops at place 6, which has no child.

new numbers