lecture23-aug5 - Lecture 23 Assignment 4 Q&A due Friday...

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

View Full Document Right Arrow Icon
8/5/2009 1 Lecture 23 ± Assignment 4 – StopWatch accumulation should not include IO ± Assignment 5 – out on request if assignment 4 finished – same teams – due next Wed. ± Exam 2 – August 14 in class ± Topics of the day – Hash tables –Maps Beyond BSTs for Searching ± BSTs Divide and Conquer – reducing work by a factor of 2 each time ± Can we reduce the work by a bigger factor? 10? 1000? ± An ArrayList does this in a way when accessing elements by index but must use an integer value each position holds a single element and positional locations must be maintained ± HashTables overcome those problems while maintaining the fast access, insertion, and deletion Hash Tables ± The goal behind the hash table is to be able to access an entry based on its key value, not its location – access an element directly through its key value – Not having to determine its location first by searching for the key value in the array ± Using a hash table enables us to retrieve an item in constant time (on average) or linear time (worst case) Hash Codes and Index Calculation ± The basis of hashing is to transform the item’s key value into an integer value which will then be transformed into a table index h(x) x Hash table Methods for Generating Hash Codes ± In most applications, the keys will consist of strings of letters or digits rather than a single character ± The number of possible key values is much larger than the table size ± Generating good hash codes is somewhat of an experimental process – Try to achieve a random distribution of key values over the table space – the hash function should be relatively simple and efficient to compute Java HashCode Method ± For strings, simply summing the int values of all characters will return the same hash code for “ sign” and “ sing” ± The Java API algorithm accounts for position of the characters as well ± The String.hashCode() method returns the integer calculated by the formula: calculated by the formula: s 0 x 31 (n-1) + s 1 x 31 (n-2) + … + s n-1 where s i is the i th character of the string, and n is the length of the string ± “Cat” will have a hash code of: ‘C’ x 31 2 + ‘a’ x 31 + ‘t’ ± 31 is a prime number that generates relatively few collisons
Background image of page 1

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

View Full Document Right Arrow Icon
8/5/2009 2 The Java String class hashCode method public int hashCode() { int h = hash; if (h == 0) { int off = offset; char val[] = value; int len = count; for( in ti=0 ;i<len ;i++) for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; } value hash offset count Instance variables Open Addressing ± Collisions can occur when 2 keys hash to the same location ± Consider two ways to organize hash tables – Open addressing (aka linear probing) – Chaining
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

lecture23-aug5 - Lecture 23 Assignment 4 Q&A due Friday...

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

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