















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
Hashing Scattering Hash Values Hash Function Open Addressing Chaining Hashing in Java
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]
Scattering Hash Values
Have to reduce it to 0..N-
might not distribute values well, particularly if N is apower of 2
Produces good hash values Hash value = Math.abs((a * key.hashCode()) % N) Where
N is table size a, N are large primes
Hash Function
Unique values for each key
0 1 2 3 4 5 6 7 8 9
Hash Function
hashCode("grapes") = 8hashCode("kiwi") = 0hashCode("strawberry") = 9hashCode("mango") = 6hashCode("banana") = 2hashCode(“orange") =
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 X
Examine entry at A[ bucket(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 unoccupied location
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
Find hash code
key
for X
Examine list at table entry A[
key
Collision
Multiple entries in list for entry
Chaining Example
Insert A,
Insert B,
Insert C
Chaining Example
Find B,
Find A