15 September 2009 Notes
Hash Table
It is a data structure that can be used to store information that can be accessed with a
key
Operations:
void insert(KeyType key, Object element)
Object find (KeyType key)
void remove(KeyType key)
It takes an expected constant time
to perform operations
A hash table is an array of lists
The hash function i=hash(key) returns the index of the list in the hash table where the
item will be added
If the item is not yet in that list, a new node is created and added into the list
i=hash(key) 0 <= i <= n1
Example of hash functions
1)
if KeyType = integer
hash(key) = key % n
2)
if KeyType = string
hash(key) = (∑ key[i]) % n i=0 to length(key)
this hash function has the problem that two words with the same
characters in a different order will give same hash value.
An improved hash function that does not have this problem is:
hash(key) = (∑ i * key[i]) % n i=0 to length(key)
Assumptions to be made in a Hash Table
1) Entries have to be evenly distributed across all lists in hash table. If all the entries
accumulate in a single list, the find operation will be O(n) instead of O(1)
2) n < N or at least n < cN where c is some small constant. This will make sure that
the lists are not long or they have very few elements.
If we follow these 2 assumptions, the find, insert, remove operations will take an
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 Staff
 Data Structures, hash function, Bloom filter, char* key, int h=hash

Click to edit the document details