Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hashing as a Dictionary Implementation Chapter 20 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.

Similar presentations


Presentation on theme: "Hashing as a Dictionary Implementation Chapter 20 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."— Presentation transcript:

1 Hashing as a Dictionary Implementation Chapter 20 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

2 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Chapter Contents The Efficiency of Hashing  The Load Factor  The Cost of Open Addressing  The Cost of Separate Chaining Rehashing Comparing Schemes for Collision Resolution

3 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Chapter Contents A Dictionary Implementation that Uses Hashing  Entries in the Hash Table  Data Fields and Constructors  The Methods getValue, remove, and add  Iterators Java Class Library: the Class HashMap

4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Efficiency Observations Successful retrieval or removal  Same efficiency as successful search Unsuccessful retrieval or removal  Same efficiency as unsuccessful search Successful addition  Same efficiency as unsuccessful search Unsuccessful addition  Same efficiency as successful search

5 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Load Factor Perfect hash function not always possible or practical  Thus, collisions likely to occur As hash table fills  Collisions occur more often Measure for table fullness, the load factor

6 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Cost of Open Addressing Fig. 20-1 The average number of comparisons required by a search of the hash table for given values of the load factor when using linear probing.

7 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Cost of Open Addressing Fig. 19-12 The average number of comparisons required by a search of the hash table for given values of the load factor when using either quadratic probing or double hashing. Note: for quadratic probing or double hashing, should have < 0.5

8 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Cost of Separate Chaining Fig. 20-3 Average number of comparisons required by search of hash table for given values of load factor when using separate chaining. Note: Reasonable efficiency requires only < 1

9 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Rehashing When load factor becomes too large  Expand the hash table Double present size, increase result to next prime number Use method add to place current entries into new hash table

10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Comparing Schemes for Collision Resolution Fig.20-4 Average number of comparisons required by search of hash table versus for 4 techniques when search is (a) successful; (b) unsuccessful.

11 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X A Dictionary Implementation That Uses Hashing Fig. 20-5 A hash table and one of its entry objects

12 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Private class TableEntry made internal to dictionary class TableEntry Note source code of HashedDictionary which uses TableEntry HashedDictionary Note methods A Dictionary Implementation That Uses Hashing

13 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X A Dictionary Implementation That Uses Hashing Fig. 19-16 A hash table containing dictionary entries, removed entries, and null values.

14 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Java Class Library: The Class HashMap Assumes search-key objects belong to a class that overrides methods hashCode and equals Hash table is collection of buckets Constructors  public HashMap()  public HashMap(int initialSize)  public HashMap(int initialSize, float maxLoadFactor)  public HashMap(Map table)


Download ppt "Hashing as a Dictionary Implementation Chapter 20 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."

Similar presentations


Ads by Google