Skip to content

Hash table that grows

storing

Level 2 of 3

Also called Resizing a hash table, Rehashing and Dynamic hash table.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

A table that grows starts with 2 empty slots and puts in 1, 2, 3, 4 and 5. How many numbers are moved in all?

the answer, and why

the answer is C

The slots double before the 2nd, 3rd and 5th numbers, moving 1, 2 and 4. 1 + 2 + 4 is 7.

if you chose another

  • That counts only the last doubling, before the 5th number.
  • That counts one move a number, but the 1st and 4th move nothing and the 5th moves 4.
  • That counts up to the 9th number, which moves 8 more.

question 2

A table that grows starts with 2 empty slots and puts in 17, 10, then 24. Which slot does 24 go in?

the answer, and why

the answer is D

Before 24, 3 numbers in 4 slots would be more than half, so the slots double to 8. 24 leaves 0 when divided by 8, and slot 0 is empty.

if you chose another

  • Slot 2 holds 10. 24's home in 8 slots is 0.
  • Slot 1 holds 17. 24's home in 8 slots is 0.
  • That is the next slot after 10, but 24's own home is empty.

question 3

A table that grows starts with 2 empty slots and puts in 5 different numbers. How many slots does it end with?

the answer, and why

the answer is D

5 numbers in 8 slots is more than half, so before the 5th the slots double from 8 to 16.

if you chose another

  • 8 slots hold 4 numbers at half full; the 5th doubles them.
  • The slots only double, 2, 4, 8, 16, so 10 never comes.
  • 16 slots hold 8 numbers at half full, so 32 comes only at the 9th.

question 4

Which is true of a table that grows, with different numbers put in?

the answer, and why

the answer is D

The slots double when the count passes half, whichever the numbers, and each doubling moves every number in.

this tests where the formula comes from, on the learn page

if you chose another

  • Sharing a home makes more probes, not more moves.
  • Only a put that doubles the slots moves numbers; the 4th moves none.
  • The moves are p−1p - 1, with pp half the slots at the end, at most 2n−12n - 1.

pen and paper

question 5about 6 minutes

A table that grows starts with 2 empty slots and puts in 1 to 9, one at a time.

  1. Which puts double the slots, and how many numbers does each move?
  2. How many moves in all?
  3. Check it against p−1p - 1, from section 4 of the learn page, with pp half the slots at the end.
a hint

Before each put, ask whether it would make the table more than half full.

a worked answer

The 2nd, 3rd, 5th and 9th puts double the slots, moving 1, 2, 4 and 8. They add to 15. After 9 numbers there are 32 slots, so pp is 16, and 16 less 1 is 15.

