Tracing Linear Probing in a Hash Table

Linear probing visits successive slots after a collision, wrapping according to the table's capacity.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Walk two inserts
  2. Understand an early stop
  3. Trace a search over a deleted slot
  4. Distinguish a probe stopping condition from a reusable slot

Walk two inserts

With five slots and initial index equal to the remainder by five, insert 1 into slot one and 6 into slot two after a collision. Looking up 6 starts at one and checks the next slot. Remainder implements wrap from slot four to zero.

Understand an early stop

If deleting 1 made its slot look never used, lookup might stop before reaching 6. A tombstone distinguishes deletion from unused space. See open-addressing deletions for repair and rebuild strategies.[1]

Trace a search over a deleted slot

In capacity seven with remainder as the index, keys six and thirteen begin at index six. Insert six there and thirteen at wrapped index zero. If deletion of six marks its slot as never used, a later search can stop before reaching thirteen. See hash collisions.

Before removal: slot 6 = key 6, slot 0 = key 13
After removal: slot 6 = tombstone, slot 0 = key 13
Search 13 checks 6, then wraps to 0

Distinguish a probe stopping condition from a reusable slot

A never-used slot can rule out a key farther along an unchanged linear probe sequence. A tombstone preserves that sequence. Insertion may reuse a tombstone after ensuring the key is not already present. Test a cluster that wraps around the end.

References

  1. Jeff Erickson, Algorithms ↩