more on hash tables andy wang data structures, algorithms, and generic programming
DESCRIPTION
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 collisionsTRANSCRIPT
More on Hash Tables
Andy WangData 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
Insert()
Name: Bobby HillAddress: Arlen
0123
Hash(name)
Hash Table
Insert()
Name: Bobby HillAddress: Arlen
01 Bobby Hill Arlen23
Hash(name)
Hash Table
Insert()
Name: Bart SimpsonAddress: Springfield
01 Bobby Hill Arlen23
Hash(name)
Hash Table
Icarumba!
Linear Probing
Name: Bart SimpsonAddress: Springfield
01 Bobby Hill Arlen23
Hash(name)
Hash Table
Linear Probing
Name: Bart SimpsonAddress: Springfield
01 Bobby Hill Arlen2 Bart Simpson Springfield3
Hash(name)
Hash Table
Remove()
Name: Bobby Hill
01 Bobby Hill Arlen2 Bart Simpson Springfield3
Hash Table
Remove()
01 Bobby Hill Arlen2 Bart Simpson Springfield3
Hash(name)
Hash Table
Name: Bobby Hill
Remove()
012 Bart Simpson Springfield3
Hash(name)
Hash Table
Name: Bobby Hill
Lookup()
Name: Bart Simpson
012 Bart Simpson Springfield3
Hash Table
Lookup()
012 Bart Simpson Springfield3
Hash(name)
Hash Table
Name: Bart Simpson
Insert()
Name: Bart SimpsonAddress: Springfield
012 Bart Simpson Springfield3
Hash(name)
Hash Table
Tricky Case…
Name: Bart SimpsonAddress: Springfield
01 Bart Simpson Springfield2 Bart Simpson Springfield3
Hash(name)
Hash Table
Oops…
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}
}
How can we make it more efficient?