putslots beforemoves
221
342
584
9168
The four puts that double, and what each moves. 15 in all.
Why do nn different numbers make p−1p - 1 moves, whichever they are, and under 2n2n?
  1. the move: the slots double before half is passed

    the line:
    a new number that would fillmore than half doubles them\begin{gathered} \text{a new number that would fill} \\ \text{more than half doubles them} \end{gathered}

    why it is allowed: The table starts with 2 slots and is never more than half full. The 9th new number finds 8 numbers in 16 slots, half; with it, 9 is more than half, so the slots double. In general, with the kkth new number kk are in, so it doubles them when kk is more than half of them. Only the count decides it, never which numbers.

  2. the move: price a doubling

    the line:
    a doubling moves everynumber in, exactly halfthe slots: 1, 2, 4, and on\begin{gathered} \text{a doubling moves every} \\ \text{number in, exactly half} \\ \text{the slots: 1, 2, 4, and on} \end{gathered}
    1. step 30 of 39

      01234567081

      Slot 1 is empty, so put 1 there.

      slots looked at: 2

    2. step 32 of 39

      012345670812

      Slot 2 is empty, so put 2 there.

      slots looked at: 3

    3. step 34 of 39

      0123456708123

      Slot 3 is empty, so put 3 there.

      slots looked at: 4

    4. step 36 of 39

      01234567081234

      Slot 4 is empty, so put 4 there.

      slots looked at: 5

    Operation 5 puts 5 in. 5 numbers in 8 slots is more than half, so the slots double to 16, and all 4 numbers move.

    why it is allowed: By line 1, before a new number that doubles them, the kkth, one fewer than kk fill at most half the slots, and kk fill more than half. So half the slots is one fewer than kk, and all of them move. In 16 slots, half is 8: the numbers in are at most 8, and with one more they are over 8, so they are exactly 8. The slots run 2, 4, 8, so it moves 1, then 2, then 4. Before the 5th number, 4 numbers fill 8 slots to half, and all 4 move.

  3. the move: the last half

    the line:
    after n numbers, halfthe slots is p, the smallestpower of 2 at or above n\begin{gathered} \text{after } n \text{ numbers, half} \\ \text{the slots is } p \text{, the smallest} \\ \text{power of 2 at or above } n \end{gathered}

    why it is allowed: For 9, the 9th doubled 16 slots to 32, so pp is 16: 8 is below 9, and 16 is at or above 9. In general, half the slots runs 1, 2, 4, and on, so pp is a power of 2. The table is at most half full, so pp is at least nn. By line 2, the last doubling came at some new number up to the nnth, when the numbers in, fewer than nn, filled half the old slots. The doubling made twice pp slots from pp, so the numbers in filled half of pp, and half of pp is below nn.

  4. the move: add the moves

    the line:
    M=1+2+4+⋯+p2M = 1 + 2 + 4 + \dots + \frac{p}{2}

    why it is allowed: MM is the moves of nn different numbers. By lines 2 and 3, the doublings move 1, 2, 4, and on, up to half of pp, the last one. For 9, 1 + 2 + 4 + 8, and pp is 16.

  5. the move: add the doublings

    the line:
    M=p−1M = p - 1
    where does this come from?
    question 1 of the page doubling and halving sumsAdd 1+2+4+⋯+2n1 + 2 + 4 + \dots + 2^n. What is the total?
    1. the move: name the sum

      the line:
      S=1+2+4+⋯+2nS = 1 + 2 + 4 + \dots + 2^n

      why it is allowed: Let SS be the doubling sum up to 2n2^n. For nn as 3, it is 1 + 2 + 4 + 8.

    2. the move: double both sides

      the line:
      2S=2+4+8+⋯+2n+12S = 2 + 4 + 8 + \dots + 2^{n + 1}

      why it is allowed: Each term doubled is the next one along, and 2n2^n becomes 2n+12^{n + 1}. Doubling both sides keeps them equal. The plan: the two sums share all but their ends.

    3. the move: take line 1 away

      the line:
      2S−S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} 2S - S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Take SS from both sides of line 2. On the right, write SS out as line 1 does.

    4. the move: tidy the left side

      the line:
      S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Two lots of SS less one lot is one lot.

    5. the move: cancel what both brackets share

      the line:
      S=2n+1−1S = 2^{n + 1} - 1
      when n is 3124816
      first bracket24816
      second bracket1248
      left overtake away 116
      Line 4 when nn is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15.

      why it is allowed: Every term from 2 to 2n2^n is in both brackets and cancels, leaving 2n+12^{n + 1} less 1.

    6. the move: the terms before the last

      the line:
      1+2+⋯+2n−1=2(n−1)+1−1\begin{gathered} 1 + 2 + \dots + 2^{n - 1} \\ = 2^{(n - 1) + 1} - 1 \end{gathered}

      why it is allowed: The sum of line 5 stopping one term sooner: line 5 with n−1n - 1 for nn. For 3, it is 1 + 2 + 4. At nn as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.

    7. the move: tidy the power

      the line:
      2(n−1)+1−1=2n−12^{(n - 1) + 1} - 1 = 2^n - 1

      why it is allowed: One more than n−1n - 1 is nn. For 3, 1 + 2 + 4 is 7, one less than 8.

    8. the move: read it

      the line:
      one less thanthe next term,under twice the last\begin{gathered} \text{one less than} \\ \text{the next term,} \\ \text{under twice the last} \end{gathered}

      why it is allowed: By lines 6 and 7, the terms before the last add to one less than it: the last outweighs them all. By line 5 the sum is under 2n+12^{n + 1}, twice the last.

    the whole page: doubling and halving sums

    why it is allowed: The box under this line shows a doubling sum from 1 is one less than the next doubling. In the box, nn is how many times 1 doubles to reach the last number added. It is not the numbers: for 9 numbers it is 3, as 1 doubled 3 times is 8, half of pp. After half of pp comes pp. For 9, 15 is 16 less 1.

  6. the move: half the slots is under twice the numbers

    the line:
    p<2np < 2n

    why it is allowed: By line 3, half of pp is below nn. Doubling both, pp is below 2n2n. For 9, 8 is below 9, so 16 is below 18.

  7. the move: a ceiling

    the line:
    M≤2n−1M \le 2n - 1

    why it is allowed: By lines 5 and 6, MM is pp less 1, and pp is below 2n2n, so MM is below 2n−12n - 1. For 9, 15 is at most 17.

  8. the move: read it as a bound

    the line:
    M∈O(n)M \in O(n)

    why it is allowed: By line 7, with the fixed multiple 2.

  9. the move: half the slots holds every number

    the line:
    p≥np \ge n

    why it is allowed: By line 3. For 9, 16 is at least 9.

  10. the move: a floor

    the line:
    M≥n−1M \ge n - 1

    why it is allowed: By lines 5 and 9, pp less 1 is at least nn less 1. For 9, 15 is at least 8.

  11. the move: a simpler floor

    the line:
    M≥n2 for n≥2M \ge \frac{n}{2} \text{ for } n \ge 2

    why it is allowed: From 2 on, nn less 1 is at least half of nn, as half of nn is at least 1. For 2, 1 is at least 1.

  12. the move: read it as a bound

    the line:
    M∈Ω(n)M \in \Omega(n)

    why it is allowed: By line 11, with the fixed multiple one half, from 2 numbers on.

  13. the move: ceiling and floor together

    the line:
    M∈Θ(n)M \in \Theta(n)

    why it is allowed: Lines 8 and 12 bound MM by the same growth, nn.

  14. the move: share it out

    the line:
    Mn≤2−1n\frac{M}{n} \le 2 - \frac{1}{n}

    why it is allowed: Line 7 divided by nn shares the moves over the nn numbers, the amortised cost. For 9, 15 over 9 is about 1.67, at most 2 less 1 over 9, about 1.89.

