Chapter 14: Hash Tables

Data Structures and Algorithms with C
Direct addressingHash functionsCollisionsLinear / quadratic probingDouble hashingChainingLoad factorTombstones
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Explain the idea

Define the hash table, which computes a position from the key and searches in O(1) on average.

Choose a function

Compute division, multiplication, folding and string hashes; list what makes a function good.

Resolve collisions

Apply linear/quadratic probing, double hashing and chaining step by step.

Write it in C

Write insert, search and delete with malloc/free and the empty cases.

Delete correctly

Show why open addressing needs a deleted marker (tombstone).

Analyze

Explain how the load factor affects cost, and the amortized cost of resizing.

The Hashing Idea

Bringing search cost down to a constant

StructureSearchInsertDeleteNote
Unsorted arrayO(n)O(1)O(n)append at the end, scan from the front
Sorted arrayO(lg n)O(n)O(n)binary search + shifting
Balanced search treeO(lg n)O(lg n)O(lg n)also gives sorted traversal
Hash tableO(1)*O(1)*O(1)** average; worst case O(n)
A hash table does not compare keys: it computes the position from the key. The price: no sorted traversal, min/max or range queries; the worst case is O(n).

The Hashing Idea

The simplest case first: a direct-address table

Key universe U = {0, …, 9}, keys in use K = {2, 3, 5, 8}0122·Ali33·Can455·Ece6788·Naz9T[k] looks directly at k: search, insert, delete O(1) — but the table must be as large as |U|.
typedef struct { int used; char name[16]; } rec;
rec T[10];               /* key = index     */

rec *da_search(int k) {
  return T[k].used ? &T[k] : NULL;
}
The problem

For 9-digit keys such as student numbers, |U| = 109: the table does not fit, and most of it stays empty.

The solution

A hash function that maps keys to a small range, h: U → {0, …, m−1}.

The Hashing Idea

Key → hash → bucket

AliNazCanEceh(k)← collision: h(Ali) = h(Ece) = 3012345
TermMeaning
bucket / slota position in the table, 0 … m−1
home addressh(k): the first place probed for the key
collisionk₁ ≠ k₂ but h(k₁) = h(k₂)
synonymskeys with the same home address
load factor αn / m (how full the table is)
Since m < |U| (the pigeonhole principle), collisions are unavoidable. Two questions remain: how to choose a good h, and how to resolve collisions?

The Hashing Idea

Abstract data type: the dictionary

typedef struct { int key; /* + value */ } item;

bool hash_insert(item x);   /* update if found */
item *hash_search(int key); /* NULL if absent  */
bool hash_delete(int key); /* false if absent */
Dictionary

A key → value mapping; each key appears at most once.

Uses

