Chapter 14: Hash Tables
Introduction
Learning Outcomes
Define the hash table, which computes a position from the key and searches in O(1) on average.
Compute division, multiplication, folding and string hashes; list what makes a function good.
Apply linear/quadratic probing, double hashing and chaining step by step.
Write insert, search and delete with malloc/free and the empty cases.
Show why open addressing needs a deleted marker (tombstone).
Explain how the load factor affects cost, and the amortized cost of resizing.
The Hashing Idea
Bringing search cost down to a constant
| Structure | Search | Insert | Delete | Note |
|---|---|---|---|---|
| Unsorted array | O(n) | O(1) | O(n) | append at the end, scan from the front |
| Sorted array | O(lg n) | O(n) | O(n) | binary search + shifting |
| Balanced search tree | O(lg n) | O(lg n) | O(lg n) | also gives sorted traversal |
| Hash table | O(1)* | O(1)* | O(1)* | * average; worst case O(n) |
The Hashing Idea
The simplest case first: a direct-address table
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; }
For 9-digit keys such as student numbers, |U| = 109: the table does not fit, and most of it stays empty.
A hash function that maps keys to a small range, h: U → {0, …, m−1}.
The Hashing Idea
Key → hash → bucket
| Term | Meaning |
|---|---|
| bucket / slot | a position in the table, 0 … m−1 |
| home address | h(k): the first place probed for the key |
| collision | k₁ ≠ k₂ but h(k₁) = h(k₂) |
| synonyms | keys with the same home address |
| load factor α | n / m (how full the table is) |
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 */
A key → value mapping; each key appears at most once.
Compiler symbol tables (Horowitz's motivating example), caches, duplicate detection, set implementations.
hsearch), so a C programmer must know how to write one.Hash Functions
What makes a hash function good
O(key length) to compute; a few arithmetic operations.
Keys should spread over the m buckets as evenly as possible (the simple uniform hashing assumption).
Functions that look only at the last digits cluster on patterned data.
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
| k | 10 | 20 | 30 | 40 | 50 | 60 | 17 | 27 |
|---|---|---|---|---|---|---|---|---|
| k mod 10 | 0 | 0 | 0 | 0 | 0 | 0 | 7 | 7 |
| k mod 11 | 10 | 9 | 8 | 7 | 6 | 5 | 6 | 5 |
- 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); }
<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):
The parts are added as they are: 699.
Every other part is reversed (like folding paper): 897.
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 */ }
Adding character codes sends anagrams such as "abc" and "cba" to the same bucket.
h = c₀·31n−1 + … + cn−1: one multiplication per character with Horner's rule. Position information enters the hash.
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)
str_hash("kedi", 11) is called.
Start with h = 0.
*s = 'k' ≠ '\0' → the loop continues.
h = (0·31 + 107) % 11 = 107 % 11 = 8.
*s = 'e' ≠ '\0' → the loop continues.
h = (8·31 + 101) % 11 = 349 % 11 = 8.
*s = 'd' ≠ '\0' → the loop continues.
h = (8·31 + 100) % 11 = 348 % 11 = 7.
*s = 'i' ≠ '\0' → the loop continues.
h = (7·31 + 105) % 11 = 322 % 11 = 3.
*s == '\0': end of string.
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
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.
All items live inside the table; α ≤ 1. An empty slot is found by following a probe sequence.
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
Insert 22, 1, 13, 11, 24, 33 in order. Home addresses: 0, 1, 2, 0, 2, 0.
22 → home 0: slot 0.
1 → home 1: slot 1.
13 → home 2: slot 2.
11: slot 0 is full (22) → try the next slot.
11: slot 1 is full (1) → try the next slot.
11: slot 2 is full (13) → try the next slot.
11 → home 0, probe sequence 0, 1, 2, 3: slot 3.
24: slot 2 is full (13) → try the next slot.
24: slot 3 is full (11) → try the next slot.
24 → home 2, probe sequence 2, 3, 4: slot 4.
33: slot 0 is full (22) → try the next slot.
33: slot 1 is full (1) → try the next slot.
33: slot 2 is full (13) → try the next slot.
33: slot 3 is full (11) → try the next slot.
33: slot 4 is full (24) → try the next slot.
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 */
A global array starts zeroed; since EMPTY = 0, every slot is empty.
Start at the home address and write into the first slot that is not USED; at most m probes.
search that the key is not already in the table.Open addressing · run the code step by step
Code trace: insert(33) — linear probing
insert(33): the table holds 22, 1, 13, 11, 24 (slots 0–4, one full block).
Home address i = 33 % 11 = 0.
d = 0 < 11: probe 0, slot 0.
Slot 0 is USED (22) → next: i = (0+1) % 11.
d = 1 < 11: probe 1, slot 1.
Slot 1 is USED (1) → next: i = (1+1) % 11.
d = 2 < 11: probe 2, slot 2.
Slot 2 is USED (13) → next: i = (2+1) % 11.
d = 3 < 11: probe 3, slot 3.
Slot 3 is USED (11) → next: i = (3+1) % 11.
d = 4 < 11: probe 4, slot 4.
Slot 4 is USED (24) → next: i = (4+1) % 11.
d = 5 < 11: probe 5, slot 5.
Slot 5 is EMPTY → write here.
table[5] = {33, USED}.
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)
search(24) is called.
Home address i = 24 % 11 = 2.
Slot 2: another key (13) → i = 3.
Slot 3: another key (11) → i = 4.
Slot 4 is USED and holds 24 → return 4 (3 probes).
search(44) is called.
Home address i = 44 % 11 = 0.
Slot 0: another key (22) → i = 1.
Slot 1: another key (1) → i = 2.
Slot 2: another key (13) → i = 3.
Slot 3: another key (11) → i = 4.
Slot 4: another key (24) → i = 5.
Slot 5: another key (33) → i = 6.
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
Same keys: 22, 1, 13, 11, 24, 33. Probes jump 1, 4, 9, 16… away from the home address.
22 → home 0: slot 0.
1 → home 1: slot 1.
13 → home 2: slot 2.
11: slot 0 is full (22) → try the next slot.
11: slot 1 is full (1) → try the next slot.
11 → home 0, probe sequence 0, 1, 4: slot 4.
24: slot 2 is full (13) → try the next slot.
24 → home 2, probe sequence 2, 3: slot 3.
33: slot 0 is full (22) → try the next slot.
33: slot 1 is full (1) → try the next slot.
33: slot 4 is full (11) → try the next slot.
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
The step size depends on the key: 11 → h₂ = 2, 24 → h₂ = 5, 33 → h₂ = 4.
22 → home 0: slot 0.
1 → home 1: slot 1.
13 → home 2: slot 2.
11: slot 0 is full (22) → try the next slot.
11: slot 2 is full (13) → try the next slot.
11 → home 0, probe sequence 0, 2, 4: slot 4.
24: slot 2 is full (13) → try the next slot.
24 → home 2, probe sequence 2, 7: slot 7.
33: slot 0 is full (22) → try the next slot.
33: slot 4 is full (11) → try the next slot.
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?
| Method | probe i | probe sequence of 33 | Probes |
|---|---|---|---|
| Linear | h + i | 0, 1, 2, 3, 4, 5 | 6 |
| Quadratic | h + i² | 0, 1, 4, 9 | 4 |
| Double hashing | h₁ + i·h₂ | 0, 4, 8 | 3 |
| Method | Clustering | Distinct probe sequences |
|---|---|---|
| Linear | primary (blocks) | m |
| Quadratic | secondary (same home address) | m |
| Double hashing | minimal | ≈ m² |
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; }
The order in the list does not matter: inserting at the head is O(1). (If duplicates are not allowed, search first.)
Only the list of its own bucket is scanned: 1 + α/2 nodes on average.
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)
chain_insert(12).
i = 12 % 5 = 2.
Memory for a new node is allocated; it is not NULL, so continue.
key = 12; next = bucket[2] (NULL): the new node points to the old head.
bucket[2] = np: 12 is at the head of the list. O(1).
chain_insert(7).
i = 7 % 5 = 2.
Memory for a new node is allocated; it is not NULL, so continue.
key = 7; next = bucket[2] (&node(12)): the new node points to the old head.
bucket[2] = np: 7 is at the head of the list. O(1).
chain_insert(22).
i = 22 % 5 = 2.
Memory for a new node is allocated; it is not NULL, so continue.
key = 22; next = bucket[2] (&node(7)): the new node points to the old head.
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)
chain_search(12): bucket 12 % 5 = 2.
p = bucket[2]: the head of the list (22).
p->key = 22 ≠ 12 → move on.
p = p->next → node(7).
p->key = 7 ≠ 12 → move on.
p = p->next → node(12).
p->key == 12: the loop condition is false; stop.
return p: found (3 nodes visited).
chain_search(17): bucket 17 % 5 = 2.
p = bucket[2]: the head of the list (22).
p->key = 22 ≠ 17 → move on.
p = p->next → node(7).
p->key = 7 ≠ 17 → move on.
p = p->next → node(12).
p->key = 12 ≠ 17 → move on.
p = p->next → NULL.
p == NULL: end of the list; leave the loop.
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)
chain_erase(7): 7 is in the middle of the chain (22 → 7 → 12).
i = 7 % 5 = 2.
p is at the head of the list; prev = NULL.
22 ≠ 7 → enter the loop.
prev = 22's node, p = 7's node.
p->key == 7 → the loop ends.
p is not NULL: the key was found.
prev is not NULL → prev->next = p->next: 22 now points straight to 12; 7 is out of the chain.
free(p): the node's memory is released. p must not be used after this (dangling pointer).
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
We will delete 13. 24's home address is 2; since slots 2 and 3 were full, 24 went to 4.
If we make the slot EMPTY…
…search(24) sees an empty slot at the home address and reports “absent”. Yet 24 is in the table!
The right way is to mark the slot with a tombstone (DELETED).
Search does not treat a tombstone as empty and keeps going; insertion, however, may reuse a tombstone.
Tombstones · run the code step by step
Code trace: erase(13), then search(24)
erase(13) is called; first its slot must be found.
Home address i = 13 % 11 = 2.
Slot 2 is USED and holds 13 → return 2 (1 probe).
search returned 2; i ≥ 0, so there is an item to delete.
table[2].state = DELETED: a tombstone.
return true.
Home address i = 24 % 11 = 2.
Slot 2 holds a tombstone: deleted, keep searching → i = 3.
Slot 3: another key (11) → i = 4.
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,25 | 1,13 | 1,25 | 1,2 | 1,4 | 1,15 | 1,3 |
| 0,5 | 1,25 | 1,50 | 1,5 | 2,5 | 1,39 | 2,0 |
| 0,75 | 1,38 | 1,75 | 2,5 | 8,5 | 1,85 | 4,0 |
| 0,9 | 1,45 | 1,90 | 5,5 | 50,5 | 2,56 | 10,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).
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));
h(k) = k mod m depends on m: items land in different slots in the new table; copying is not enough.
If the size doubles each time, n insertions copy at most 2n items in total: constant per insertion.
Rehashing also clears out the tombstones.
Resizing · run the code step by step
Code trace: resize(t, 11)
n = 3, m = 5 → α = 0.6 > 0.5: the table grows to 11 slots (≈ 2m, prime).
The old array's address and size are saved.
calloc returns 11 zeroed slots: all EMPTY (= 0).
Memory allocated; the table is linked to the new array, n = 0.
old[0] is EMPTY: skipped.
old[1] is EMPTY: skipped.
old[2] = 7 USED → rehash: 7 % 11 = 7 → slot 7. (Its old home address was 7 % 5 = 2.)
old[3] = 12 USED → rehash: 12 % 11 = 1 → slot 1. (Its old home address was 12 % 5 = 2.)
old[4] = 3 USED → rehash: 3 % 11 = 3 → slot 3. (Its old home address was 3 % 5 = 3.)
The old array is freed. New α = 3/11 ≈ 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
malloccost 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.
Summary
Complexity summary
| Operation | Average (α constant) | Worst | Note |
|---|---|---|---|
| search | O(1) | O(n) | if every key lands in the same bucket |
| insert | O(1) | O(n) | inserting at the head of a chain is always O(1) |
| delete | O(1) | O(n) | tombstones in open addressing |
| resize | O(n + m) | O(n + m) | amortized O(1) per insertion |
| sorted traversal / min / max | O(m + n lg n) | O(m + n lg n) | hashing keeps no order; min/max only by scanning: O(n + m) |
Summary
Common mistakes
k % m negative, writing outside the array.malloc; using a node after free.Summary
Exercises
- m = 7, h(k) = k mod 7, linear probing: what does the table look like after inserting 10, 17, 24, 3 in order?
- Place the same keys with chaining (insert at the head); how long is the longest chain?
- Write the probe sequence 0, 1, 4, 9, 16, 25 (mod 11) for quadratic probing with m = 11.
- How many probes does an unsuccessful search expect in a linear-probing table with α = 0.9?
- Compute the additive hash and the base-31 polynomial hash (mod 11) of
"ab"and"ba". - In open addressing, why must
insertcallsearchbefore writing into a DELETED slot?
Show answers
- 10 → 3, 17 → 3 full → 4, 24 → 3, 4 full → 5, 3 → 3, 4, 5 full → 6: slots 3–6 = 10, 17, 24, 3.
- All in bucket 3: 3 → 24 → 17 → 10 (4 nodes). A poor hash choice makes chains long.
- 0, 1, 4, 9, 5, 3 (16 mod 11 = 5, 25 mod 11 = 3).
- ½(1 + 1/0.01) = 50.5 probes: the cost explodes as α → 1.
- Additive: 97 + 98 = 195 → both 195 % 11 = 8. Polynomial: "ab" = 97·31 + 98 = 3105 → 3; "ba" = 98·31 + 97 = 3135 → 0.
- The same key may already sit in a slot past the tombstone; without the check it enters the table twice.
Summary
Chapter summary
Compute the position from the key: O(1) search/insert/delete on average.
Division (m prime), multiplication (A ≈ 0.618), folding, polynomial string hash.
Linear, quadratic, double hashing; clustering and tombstones.
A linked list per bucket; O(1) insertion at the head, easy deletion.
Keep α = n/m small; resize past the threshold.
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.