Data Structures · Hashing

Linear probing with open addressing

In open addressing every key lives inside the table itself. When a key's home slot is taken, linear probing simply tries the next slot, then the next, wrapping around at the end. The rules are short, but deleting needs care, and the table slows down sharply as it fills.

How it works

The probe sequence

A key k starts at its home slot h(k) = k % m, where m is the table size. If that slot is not the one we want, try h(k)+1, h(k)+2, …, all mod m. Every operation walks this same sequence.

Insert, search, delete

Insert: probe until an empty slot (or a deleted one) and put the key there. Search: probe until you find the key, or reach an empty slot, which proves the key is absent. Delete: find the key, then mark the slot deleted instead of emptying it.

Why a "deleted" marker?

Search stops at the first empty slot. If delete just emptied a slot, a key placed after it in the same run would become unreachable. A deleted marker (a "tombstone") says "keep probing." Insert may reuse a tombstone; search must walk past it.

Clusters and load factor

Keys that collide pile up in runs of filled slots called clusters, and a key landing anywhere in a cluster makes it longer (primary clustering). The load factor α = n / m tells you how full the table is. Keep α below about 0.7.

Try it

The table: HashNode* arr[10]

Empty (nullptr) Holds a key Deleted (tombstone) Being probed Cluster (red when 4 or longer) home marks h(key)

What is happening

Numbers

Calls

    HashMap.cpp

      How fast is it?

      With a good hash function and a table that is not too full, each operation looks at only a couple of slots, so it is O(1) on average. When the table is nearly full, or many keys share one home slot, a probe can walk across most of the table: O(N).

      OperationBest caseAverage caseWorst caseExtra space
      InsertO(1)O(1) good hash, O(N) bad hashO(N)O(1)
      SearchO(1)O(1) good hash, O(N) bad hashO(N)O(1)
      DeleteO(1)O(1) good hash, O(N) bad hashO(N)O(1)

      How full is too full? For linear probing the expected number of probes grows like 1 / (1 − α)². At α = 0.5 an unsuccessful search takes about 2.5 probes; at α = 0.9 it takes about 50. The Numbers panel above shows these estimates for the current table. When α gets high, real hash tables allocate a bigger array and re-insert every key (rehashing). Changing the capacity in the lab does exactly that.

      Details the code takes care of

      The HashMap in the code panel handles four cases that are easy to get wrong:

      1. No duplicates after a delete. insertNode remembers the first tombstone it passes, but keeps probing until an empty slot to make sure the key is not already stored further along. Only then does it reuse the tombstone.
      2. Updating a key. If the key is already in the table, the code changes arr[i]->value in place instead of allocating a new node, so no memory is leaked.
      3. Negative keys. In C++, -7 % 10 is -7, which is not a valid index. hashCode uses ((key % capacity) + capacity) % capacity so the index is always 0 to capacity − 1.
      4. Telling tombstones apart. The tombstone is a single dummy node. The code always tests arr[i] == dummy before comparing keys, so no real key can be mistaken for it.