Prelude to
increasing/decreasing
the hash table size
Review:
-
Hash function
H( ):
maps
a key k to
an integer in the
range [0..(M-1)]
H(k) = integer in the range [0..(M-1)]
|
-
Hash value h
=
the value returned by
the
hash function
H( )
-
Bucket
=
the array element
used to store
an entry of
the dictionary
|
Prelude to
increasing/decreasing
the hash table size
Review:
- Commonly used
hash function:
h = H2( H1( k ) )
H1(k) = k.hashCode()
H2(x) = Math.abs( a*x + b ) ) % p % M M = array size
|
-
Always
store the
entry (k, v)
at index h
in the array
- Example: how to store a
map (dictionary) using
hashing
|
Consequence of
increasing/decreasing
the hash table size
Example of
error when changing the has table size
Example of
error when changing the has table size
Naive way to increase/decrese
the hash table size
- Because the
hash function
changes with the
hash table size:
- We
must
rehash
all the
keys and
insert into
the new
hash table
|
- A
naive
Algorithm to
double the
hash table:
public void doubleHashTable()
{
Entry[] oldBucket = bucket;
// Double the size of the bucket
bucket = (Entry[]) new Entry[2*oldBucket.length];
capacity = 2*oldBucket.length;
// Rehash all entries by inserting them in the new hash table
for ( int i = 0; i < oldBucket.length; i++ )
{
if ( oldBucket[i] != null && oldBucket[i] != AVAILABLE )
this.put( oldBucket[i].key, oldBucket[i].value );
}
}
|
|
DEMO:
15-hashing/30-dyn-hashing/Demo.java +
HashTableLinProbe.java
Dynamic hashing techniques
and
comclussion
❮
❯