Hash Tables
Hash Tables
Hash Tables
Hash Tables
A "faster" implementation for Map ADTs
Outline
What is hash function?
translation of a string key into an integer
Consider a few strategies for implementing a hash table
linear probing
quadratic probing
separate chaining hashing
Big Ohs using different
data structures for a Map ADT?
Data Structure put get remove
Unsorted Array
Sorted Array
hash(key)
AAAAAAAA 8482
zzzzzzzz 1273
hash(key)
A string of 8 chars Range: 0 ... 9996
Hash method
What if the ASCII value of individual chars of the string key
added up to a number from ("A") 65 to possibly 488 ("zzzz")
4 chars max
If the array has size = 309, mod the sum
390 % TABLE_SIZE = 81
394 % TABLE_SIZE = 85
404 % TABLE_SIZE = 95
These array indices index these keys
81 abba
85 abcd
95 able
A too simple hash method
@Test
public void testHash() {
assertEquals(81, hash("abba"));
assertEquals(81, hash("baab"));
assertEquals(85, hash("abcd"));
assertEquals(86, hash("abce"));
assertEquals(308, hash("IKLT"));
assertEquals(308, hash("KLMP"));
}
0
insert Keys
objects with ...
these three 80
keys: 81 "abba"
82
"abba" 83
"abcd" 84
"abce" 85 "abcd"
86 "abce"
...
308
Collision occurs while inserting "baab"
can't insert
"baab" where
it hashes to 0
same slot as ...
"abba" 80 "baab"
81 "abba" Try [81]
82 "baab" Put in [82]
83
Linear probe
forward by 1, 84
inserting it 85 "abcd"
at the next 86 "abce"
available slot ...
308
Wrap around when collision occurs at end
0 "IKLT"
Insert "KLMP" ...
and "IKLT" 80
both of which 81 "abba"
have a hash "baab"
82
value of 308
83
84
85 "abcd"
86 "abce"
...
308 "KLMP
"
Find object with key "baab"
public HashTable() {
// Since HashNodeTable has generics, we can not have
// a new HashNodeTable[], so use Object[]
table = new Object[TABLE_SIZE];
for (int j = 0; j < TABLE_SIZE; j++)
table[j] = new HashTableNode();
}
Black areas represent slots in use; white areas are empty slots
Quadratic Probing
0
1 “AB” 9 “BA” 9
2
Insert Six Objects
@Test
public void testPutAndGet() {
MyHashTable<String, BankAccount> h =
new MyHashTable<String, BankAccount>();
put ____________
remove _____________
Hash Table Summary
Hashing involves transforming a key to produce an integer in
a fixed range (0..TABLE_SIZE-1)
The function that transforms the key into an array index is
known as the hash function
When two data values produce the same hash value, you get a
collision
it happens!
Collision resolution may be done by searching for the next
open slot at or after the position given by the hash function,
wrapping around to the front of the table when you run off the
end (known as linear probing)
Hash Table Summary
Another common collision resolution technique is to store
the table as an array of linked lists and to keep at each
array index the list of values that yield that hash value
known as separate chaining
Most often the data stored in a hash table includes both a
key field and a data field (e.g., social security number and
student information).
The key field determines where to store the value.
A lookup on that key will then return the value associated
with that key (if it is mapped in the table)