Skip to content

Hash table with open addressing

storing

Level 2 of 3

Also called Linear probing.

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

An empty table of 7 slots puts in 12, 5, 19 and 6, in that order. How many probes do the four puts make in all?

the answer, and why

the answer is D

12, 5 and 19 have home 5; 6 has home 6. 12 takes slot 5, 1 probe. 5 walks to slot 6, 2 probes. 19 walks slots 5 and 6 to slot 0, 3 probes. 6 finds slot 6 taken by 5, then slot 0 by 19, and takes slot 1, 3 probes. That is 9.

  1. step 11 of 15

    012345619125

    6 leaves 6 when divided by 7, so its home is slot 6.

    slots looked at: 0

  2. step 12 of 15

    012345619125

    Slot 6 holds 5, not 6.

    slots looked at: 1

  3. step 13 of 15

    012345619125

    Slot 0 holds 19, not 6.

    slots looked at: 2

  4. step 14 of 15

    0123456196125

    Slot 1 is free, so put 6 there.

    slots looked at: 3

Operation 4 puts 6 in. Its home, slot 6, already holds 5, and slot 0 holds 19. 3 probes, though 6 shares its home with no number.

if you chose another

  • That counts 1 probe a put, as if every home were free.
  • That counts 6 as 1 probe, as if its home were free, but 5 took slot 6 on its walk.
  • That leaves out 6's put, which makes 3 probes.

question 2

An empty table of 7 slots puts in 17, 24 and 38, then 31. Which slot does 31 go in?

the answer, and why

the answer is C

All four have home 3. 17 takes slot 3, 24 slot 4, 38 slot 5, so 31 walks 3, 4 and 5 to slot 6.

if you chose another

  • Slot 3 is 31's home, but 17 already holds it.
  • That stops at the first slot after its home, two too early; slot 4 holds 24.
  • That walks one slot too far; slot 6 is free.

question 3

25, 32 and 39, all home 4, fill slots 4, 5 and 6. Then 32 is taken out, but say its slot were cleared, with no marker. What would a search for 39 report?

the answer, and why

the answer is D

The search looks at slot 4, 25, then slot 5, now empty, and stops there. So it reports 39 missing, though 39 sits in slot 6. A marker in slot 5 keeps the walk going.

this tests how it works, on the learn page

if you chose another

  • That is what the table does with the marker. Emptied, slot 5 ends the search first.
  • Slot 4 holds 25, not 39.
  • A search walks all 7 slots only when it meets no empty slot before its last.

question 4

Which is true of this page's table of 7 slots, with different numbers put in?

the answer, and why

the answer is C

The 7 slots are full after 7 numbers, so every later put walks all the way round and finds no free slot.

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

if you chose another

  • A put probes 1 slot only when its home is free; with one home, the 4th put probes 4.
  • A walk never looks at more than the 7 slots, so the 20th put probes 7 and is turned away.
  • The free slot is 1 to 7 probes away, 4 on average.

pen and paper

question 5about 6 minutes

In a table of 7 slots, every slot holds a number except slot 2. A put of a new number has its home at any of the 7 slots, each as likely.

  1. For each home, 0 to 6, how many probes does the put make?
  2. What is the average?
  3. Check it against n+12\frac{n + 1}{2}, the formula of section 4, with nn the 7 slots.
a hint

The put walks from its home to slot 2, round to the start if it must.

a worked answer

Home 2 makes 1 probe, home 1 makes 2, home 0 makes 3, home 6 makes 4, home 5 makes 5, home 4 makes 6 and home 3 makes 7. They add to 28, and 28 over 7 is 4. The formula with 7 slots gives 8 over 2, also 4.

homeits walkprobes
221
11, 22
00, 1, 23
66, 0, 1, 24
55, 6, 0, 1, 25
44, 5, 6, 0, 1, 26
3all 77
Each home a different distance from slot 2, 1 to 7.
Here let nn count slots, not numbers put in. A table of nn slots has one free slot. How many probes does a put of a new number make, on average over its home?
  1. the move: walk to the free slot

    the line:
    the put probes from its hometo the free slot: 1 up to n\begin{gathered} \text{the put probes from its home} \\ \text{to the free slot: 1 up to } n \end{gathered}
    homeits walkprobes
    551
    44, 52
    33, 4, 53
    22 to 54
    11 to 55
    00 to 56
    66, 0 to 57
    7 slots, only slot 5 free. Each home gives a different number of probes, 1 to 7.

    why it is allowed: It looks at its home, then the next slots, round to the start, and stops at the one free slot. Its home is any of the nn slots, each as likely, and each home is a different distance away.

  2. the move: average over the homes

    the line:
    L=1+2+⋯+nnL = \frac{1 + 2 + \dots + n}{n}

    why it is allowed: LL is the probes averaged over the nn homes. By line 1, the homes give 1 up to nn, one each. For 7, 28 over 7.

  3. the move: use the page on adding up

    the line:
    L=n(n+1)2nL = \frac{\frac{n(n + 1)}{2}}{n}
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

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

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15.

      why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds nn standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    why it is allowed: The box under this line shows that 1 up to a last number adds to that number times one more, halved. Here the last number is nn. For 7, 7 times 8, halved, is 28.

  4. the move: halve and divide at once

    the line:
    L=n(n+1)2nL = \frac{n(n + 1)}{2n}

    why it is allowed: Halved, then over nn, is over twice nn. For 7, 7 times 8 over 14 is 4.

  5. the move: tidy

    the line:
    L=n+12L = \frac{n + 1}{2}

    why it is allowed: The nn on top and below cancel. For 7 slots, 8 over 2 is 4, section 3's average for the 7th put, which finds 6 numbers in 7 slots.

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

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

what a full answer includes
  • 1 to 7 probes, one for each home.
  • The average, 28 over 7, 4.
  • The formula giving 4 at 7 slots.

question 6about 8 minutes

An empty table of 7 slots puts in 9, 16 and 23, takes 16 out, then puts in 30.

  1. Where does each of 9, 16 and 23 go?
  2. What does taking 16 out leave?
  3. Which slot does 30 go in, and after how many probes?
a hint

All four have home 2. A put notes the first marker it passes, but walks on to a free slot first.

a worked answer

9 takes slot 2, 16 slot 3 and 23 slot 4. Taking 16 out leaves a marker in slot 3. 30 looks at slot 2 (9), slot 3 (the marker, noted), slot 4 (23) and slot 5, free: 4 probes. 30 is nowhere further along, so it goes in slot 3, where the marker was.

  1. step 15 of 20

    01234569×23

    30 leaves 2 when divided by 7, so its home is slot 2.

    slots looked at: 0

  2. step 16 of 20

    01234569×23

    Slot 2 holds 9, not 30.

    slots looked at: 1

  3. step 17 of 20

    01234569×23

    Slot 3 holds a marker left by a number taken out. Note it: 30 can go here if it is nowhere further along.

    slots looked at: 2

  4. step 18 of 20

    01234569×23

    Slot 4 holds 23, not 30.

    slots looked at: 3

  5. step 19 of 20

    012345693023

    30 is nowhere further along, so put it in slot 3, where the first marker was.

    slots looked at: 4

Operation 5 puts 30 in. It notes the marker in slot 3, walks on to the free slot 5, then goes in slot 3. 4 probes.

this tests how it works, on the learn page

what a full answer includes
  • Slots 2, 3 and 4.
  • A marker in slot 3.
  • Slot 3, after 4 probes.