Compiler symbol tables (Horowitz's motivating example), caches, duplicate detection, set implementations.

The C standard library has no ready-made hash table (POSIX has a limited hsearch), so a C programmer must know how to write one.

Hash Functions

What makes a hash function good

Fast

O(key length) to compute; a few arithmetic operations.

Spreads evenly

Keys should spread over the m buckets as evenly as possible (the simple uniform hashing assumption).

Uses all the bits

Functions that look only at the last digits cluster on patterned data.

Deterministic

The same key always goes to the same bucket; randomness may enter only when the function is chosen.

Hash Functions

Division method: h(k) = k mod m

k1020304050601727
k mod 1000000077
k mod 11109876565
  • m = 10: everything but the last digit is ignored; 10, 20, …, 60 land in the same bucket.
  • A good rule is to choose m as a prime not close to a power of 2 (CLRS).
  • If m = 2p, h(k) is just the lowest p bits of k: avoid it.
unsigned h_div(unsigned k, unsigned m) { return k % m; }

In C, % can give a negative result for a negative number: keep the key unsigned or use ((k % m) + m) % m.

Hash Functions

Multiplication method: h(k) = ⌊m · (k·A mod 1)⌋

  • A is a constant with 0 < A < 1; the fractional part of k·A is multiplied by m.
  • The choice of m is not critical; m = 2p is fine.
  • Knuth suggests A ≈ (√5 − 1)/2 = 0.6180339887…
  • Example (CLRS): k = 123456, p = 14 (m = 16384) → h(k) = 67.
/* w = 32 bit, m = 2^p; s = A * 2^32 */
uint32_t h_mul(uint32_t k, int p) {
  const uint32_t s = 2654435769u;
  return (uint32_t)(k * s) >> (32 - p);
}
In the integer version the product k·s overflows modulo 232; the overflow is discarded (same as taking the fractional part). The top p bits of the remaining 32 bits are h(k). The floating-point version gives the same result: ⌊16384 · 0.…⌋ = 67. (Needs <stdint.h>.)

Hash Functions

Folding and mid-square methods

The key x = 12320324111220 is split into three-digit parts that are added up (Horowitz's example):

123P1203P2241P3112P420P5shift: 123+203+241+112+20 = 69912330224121120boundary: 123+302+241+211+20 = 897
Shift folding

The parts are added as they are: 699.

Folding at the boundaries

Every other part is reversed (like folding paper): 897.

Mid-square

Compute k² and take the middle r bits: m = 2r. The middle bits depend on all the bits of k.

If the result exceeds the table size, also take mod m.

Hash Functions

String keys: polynomial (Horner) hashing

unsigned str_hash(const char *s, unsigned m) {
  unsigned h = 0;
  for (; *s != '\0'; s++)
    h = (h * 31 + (unsigned char)*s) % m;
  return h;
}
unsigned long djb2(const char *s) {  /* Bernstein */
  unsigned long h = 5381;
  int c;
  while ((c = (unsigned char)*s++) != 0)
    h = h * 33 + c;
  return h;       /* then: h % m */
}
Summing is bad

Adding character codes sends anagrams such as "abc" and "cba" to the same bucket.

Polynomial

h = c₀·31n−1 + … + cn−1: one multiplication per character with Horner's rule. Position information enters the hash.

Overflow

Taking % m at each step keeps values small; unsigned overflow is well defined in C (mod 2w).

String hashing · run the code step by step

Code trace: str_hash("kedi", 11)

k107e101d100i105\0characters and codesh = ?m = 11

str_hash("kedi", 11) is called.

k107e101d100i105\0characters and codesh = 0m = 11

Start with h = 0.

k107e101d100i105\0characters and codesh = 0m = 11

*s = 'k' ≠ '\0' → the loop continues.

k107e101d100i105\0characters and codesh = 8m = 11

h = (0·31 + 107) % 11 = 107 % 11 = 8.

k107e101d100i105\0characters and codesh = 8m = 11

*s = 'e' ≠ '\0' → the loop continues.

k107e101d100i105\0characters and codesh = 8m = 11

h = (8·31 + 101) % 11 = 349 % 11 = 8.

k107e101d100i105\0characters and codesh = 8m = 11

*s = 'd' ≠ '\0' → the loop continues.

k107e101d100i105\0characters and codesh = 7m = 11

h = (8·31 + 100) % 11 = 348 % 11 = 7.

k107e101d100i105\0characters and codesh = 7m = 11

*s = 'i' ≠ '\0' → the loop continues.

k107e101d100i105\0characters and codesh = 3m = 11

h = (7·31 + 105) % 11 = 322 % 11 = 3.

k107e101d100i105\0characters and codesh = 3m = 11

*s == '\0': end of string.

k107e101d100i105\0characters and codesh = 3m = 11

return 3: the home address of the key "kedi" is 3. The cost is linear in the string length.

unsigned str_hash(const char *s, unsigned m) {
  unsigned h = 0;
  for (; *s != '\0'; s++)
    h = (h * 31 + (unsigned char)*s) % m;
  return h;
}

Check: the full polynomial is 3287903, and 3287903 mod 11 = 3; taking the mod at each step does not change the result.

Open Addressing

Collisions: two basic remedies

Open addressing: look for another slot in the table0221121331145611 → home 0 full → 1 → 2 → 3Chaining: each bucket keeps a list[0][1][2]1122113
The birthday paradox

In a class of 23, the chance that two people share a birthday is ≈ 50.7%. 23 keys in m = 365 slots: collisions come surprisingly early.

Open addressing

All items live inside the table; α ≤ 1. An empty slot is found by following a probe sequence.

Chaining

Each bucket heads a linked list; α > 1 is possible. Insertion is O(1).

Open Addressing

Linear probing: h(k, i) = (h(k) + i) mod m

012345678910m = 11, h(k) = k mod 11empty table

Insert 22, 1, 13, 11, 24, 33 in order. Home addresses: 0, 1, 2, 0, 2, 0.

02212345678910▲ ihomeinsert(22) · linear1 probe

22 → home 0: slot 0.

022112345678910▲ ihomeinsert(1) · linear1 probe

1 → home 1: slot 1.

02211213345678910▲ ihomeinsert(13) · linear1 probe

13 → home 2: slot 2.

02211213345678910▲ ihomeinsert(11) · linearprobe 0: slot 0 full

11: slot 0 is full (22) → try the next slot.

02211213345678910▲ home▲ iinsert(11) · linearprobe 1: slot 1 full

11: slot 1 is full (1) → try the next slot.

02211213345678910▲ home▲ iinsert(11) · linearprobe 2: slot 2 full

11: slot 2 is full (13) → try the next slot.

0221121331145678910▲ home▲ iinsert(11) · linear4 probes

11 → home 0, probe sequence 0, 1, 2, 3: slot 3.

0221121331145678910▲ ihomeinsert(24) · linearprobe 0: slot 2 full

24: slot 2 is full (13) → try the next slot.

0221121331145678910▲ home▲ iinsert(24) · linearprobe 1: slot 3 full

24: slot 3 is full (11) → try the next slot.

022112133114245678910▲ home▲ iinsert(24) · linear3 probes

24 → home 2, probe sequence 2, 3, 4: slot 4.

022112133114245678910▲ ihomeinsert(33) · linearprobe 0: slot 0 full

33: slot 0 is full (22) → try the next slot.

022112133114245678910▲ home▲ iinsert(33) · linearprobe 1: slot 1 full

33: slot 1 is full (1) → try the next slot.

022112133114245678910▲ home▲ iinsert(33) · linearprobe 2: slot 2 full

33: slot 2 is full (13) → try the next slot.

022112133114245678910▲ home▲ iinsert(33) · linearprobe 3: slot 3 full

33: slot 3 is full (11) → try the next slot.

022112133114245678910▲ home▲ iinsert(33) · linearprobe 4: slot 4 full

33: slot 4 is full (24) → try the next slot.

02211213311424533678910▲ home▲ iinsert(33) · linear6 probes

33 → home 0, probe sequence 0, 1, 2, 3, 4, 5: slot 5.

Primary clustering: occupied slots form a contiguous block, and every new key that lands in it makes it bigger. 33 needed 6 probes.

Open Addressing

Open addressing in C: slot state and insertion

#define M 11
typedef enum { EMPTY, USED, DELETED } state_t;
typedef struct {
  int key;          /* assumes key >= 0 */
  state_t state;
} slot;
slot table[M];          /* global: all 0 = EMPTY */
Initial state

A global array starts zeroed; since EMPTY = 0, every slot is empty.

Insertion

Start at the home address and write into the first slot that is not USED; at most m probes.

A slot is in one of three states: EMPTY (never used), USED, DELETED (tombstone). Insertion may reuse a DELETED slot, but it must first check with search that the key is not already in the table.

Open addressing · run the code step by step

Code trace: insert(33) — linear probing

022112133114245678910insert(33)n = 5, α = 5/11

insert(33): the table holds 22, 1, 13, 11, 24 (slots 0–4, one full block).

022112133114245678910▲ ihomeinsert(33)

Home address i = 33 % 11 = 0.

022112133114245678910▲ ihomeinsert(33)

d = 0 < 11: probe 0, slot 0.

022112133114245678910▲ ihomeinsert(33)

Slot 0 is USED (22) → next: i = (0+1) % 11.

022112133114245678910▲ home▲ iinsert(33)

d = 1 < 11: probe 1, slot 1.

022112133114245678910▲ home▲ iinsert(33)

Slot 1 is USED (1) → next: i = (1+1) % 11.

022112133114245678910▲ home▲ iinsert(33)

d = 2 < 11: probe 2, slot 2.

022112133114245678910▲ home▲ iinsert(33)

Slot 2 is USED (13) → next: i = (2+1) % 11.

022112133114245678910▲ home▲ iinsert(33)

d = 3 < 11: probe 3, slot 3.

022112133114245678910▲ home▲ iinsert(33)

Slot 3 is USED (11) → next: i = (3+1) % 11.

022112133114245678910▲ home▲ iinsert(33)

d = 4 < 11: probe 4, slot 4.

022112133114245678910▲ home▲ iinsert(33)

Slot 4 is USED (24) → next: i = (4+1) % 11.

022112133114245678910▲ home▲ iinsert(33)

d = 5 < 11: probe 5, slot 5.

022112133114245678910▲ home▲ iinsert(33)

Slot 5 is EMPTY → write here.

02211213311424533678910▲ home▲ iinsert(33)n = 6

table[5] = {33, USED}.

02211213311424533678910insert(33)n = 6

return 5: 6 probes. Primary clustering pushed it 5 slots away from its home address.

int insert(int k) {
  int i = k % M;                 /* home addr */
  for (int d = 0; d < M; d++, i = (i + 1) % M) {
    if (table[i].state != USED) { /* free slot */
      table[i].key = k;
      table[i].state = USED;
      return i;
    }
  }
  return -1;                     /* table full */
}

Open addressing · run the code step by step

Code trace: search(24) and search(44)

02211213311424533678910search(24)

search(24) is called.

02211213311424533678910▲ ihomesearch(24)

Home address i = 24 % 11 = 2.

02211213311424533678910▲ ihomesearch(24)

Slot 2: another key (13) → i = 3.

02211213311424533678910▲ home▲ isearch(24)

Slot 3: another key (11) → i = 4.

02211213311424533678910▲ home▲ isearch(24)3 probes

Slot 4 is USED and holds 24 → return 4 (3 probes).

02211213311424533678910search(44)

search(44) is called.

02211213311424533678910▲ ihomesearch(44)

Home address i = 44 % 11 = 0.

02211213311424533678910▲ ihomesearch(44)

Slot 0: another key (22) → i = 1.

02211213311424533678910▲ home▲ isearch(44)

Slot 1: another key (1) → i = 2.

02211213311424533678910▲ home▲ isearch(44)

Slot 2: another key (13) → i = 3.

02211213311424533678910▲ home▲ isearch(44)

Slot 3: another key (11) → i = 4.

02211213311424533678910▲ home▲ isearch(44)

Slot 4: another key (24) → i = 5.

02211213311424533678910▲ home▲ isearch(44)

Slot 5: another key (33) → i = 6.

02211213311424533678910▲ home▲ isearch(44)7 probes

Slot 6 is EMPTY: had 44 been inserted, it would be here at the latest → return −1 (absent).

int search(int k) {
  int i = k % M;
  for (int d = 0; d < M; d++, i = (i + 1) % M) {
    if (table[i].state == EMPTY) return -1;
    if (table[i].state == USED &&
        table[i].key == k) return i;
  }
  return -1;
}
bool erase(int k) {
  int i = search(k);
  if (i < 0) return false;
  table[i].state = DELETED;   /* tombstone */
  return true;
}

Open Addressing

Quadratic probing: h(k, i) = (h(k) + i²) mod m

012345678910m = 11, h(k) = k mod 11empty table

Same keys: 22, 1, 13, 11, 24, 33. Probes jump 1, 4, 9, 16… away from the home address.

02212345678910▲ ihomeinsert(22) · quadratic1 probe

22 → home 0: slot 0.

022112345678910▲ ihomeinsert(1) · quadratic1 probe

1 → home 1: slot 1.

02211213345678910▲ ihomeinsert(13) · quadratic1 probe

13 → home 2: slot 2.

02211213345678910▲ ihomeinsert(11) · quadraticprobe 0: slot 0 full

11: slot 0 is full (22) → try the next slot.

02211213345678910▲ home▲ iinsert(11) · quadraticprobe 1: slot 1 full

11: slot 1 is full (1) → try the next slot.

0221121334115678910▲ home▲ iinsert(11) · quadratic3 probes

11 → home 0, probe sequence 0, 1, 4: slot 4.

0221121334115678910▲ ihomeinsert(24) · quadraticprobe 0: slot 2 full

24: slot 2 is full (13) → try the next slot.

022112133244115678910▲ home▲ iinsert(24) · quadratic2 probes

24 → home 2, probe sequence 2, 3: slot 3.

022112133244115678910▲ ihomeinsert(33) · quadraticprobe 0: slot 0 full

33: slot 0 is full (22) → try the next slot.

022112133244115678910▲ home▲ iinsert(33) · quadraticprobe 1: slot 1 full

33: slot 1 is full (1) → try the next slot.

022112133244115678910▲ home▲ iinsert(33) · quadraticprobe 2: slot 4 full

33: slot 4 is full (11) → try the next slot.

02211213324411567893310▲ home▲ iinsert(33) · quadratic4 probes

33 → home 0, probe sequence 0, 1, 4, 9: slot 9.

Primary clustering goes away; keys with the same home address still follow the same sequence (secondary clustering). If m is prime and α ≤ 1/2, an empty slot is guaranteed to be found.

Open Addressing

Double hashing: h(k, i) = (h₁(k) + i · h₂(k)) mod m

012345678910h₁ = k mod 11, h₂ = 1 + (k mod 10)empty table

The step size depends on the key: 11 → h₂ = 2, 24 → h₂ = 5, 33 → h₂ = 4.

02212345678910▲ ihomeinsert(22) · double hashing1 probe

22 → home 0: slot 0.

022112345678910▲ ihomeinsert(1) · double hashing1 probe

1 → home 1: slot 1.

02211213345678910▲ ihomeinsert(13) · double hashing1 probe

13 → home 2: slot 2.

02211213345678910▲ ihomeinsert(11) · double hashingprobe 0: slot 0 full

11: slot 0 is full (22) → try the next slot.

02211213345678910▲ home▲ iinsert(11) · double hashingprobe 1: slot 2 full

11: slot 2 is full (13) → try the next slot.

0221121334115678910▲ home▲ iinsert(11) · double hashing3 probes

11 → home 0, probe sequence 0, 2, 4: slot 4.

0221121334115678910▲ ihomeinsert(24) · double hashingprobe 0: slot 2 full

24: slot 2 is full (13) → try the next slot.

022112133411567248910▲ home▲ iinsert(24) · double hashing2 probes

24 → home 2, probe sequence 2, 7: slot 7.

022112133411567248910▲ ihomeinsert(33) · double hashingprobe 0: slot 0 full

33: slot 0 is full (22) → try the next slot.

022112133411567248910▲ home▲ iinsert(33) · double hashingprobe 1: slot 4 full

33: slot 4 is full (11) → try the next slot.

02211213341156724833910▲ home▲ iinsert(33) · double hashing3 probes

33 → home 0, probe sequence 0, 4, 8: slot 8.

h₂(k) must never be 0 and must be coprime to m (automatic if m is prime). The probe sequence is specific to the key, so clustering is minimal.

Open Addressing

One table, three probing methods: where does 33 land?

Methodprobe iprobe sequence of 33Probes
Linearh + i0, 1, 2, 3, 4, 56
Quadratich + i²0, 1, 4, 94
Double hashingh₁ + i·h₂0, 4, 83
MethodClusteringDistinct probe sequences
Linearprimary (blocks)m
Quadraticsecondary (same home address)m
Double hashingminimal≈ m²
Linear probing is cache-friendly and very fast while α is small; as α grows, clustering makes the cost climb quickly.

Chaining

Separate chaining: one list per bucket

#define MZ 5
typedef struct node {
  int key;
  struct node *next;
} node;
node *bucket[MZ];           /* all NULL */
bool chain_insert(int k) {
  int i = k % MZ;
  node *np = malloc(sizeof *np);
  if (np == NULL) return false;  /* no memory */
  np->key = k;
  np->next = bucket[i];         /* at the head */
  bucket[i] = np;
  return true;
}
Insert at the head

The order in the list does not matter: inserting at the head is O(1). (If duplicates are not allowed, search first.)

Search

Only the list of its own bucket is scanned: 1 + α/2 nodes on average.

Easy deletion

Deleting from a linked list is enough; no tombstone is needed.

Chaining · run the code step by step

Code trace: chain_insert 12, 7, 22 (all in bucket 2)

insert(12)bucket[0]10∅bucket[1]∅bucket[2]∅bucket[3]∅bucket[4]4∅

chain_insert(12).

insert(12)bucket[0]10∅bucket[1]∅bucket[2]∅bucket[3]∅bucket[4]4∅

i = 12 % 5 = 2.

insert(12)bucket[0]10∅bucket[1]∅bucket[2]∅bucket[3]∅bucket[4]4∅?np (malloc)

Memory for a new node is allocated; it is not NULL, so continue.

insert(12)bucket[0]10∅bucket[1]∅bucket[2]∅bucket[3]∅bucket[4]4∅12np (malloc)

key = 12; next = bucket[2] (NULL): the new node points to the old head.

insert(12)bucket[0]10∅bucket[1]∅bucket[2]12∅bucket[3]∅bucket[4]4∅

bucket[2] = np: 12 is at the head of the list. O(1).

insert(7)bucket[0]10∅bucket[1]∅bucket[2]12∅bucket[3]∅bucket[4]4∅

chain_insert(7).

insert(7)bucket[0]10∅bucket[1]∅bucket[2]12∅bucket[3]∅bucket[4]4∅

i = 7 % 5 = 2.

insert(7)bucket[0]10∅bucket[1]∅bucket[2]12∅bucket[3]∅bucket[4]4∅?np (malloc)

Memory for a new node is allocated; it is not NULL, so continue.

insert(7)bucket[0]10∅bucket[1]∅bucket[2]12∅bucket[3]∅bucket[4]4∅7np (malloc)

key = 7; next = bucket[2] (&node(12)): the new node points to the old head.

insert(7)bucket[0]10∅bucket[1]∅bucket[2]712∅bucket[3]∅bucket[4]4∅

bucket[2] = np: 7 is at the head of the list. O(1).

insert(22)bucket[0]10∅bucket[1]∅bucket[2]712∅bucket[3]∅bucket[4]4∅

chain_insert(22).

insert(22)bucket[0]10∅bucket[1]∅bucket[2]712∅bucket[3]∅bucket[4]4∅

i = 22 % 5 = 2.

insert(22)bucket[0]10∅bucket[1]∅bucket[2]712∅bucket[3]∅bucket[4]4∅?np (malloc)

Memory for a new node is allocated; it is not NULL, so continue.

insert(22)bucket[0]10∅bucket[1]∅bucket[2]712∅bucket[3]∅bucket[4]4∅22np (malloc)

key = 22; next = bucket[2] (&node(7)): the new node points to the old head.

insert(22)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅

bucket[2] = np: 22 is at the head of the list. O(1).

bool chain_insert(int k) {
  int i = k % MZ;
  node *np = malloc(sizeof *np);
  if (np == NULL) return false;  /* no memory */
  np->key = k;
  np->next = bucket[i];         /* at the head */
  bucket[i] = np;
  return true;
}

Chaining · run the code step by step

Code trace: chain_search(12) and chain_search(17)

search(12)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅

chain_search(12): bucket 12 % 5 = 2.

search(12)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p = bucket[2]: the head of the list (22).

search(12)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p->key = 22 ≠ 12 → move on.

search(12)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p = p->next → node(7).

search(12)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p->key = 7 ≠ 12 → move on.

search(12)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p = p->next → node(12).

search(12)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p->key == 12: the loop condition is false; stop.

search(12)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

return p: found (3 nodes visited).

search(17)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅

chain_search(17): bucket 17 % 5 = 2.

search(17)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p = bucket[2]: the head of the list (22).

search(17)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p->key = 22 ≠ 17 → move on.

search(17)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p = p->next → node(7).

search(17)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p->key = 7 ≠ 17 → move on.

search(17)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p = p->next → node(12).

search(17)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p->key = 12 ≠ 17 → move on.

search(17)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅◄ p = NULL

p = p->next → NULL.

search(17)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅◄ p = NULL

p == NULL: end of the list; leave the loop.

search(17)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅◄ p = NULL

return NULL: 17 is not in the table. The whole chain (3 nodes) was scanned.

node *chain_search(int k) {
  node *p = bucket[k % MZ];
  while (p != NULL && p->key != k)
    p = p->next;
  return p;                 /* or NULL */
}

Chaining · run the code step by step

Code trace: chain_erase(7)

erase(7)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅

chain_erase(7): 7 is in the middle of the chain (22 → 7 → 12).

erase(7)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅

i = 7 % 5 = 2.

erase(7)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

p is at the head of the list; prev = NULL.

erase(7)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅p

22 ≠ 7 → enter the loop.

erase(7)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅prevp

prev = 22's node, p = 7's node.

erase(7)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅prevp

p->key == 7 → the loop ends.

erase(7)bucket[0]10∅bucket[1]∅bucket[2]22712∅bucket[3]∅bucket[4]4∅prevp

p is not NULL: the key was found.

erase(7)bucket[0]10∅bucket[1]∅bucket[2]2212∅bucket[3]∅bucket[4]4∅7free()

prev is not NULL → prev->next = p->next: 22 now points straight to 12; 7 is out of the chain.

erase(7)bucket[0]10∅bucket[1]∅bucket[2]2212∅bucket[3]∅bucket[4]4∅7free()

free(p): the node's memory is released. p must not be used after this (dangling pointer).

erase(7)bucket[0]10∅bucket[1]∅bucket[2]2212∅bucket[3]∅bucket[4]4∅

return true. Had the head node been deleted (prev == NULL), bucket[i] would have been updated.

bool chain_erase(int k) {
  int i = k % MZ;
  node *p = bucket[i], *prev = NULL;
  while (p != NULL && p->key != k) {
    prev = p;
    p = p->next;
  }
  if (p == NULL) return false;        /* absent */
  if (prev == NULL) bucket[i] = p->next;
  else prev->next = p->next;
  free(p);
  return true;
}

Load Factor and Deletion

The deletion trap: emptying a slot cuts searches short

02211213311424533678910start22, 1, 13, 11, 24, 33

We will delete 13. 24's home address is 2; since slots 2 and 3 were full, 24 went to 4.

022112311424533678910wrong: slot 2 = EMPTY

If we make the slot EMPTY…

022112311424533678910▲ ihomesearch(24) → slot 2 EMPTY−1 (wrong!)

…search(24) sees an empty slot at the home address and reports “absent”. Yet 24 is in the table!

022112✝311424533678910right: slot 2 = DELETED (✝)

The right way is to mark the slot with a tombstone (DELETED).

022112✝311424533678910▲ home▲ isearch(24): 2 ✝ → 3 → 4found

Search does not treat a tombstone as empty and keeps going; insertion, however, may reuse a tombstone.

In tables with many deletions tombstones pile up and searches get longer: rehash periodically (cleanup). Chaining does not have this problem.

Tombstones · run the code step by step

Code trace: erase(13), then search(24)

02211213311424533678910erase(13)

erase(13) is called; first its slot must be found.

02211213311424533678910▲ ihomesearch(13)

Home address i = 13 % 11 = 2.

02211213311424533678910▲ ihomesearch(13)1 probe

Slot 2 is USED and holds 13 → return 2 (1 probe).

02211213311424533678910▲ ierase(13)

search returned 2; i ≥ 0, so there is an item to delete.

022112✝311424533678910erase(13)

table[2].state = DELETED: a tombstone.

022112✝311424533678910erase(13)

return true.

022112✝311424533678910▲ ihomesearch(24)

Home address i = 24 % 11 = 2.

022112✝311424533678910▲ ihomesearch(24)

Slot 2 holds a tombstone: deleted, keep searching → i = 3.

022112✝311424533678910▲ home▲ isearch(24)

Slot 3: another key (11) → i = 4.

022112✝311424533678910▲ home▲ isearch(24)3 probes

Slot 4 is USED and holds 24 → return 4 (3 probes).

int search(int k) {
  int i = k % M;
  for (int d = 0; d < M; d++, i = (i + 1) % M) {
    if (table[i].state == EMPTY) return -1;
    if (table[i].state == USED &&
        table[i].key == k) return i;
  }
  return -1;
}
bool erase(int k) {
  int i = search(k);
  if (i < 0) return false;
  table[i].state = DELETED;   /* tombstone */
  return true;
}

Load Factor and Deletion

Load factor α = n/m and the expected number of probes

αChain ✓Chain ✗Linear ✓Linear ✗Uniform* ✓Uniform* ✗
0,251,131,251,21,41,151,3
0,51,251,501,52,51,392,0
0,751,381,752,58,51,854,0
0,91,451,905,550,52,5610,0

✓ successful, ✗ unsuccessful search. Formulas: chaining 1 + α/2 and 1 + α (Θ(1 + α)); linear probing (Knuth) ½(1 + 1/(1−α)) and ½(1 + 1/(1−α)²); * open addressing under uniform hashing (1/α)·ln(1/(1−α)) and 1/(1−α) (CLRS).

If α is held constant, every operation is O(1) on average. Practical thresholds: α ≤ 0.5–0.7 for open addressing, α ≤ about 1 for chaining. Past the threshold the table grows.

Load Factor and Deletion

Resizing (rehashing)

typedef struct {
  slot *a;      /* dynamic array of m slots */
  int m, n;     /* size, used slots */
} Table;
/* after inserting: */
if (2 * t->n > t->m)             /* α > 1/2 */
  resize(t, next_prime(2 * t->m));
Why rehash?

h(k) = k mod m depends on m: items land in different slots in the new table; copying is not enough.

Amortized O(1)

If the size doubles each time, n insertions copy at most 2n items in total: constant per insertion.

Tombstones

Rehashing also clears out the tombstones.

Resizing · run the code step by step

Code trace: resize(t, 11)

old (m = 5)012731243α = 0,6

n = 3, m = 5 → α = 0.6 > 0.5: the table grows to 11 slots (≈ 2m, prime).

old (m = 5)012731243

The old array's address and size are saved.

old (m = 5)012731243new (m = 11)012345678910

calloc returns 11 zeroed slots: all EMPTY (= 0).

old (m = 5)012731243new (m = 11)012345678910

Memory allocated; the table is linked to the new array, n = 0.

old (m = 5)012731243▲ inew (m = 11)012345678910

old[0] is EMPTY: skipped.

old (m = 5)012731243▲ inew (m = 11)012345678910

old[1] is EMPTY: skipped.

old (m = 5)012731243▲ inew (m = 11)0123456778910

old[2] = 7 USED → rehash: 7 % 11 = 7 → slot 7. (Its old home address was 7 % 5 = 2.)

old (m = 5)012731243▲ inew (m = 11)011223456778910

old[3] = 12 USED → rehash: 12 % 11 = 1 → slot 1. (Its old home address was 12 % 5 = 2.)

old (m = 5)012731243▲ inew (m = 11)0112233456778910

old[4] = 3 USED → rehash: 3 % 11 = 3 → slot 3. (Its old home address was 3 % 5 = 3.)

old (m = 5) — free()012731243new (m = 11)0112233456778910

The old array is freed. New α = 3/11 ≈ 0.27.

old (m = 5) — free()012731243new (m = 11)0112233456778910α ≈ 0,27

return true. Every item was reinserted: O(n + m).

bool resize(Table *t, int new_m) {
  slot *old = t->a;
  int old_m = t->m;
  slot *a = calloc(new_m, sizeof *a); /* EMPTY */
  if (a == NULL) return false;
  t->a = a; t->m = new_m; t->n = 0;
  for (int i = 0; i < old_m; i++)
    if (old[i].state == USED)
      table_insert(t, old[i].key);
  free(old);
  return true;
}

Lab · C code step by step

Lab: Open Addressing

Summary

Chaining or open addressing?

Chaining

  • α > 1 is possible; it never fills up, it just slows down.
  • Deletion is simple, no tombstones.
  • More robust against a poor hash.
  • A pointer plus malloc cost per node; not cache-friendly.

Open addressing

  • No extra pointers; contiguous memory, cache-friendly.
  • α ≤ 1; the cost explodes as α → 1.
  • Deletion needs tombstones.
  • Needs good probing (double hashing) against clustering.
Rule of thumb: if the item count is unpredictable and deletions are frequent, chain; if memory and speed are critical, deletions are rare and α can be kept low, use open addressing.

Summary

Complexity summary

OperationAverage (α constant)WorstNote
searchO(1)O(n)if every key lands in the same bucket
insertO(1)O(n)inserting at the head of a chain is always O(1)
deleteO(1)O(n)tombstones in open addressing
resizeO(n + m)O(n + m)amortized O(1) per insertion
sorted traversal / min / maxO(m + n lg n)O(m + n lg n)hashing keeps no order; min/max only by scanning: O(n + m)
The worst case can be triggered by malicious input (hash flooding). The defense: choose the function at run time from a random family — universal hashing (CLRS 11.3.3).

Summary

Common mistakes

1A negative key makes k % m negative, writing outside the array.
2Choosing m as 10 or a power of 2, so only the last digits/bits matter.
3Marking a deleted slot EMPTY in open addressing: searches stop halfway.
4Looping forever on a full table: limit the number of probes to m.
5Not checking the result of malloc; using a node after free.
6Copying items at their old indices when resizing (without rehashing).

Summary

Exercises

  1. m = 7, h(k) = k mod 7, linear probing: what does the table look like after inserting 10, 17, 24, 3 in order?
  2. Place the same keys with chaining (insert at the head); how long is the longest chain?
  3. Write the probe sequence 0, 1, 4, 9, 16, 25 (mod 11) for quadratic probing with m = 11.
  4. How many probes does an unsuccessful search expect in a linear-probing table with α = 0.9?
  5. Compute the additive hash and the base-31 polynomial hash (mod 11) of "ab" and "ba".
  6. In open addressing, why must insert call search before writing into a DELETED slot?
Show answers
  1. 10 → 3, 17 → 3 full → 4, 24 → 3, 4 full → 5, 3 → 3, 4, 5 full → 6: slots 3–6 = 10, 17, 24, 3.
  2. All in bucket 3: 3 → 24 → 17 → 10 (4 nodes). A poor hash choice makes chains long.
  3. 0, 1, 4, 9, 5, 3 (16 mod 11 = 5, 25 mod 11 = 3).
  4. ½(1 + 1/0.01) = 50.5 probes: the cost explodes as α → 1.
  5. Additive: 97 + 98 = 195 → both 195 % 11 = 8. Polynomial: "ab" = 97·31 + 98 = 3105 → 3; "ba" = 98·31 + 97 = 3135 → 0.
  6. The same key may already sit in a slot past the tombstone; without the check it enters the table twice.

Summary

Chapter summary

The idea

Compute the position from the key: O(1) search/insert/delete on average.

Hash function

Division (m prime), multiplication (A ≈ 0.618), folding, polynomial string hash.

Open addressing

Linear, quadratic, double hashing; clustering and tombstones.

Chaining

A linked list per bucket; O(1) insertion at the head, easy deletion.

Load factor

Keep α = n/m small; resize past the threshold.

Limits

No order information; worst case O(n); universal hashing as protection.

Summary

References and next steps

  • E. Horowitz, S. Sahni, S. Anderson-Freed, Fundamentals of Data Structures in C, Chapter 8 (Hashing).
  • T. H. Cormen et al., Introduction to Algorithms (CLRS), Chapter 11 (Hash Tables).
  • R. Sedgewick, Algorithms in C, Parts 1–4, Chapter 14 (Hashing).
  • D. E. Knuth, The Art of Computer Programming, Vol. 3, Section 6.4.

The End

Chapter 14: Hash Tables
Next chapter: Chapter 15: Graphs · Course page
1 / 1 Data Structures in C