180 likes | 629 Views
More on Hash Tables. Andy Wang Data Structures, Algorithms, and Generic Programming. Why Hash Tables?. Arguments Linear search is simple Hash table does not save that much time Counter arguments What if the data volume is high? (internet routers)
E N D
More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming
Why Hash Tables? • Arguments • Linear search is simple • Hash table does not save that much time • Counter arguments • What if the data volume is high? (internet routers) • What if the data set if large? (yellow page)
Steps to Build a Hash Table 1. Build a table to support basic operations • Insert • Lookup • Remove 2. Use a hash function to determine the table entry 3. Add mechanisms to handle collisions
Hash(name) Insert() Hash Table
Hash(name) Insert() Hash Table
Hash(name) Insert() Icarumba! Hash Table
Hash(name) Linear Probing Hash Table
Hash(name) Linear Probing Hash Table
Remove() Hash Table
Hash(name) Remove() Hash Table
Hash(name) Remove() Hash Table
Lookup() Hash Table
Hash(name) Lookup() Hash Table
Hash(name) Insert() Hash Table
Hash(name) Tricky Case… Oops… Hash Table
To Handle Tricky Cases Insert(…) { if (Lookup(…) == false) { // insert } } Remove(…) { if (entry[hash] empty || entry[hash] != key) // linear search for the entry } }
To Handle Tricky Cases Lookup(…) { if (entry[hash] empty || entry[hash] != key) // linear search for the entry } }