19-Hashtables

# 19-Hashtables - Hash Table How can we retrieve a value(by...

This preview shows pages 1–5. Sign up to view the full content.

How can we retrieve a value (by key) from an associative container in O(1) time? Yes we can, with the help of a Hash Table! Hash table typically implements an internal array for storing data and provides a function for index (i.e. hash code ) calculation. The trick is to convert the key into an index. Hash Table Hash Table index calculation [0] [1] [2] [n-1] Table Index Key

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

View Full Document
Suppose you have a message written in Unicode characters. There are 65,536 Unicode characters. If you have only 100 different ones in your message, would you allocate an array of 65,536 elements? Not if you are pressed for space (e.g. cell phone, PDA). Instead you can device a rule for calculating index, e.g. int index = unicodeChar % 100; Here integer value of the UnicodeChar serves as hash code from which you calculate index. This index is fast ( + ), but not unique ( - ) as different Unicode characters (e.g. #2212 and #3212) would get the same index (12) resulting in collisions . Hash Codes Hash Codes
Most frequently key is represented by a string (i.e. non-integer). How to generate a hash code? Here are some examples: 1) Calculate the sum of characters; 2) Take the ASCII value of the first character; 3) Index = (s[0]*31 n-1 + s[1]*31 n-2 +… + s[n-1]) % TABLE_SIZE; The index is not unique, but the probability of collisions is small due to the use of a prime number (31). Generates “ random” indexes evenly distributed through the range 0..TABLE_SIZE-1 (clustered distribution would be bad!) Criteria for good hash function: 1) Generates even distribution of indexes; 2) Efficient / computes quickly. Create your own! Generating Hash Codes Generating Hash Codes

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

View Full Document
2) When the table is nearly full – rehash as follows: a. Allocate table twice the capacity of the original (better yet pick the nearest prime number); b. Recalculate indexes and re-insert all old table entries that have not been deleted. Reducing Collisions
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/18/2010 for the course CSE 122 taught by Professor Yoxheimer,ericch during the Spring '07 term at Penn State.

### Page1 / 17

19-Hashtables - Hash Table How can we retrieve a value(by...

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

View Full Document
Ask a homework question - tutors are online