Hash Tables
Hash Tables
0
1 025-612-0001
2 981-101-0002
3
4 451-229-0004
…
positive integer
Our hash table uses an 9997
9998 200-751-9998
array of size N10,000 9999
and the hash function
h(x)last four digits of x
cell
Separate Chaining: Separate chaining is
let each cell in the simple, but requires
table point to a additional memory
linked list of entries outside the table
that map there
© 2010 Goodrich, Tamassia Hash Tables 11
Map with Separate Chaining
Delegate operations to a list-based map at each
cell:
Algorithm find(k):
return A[h(k)].find(k)
Algorithm put(k,v):
t = A[h(k)].put(k,v)
if t = null then {k is a new key}
n=n+1
return t
Algorithm erase(k):
t = A[h(k)].erase(k)
if t ≠ null then {k was found}
n=n-1
return t
© 2010 Goodrich, Tamassia Hash Tables 12
Linear Probing
Open addressing: the Example:
colliding item is placed in a
different cell of the table
h(x) x mod 13
Linear probing: handles Insert keys 18, 41,
collisions by placing the 22, 44, 59, 32, 31,
colliding item in the next
73, in this order
(circularly) available table
cell
Each table cell inspected is
referred to as a “probe” 0 1 2 3 4 5 6 7 8 9 10 11 12
Colliding items lump
together, causing future
collisions to cause a longer 41 18445932223173
sequence of probes 0 1 2 3 4 5 6 7 8 9 10 11 12