60 likes | 233 Views
Hash Tables. ADT Data Dictionary, with two operations Insert an item, Search for (and retrieve) an item How should we implement a data dictionary? Maintain a sorted array; use binary search Use sophisticated trees (later in curriculum) Use an array as a “hash table”.
E N D
Hash Tables • ADT Data Dictionary, with two operations • Insert an item, • Search for (and retrieve) an item • How should we implement a data dictionary? • Maintain a sorted array; use binary search • Use sophisticated trees (later in curriculum) • Use an array as a “hash table”
Hash Table Implementation • Maintain an array of the data to be stored in the data dictionary • Need one (or maybe two) hash functions to map the “data” to an index in the array • Need a “collision policy” to resolve conflicts when multiple data hash (map) to the same index • Insert, Search follow same probe sequence
Example Hash Table • Data will be strings • Array will be size 13 • h1(char *) = sum of ASCII values of characters in the string mod table size (13). • Collision policy – linear probing • When collision occurs add 1 mod table size to the location of the collision and continue to do that until an empty location is found
Hash “Facts” • Two “main” kinds of hash tables, called open addressing, and chained hashing • Hash tables are very efficient IF table is less than 50% full • Linear probing is NOT a good collision policy, BUT, its easy to implement and works well when table is less than 50% full • If you’re interested look into double hashing
Hash “Facts” • Two “main” kinds of hash tables, called open addressing, and chained hashing • Hash tables are very efficient IF table is less than 50% full • Linear probing is NOT a good collision policy, BUT, its easy to implement and works well when table is less than 50% full • If you’re interested look into double hashing
Hash Table Effectiveness From Data Structure Techniques, p. 154 Thomas A. Standish, 1980