Hashing as a Dictionary Implementation Chapter 20 Slides














- Slides: 14
Hashing as a Dictionary Implementation Chapter 20 Slides by Steve Armstrong Le. Tourneau University Longview, TX ã 2007, Prentice Hall
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 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 get. Value, remove, and add Iterators • Java Class Library: the Class Hash. Map § § 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 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Load Factor 2 • 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 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 3 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. 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 5 Note: for quadratic probing or double hashing, should have < 0. 5 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. 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 6 Note: Reasonable efficiency requires only < 1 Fig. 20 -3 Average number of comparisons required by search of hash table for given values of load factor when using separate chaining. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X
Rehashing 7 • 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 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 8 Fig. 20 -4 Average number of comparisons required by search of hash table versus for 4 techniques when search is (a) successful; (b) unsuccessful. 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 9 Fig. 20 -5 A hash table and one of its entry objects 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 11 • Private class Table. Entry made internal to dictionary class • Note source code of Hashed. Dictionary which uses Table. Entry • Note methods 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 17 Fig. 19 -16 A hash table containing dictionary entries, removed entries, and null values. 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 Hash. Map 18 • Assumes search-key objects belong to a class that overrides methods hash. Code and equals • Hash table is collection of buckets • Constructors § public Hash. Map() § public Hash. Map(int initial. Size, float max. Load. Factor) § public Hash. Map(Map<? extends K, ? extends V> table) Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -237045 -X