notes18 - 15 September 2009 Notes Hash Table -It is a data...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
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 <= n-1 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 5

notes18 - 15 September 2009 Notes Hash Table -It is a data...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online