Chapter 12 · Simulation

Hash Table Simulator

← Chapter 12

Hash computation

hash(key) = FNV-1a hash mod capacity

Steps

  1. No operations yet.

History

  1. No operations yet.

Hash Table Simulator

A hash table with separate chaining: hashing turns each key into a bucket index, and every bucket keeps a chain (a list) of the entries that land there.

Controls

  • Type a Key and a Value. Put / Update adds the entry or changes the value of an existing key; Get and Remove use only the key; Add Random inserts a random entry. The table holds at most 12 entries.
  • Capacity sets the initial number of buckets; changing it empties the table (Clear and Load Example use it too). Auto-resize doubles the capacity once the load factor exceeds 0.75.
  • Load Example brings back the sample table. Cancel stops the running animation: if nothing has changed yet the table stays as it was, and a pending resize is completed.

The hash function

An FNV-1a-style hash: start from 2,166,136,261; for each character, XOR in its code and multiply by 16,777,619 (32-bit arithmetic). The bucket index is hash mod capacity.

Notes

  • A collision happens when two keys land in the same bucket; once the chain is known not to contain the key, the new entry goes at its end.
  • The load factor λ = entries / capacity is the average chain length.
  • Resizing rehashes every entry, because the index depends on the capacity.

Complexity

Put, get and remove take O(1 + λ) time on average, degrading to O(n) if every key lands in one bucket. A resize costs O(n) but happens rarely, so insertion stays amortized O(1).