Data Structures - Hash Table
A hash table (Hash Table) is a data structure that maps keys to specific positions in an array through a hash function, thereby achieving average O(1) time complexity for lookup, insertion, and deletion operations.
Hash functions and hash collisions
Hash table principle - hash function mapping
key=1
key=2
key=11
→
hash(key) = key % 10
→
[0]
[1] 1,11
[2] 2
[3]
[4]
[5]
[6]
[7]
[8]
[9]
Red slot = collision! key=1 and key=11 map to the same position
The design goal of a hash function is to convert any input (key) into an integer within a fixed range (the array index).
A good hash function should be fast to compute and produce a uniform distribution of results.
However, because the capacity of a hash table is always limited, hash collisions - where two different keys map to the same position - are inevitable.
Collision resolution methods
Hash collision resolution - separate chaining vs open addressing
Separate chaining
Each slot stores a linked list; colliding keys are appended
Advantage: simple to implement, easy to delete
Disadvantage: pointer overhead, cache-unfriendly
Load factor can exceed 1.0
Open addressing
Probe for the next empty slot when a collision occurs
Advantage: no pointers, cache-friendly
Disadvantage: deletion requires lazy marking
Load factor must be <= 0.7
| Method | Principle | Advantages | Disadvantages |
|---|---|---|---|
| Separate chaining | Each slot stores a linked list, and colliding keys are appended to the list | Simple to implement, easy to delete, load factor can be greater than 1 | Extra pointer overhead, cache-unfriendly |
| Open addressing | When a collision occurs, search the array for the next empty slot according to the probing rule | No pointers needed, cache-friendly | Deletion is complex (requires lazy deletion), load factor must be controlled |
In open addressing, you cannot simply empty a slot when deleting an element, otherwise the lookup chain of subsequent elements is broken. The usual approach is "lazy deletion" - mark the slot as "deleted" rather than "empty".
Separate chaining implementation
Example
#include
#include
#include
#define TABLE_SIZE 10 /* hash table capacity */
/* hash table node */
struct
HashNode
{
int
key
;
int
value
;
struct
HashNode
next
;
/* points to the next node in the same slot */
}
;
/* hash function: simple modulo method */
int
hashFunc
(
int
key
)
{
return
key
%
TABLE_SIZE
;
}
/* insert a key-value pair (separate chaining)
time complexity: O(1) average, O(n) worst case (all keys map to the same slot) */
void
insert
(
struct
HashNode
table
[
]
,
int
key
,
int
value
)
{
int
index
=
hashFunc
(
key
)
;
struct
HashNode
newNode
=
(
struct
HashNode
)
malloc
(
sizeof
(
struct
HashNode
)
)
;
newNode
->
key
=
key
;
newNode
->
value
=
value
;
newNode
->
next
=
table
[
index
]
;
/* head insertion */
table
[
index
]
=
newNode
;
}
/* lookup: get a value by key
first compute the hash value to locate the slot, then traverse the list to find the matching key */
int
search
(
struct
HashNode
table
[
]
,
int
key
)
{
int
index
=
hashFunc
(
key
)
;
struct
HashNode
cur
=
table
[
index
]
;
while
(
cur
!=
NULL
)
{
if
(
cur
->
key
==
key
)
{
return
cur
->
value
;
/* found */
}
cur
=
cur
->
next
;
}
return
1
;
/* Not found */
}
int
main
(
)
{
struct
HashNode
table
[
TABLE_SIZE
]
=
{
NULL
}
;
/* initialize all slots to empty */
insert
(
table
,
1
,
100
)
;
/* key=1, value=100, index=1 */
insert
(
table
,
2
,
200
)
;
/* key=2, value=200, index=2 */
insert
(
table
,
11
,
300
)
;
/* key=11, value=300, index=1 - collides with key=1! */
printf
(
"key=1 → %d\n"
,
search
(
table
,
1
)
)
;
/* Output: 100 */
printf
(
"key=2 → %d\n"
,
search
(
table
,
2
)
)
;
/* output: 200 */
printf
(
"key=11 → %d\n"
,
search
(
table
,
11
)
)
;
/* output: 300 (separate chaining handled the collision correctly) */
printf
(
"key=99 → %d\n"
,
search
(
table
,
99
)
)
;
/* output: -1 (not found) */
return
0
;
}
Application scenarios
| Scenario | Description |
|---|---|
| Dictionary / Map | Storage and fast lookup of key-value pairs, such as Python's dict and Java's HashMap |
| Caching system | Underlying implementation of caching systems such as LRU Cache |
| Database index | Hash indexes achieve O(1) lookup for equality queries |
| Compiler symbol table | Records the mapping between variable names, function names, and their types |