Presentation is loading. Please wait.

Presentation is loading. Please wait.

HashMaps. Overview What are HashMaps? Implementing DictionaryADT with HashMaps HashMaps 2/16.

Similar presentations


Presentation on theme: "HashMaps. Overview What are HashMaps? Implementing DictionaryADT with HashMaps HashMaps 2/16."— Presentation transcript:

1 HashMaps

2 Overview What are HashMaps? Implementing DictionaryADT with HashMaps HashMaps 2/16

3 Hashing What if: We could store everything in our dictionary in an array? We could somehow take any key and turn it into an index into the array in constant time? What would the cost of insert, delete, and find be then? HashMaps p. 3/16

4 Designing a hashtable There are three main design questions: What hash function to use (the function that turns a key into an array index) What size to make the array What to do if two items index into the same spot (a “collision”) HashMaps p. 4/16

5 Step 1: what if your key isn’t an integer? Suppose the keys are individual letters, stored as Strings. What happens if the hash function returns A number from 0-25? The ASCII code (or Unicode) of the letter? HashMaps p. 5/16

6 Choosing a hash function for Strings What if the hash function for a String returns The value of the hash function for the first letter of the String? The sum of the values of the hash functions for all the letters? Java’s solution: For a String of length n: s[0] *31 n-1 + s[1]*31 n-2 + … + s[n-1]*31 0 HashMaps p. 6/16

7 Step 2: get the right integer Once your key has been converted into an integer, then what? Is it in the right range to index into the array? Easy solution: index = hashCode % arraySize; Harder question: what if two keys hash to the same array index (a collision)? HashMaps p. 7/16

8 Resolving collisions 1: chaining What is chaining? Suppose we insert the following names into a table of size 26 using chaining (with the hash function h(A)=0, h(B)=1, etc.) Ann, Andrew, Bob, Doug, Elizabeth, Betty, Barbara, Hal, Bill, Mary, Tim, Walter, Xena What does the table look like after the inserts? HashMaps p. 8/16

9 Resolving collisions: chaining A chained hashtable has an array size of 512. What is the maximum number of entries that can be placed in the table? A. 256 B. 511 C. 512 D. 164 E. There is no maximum. HashMaps p. 9/16

10 Resolving collisions: linear probing What is linear probing? Suppose we insert the following names into a table of size 26 using linear probing (with the hash function h(A)=0, h(B)=1, etc.) Ann, Andrew, Bob, Doug, Elizabeth, Betty, Barbara, Hal, Bill, Mary, Tim, Walter, Xena What does the hashtable look like after the inserts? HashMaps p. 10/16

11 Resolving collisions: Quadratic probing What is quadratic probing? Suppose we insert the following names into a table of size 26 using quadratic probing (with the hash function h(A)=0, h(B)=1, etc.) Ann, Andrew, Bob, Doug, Elizabeth, Betty, Barbara, Hal, Bill, Mary, Tim, Walter, Xena What does the hashtable look like after the inserts? HashMaps p. 11/16

12 Resolving collisions: Double Hashing What is double hashing? What are its pros and cons? HashMaps p. 12/16

13 Deleting elements from a hashtable How does this work if collisions are handled using chaining? How does this work if collisions are handled using linear probing or another strategy that stores data in the table? HashMaps p. 13/16

14 Load factor Suppose you place m items in a hashtable with an array size of s. What is the correct formula for the load factor? A. s+m B. s-m C. m-s D. m*s E. m/s HashMaps p. 14/16

15 Java’s HashMap class What does Java mean by a map? What is Java’s default value for the load factor for the class HashMap? What are the HashMap operations for insert, delete, and find? HashMaps p. 15/16

16 Coming attractions Next time, we’ll look at a new data structure that allows us to model the Internet, airplane routes, and a variety of other data connected in ways that may be neither linear nor hierarchical: graphs. Homework: read chapter 13 Graphs (or the equivalent in the earlier edition). p. 16/16


Download ppt "HashMaps. Overview What are HashMaps? Implementing DictionaryADT with HashMaps HashMaps 2/16."

Similar presentations


Ads by Google