

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















Overview
Scattering Hash Values Hash Function
Open Addressing Chaining
Hashing
Array indexed using hash values Hash table A with size N Indices of A range from 0 to N-1 Store in A[ hashValue % N]
Hash Function
Hash value
32-bit signed int
Default hash function
int hashCode( )
Must reduce hash value to 0..N – 1
Scattering Hash Values
Produces good hash values Hash value = Math.abs((a * key.hashCode( )) % N) Where
N is table size a is large prime number
Beware of % (Modulo Operator)
x % y == x – y * ( x / y )
–|y| <
x % y
< +|y|
Rather than Math.abs(x) % N In case N is negative
Hash Function
Unique values for each key
0 1 2 3 4 5 6 7 8 9
Hash Function
Same hash value formultiple keys
0 1 2 3 4 5 6 7 8 9
Open Addressing Hashing
Hash table contains objects Probe
examine table entry
Collision
Move K entries past current location Wrap around table if necessary Find location for X1.
Examine entry at A[ key(X) ]
If entry = X, found
If entry = empty, X not in hash table
Else increment location by K, repeat
Open Addressing Hashing
Linear probing
K = 1 May form clusters of contiguous entries Deletions
Find location for X If X inside cluster, leave non-empty marker Insertion
Find location for X Insert if X not in hash table Can insert X at first non-empty marker
Open Addressing Example
Insert A, Insert B, Insert C, Insert D
Open Addressing Example
Find A,
Find B,
Find C,
Find D
Efficiency of Open Hashing
Chaining (Bucket Hashing)
Hash table contains lists of objects Find location for X
Find hash code key for X Examine list at table entry A[ key ] Collision
Multiple entries in list for entry