hashmaps. overview what are hashmaps? implementing dictionaryadt with hashmaps hashmaps 2/16

16
HashMaps

Post on 15-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

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

HashMaps

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

OverviewWhat are HashMaps?Implementing DictionaryADT with HashMaps

HashMaps 2/16

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

HashingWhat 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

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

Designing a hashtableThere 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 arrayWhat to do if two items index into the same spot (a

“collision”)

HashMaps p. 4/16

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

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 returnsA number from 0-25?The ASCII code (or Unicode) of the letter?

HashMaps p. 5/16

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

Choosing a hash function for StringsWhat if the hash function for a String returnsThe 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] *31n-1 + s[1]*31n-2 + … + s[n-1]*310

HashMaps p. 6/16

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

Step 2: get the right integerOnce 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

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

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

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

Resolving collisions: chainingA chained hashtable has an array size of 512. What is the

maximum number of entries that can be placed in the table?

A.256B.511C.512D.164E.There is no maximum.

HashMaps p. 9/16

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

Resolving collisions: linear probingWhat 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

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

Resolving collisions: Quadratic probingWhat 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

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

Resolving collisions: Double HashingWhat is double hashing?What are its pros and cons?

HashMaps p. 12/16

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

Deleting elements from a hashtableHow 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

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

Load factorSuppose you place m items in a hashtable with an

array size of s. What is the correct formula for the load factor?

A.s+mB.s-mC.m-sD.m*sE.m/s

HashMaps p. 14/16

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

Java’s HashMap classWhat 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

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

Coming attractionsNext 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