try it with a small nn: at n=8n = 8, line 4 gives M=7M = 7, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • The 2nd, 3rd, 5th and 9th puts, moving 1, 2, 4 and 8.
  • 15 moves.
  • The formula giving 15 for pp of 16.

question 6about 8 minutes

A table that grows starts with 2 empty slots and puts in 1, 17 and 33, every one with home 1 in every room.

  1. Count the probes of each put.
  2. How many probes in all?
a hint

Each walk passes every number before it. Count the moves' walks and the walk again after each doubling.

a worked answer

1 makes 1 probe. 17 makes 2, finding slot 0 empty. The slots double to 4: 1 moves in 1 probe, and 17 looks again, 2 probes. 33 makes 3 in 4 slots, slot 3 empty. They double to 8: 1 moves in 1, 17 in 2, and 33 looks again, 3. So 1, 5 and 9 probes, 15 in all.

  1. step 16 of 29

    01234567117

    Slot 1 holds 1, so look at the next slot for 33.

    slots looked at: 1

  2. step 17 of 29

    01234567117

    Slot 2 holds 17, so look at the next slot for 33.

    slots looked at: 2

  3. step 18 of 29

    01234567117

    Slot 3 is empty, so 33 is not in the table yet.

    slots looked at: 3

  4. step 21 of 29

    012345671

    Slot 1 is empty, so put 1 there.

    slots looked at: 4

  5. step 23 of 29

    012345671

    Slot 1 holds 1, so look at the next slot for 17.

    slots looked at: 5

  6. step 24 of 29

    01234567117

    Slot 2 is empty, so put 17 there.

    slots looked at: 6

  7. step 26 of 29

    01234567117

    Slot 1 holds 1, so look at the next slot for 33.

    slots looked at: 7

  8. step 27 of 29

    01234567117

    Slot 2 holds 17, so look at the next slot for 33.

    slots looked at: 8

  9. step 28 of 29

    0123456711733

    Put 33 in slot 3.

    slots looked at: 9

Operation 3 puts 33 in, 9 probes.

this tests how it works, on the learn page

what a full answer includes
  • 1, 5 and 9 probes.
  • 15 probes in all.