More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.
-
Upload
melvyn-moody -
Category
Documents
-
view
213 -
download
0
description
Transcript of More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.
![Page 1: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/1.jpg)
More on Hash Tables
Andy WangData Structures, Algorithms, and
Generic Programming
![Page 2: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/2.jpg)
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)
![Page 3: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/3.jpg)
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
![Page 4: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/4.jpg)
Insert()
Name: Bobby HillAddress: Arlen
0123
Hash(name)
Hash Table
![Page 5: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/5.jpg)
Insert()
Name: Bobby HillAddress: Arlen
01 Bobby Hill Arlen23
Hash(name)
Hash Table
![Page 6: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/6.jpg)
Insert()
Name: Bart SimpsonAddress: Springfield
01 Bobby Hill Arlen23
Hash(name)
Hash Table
Icarumba!
![Page 7: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/7.jpg)
Linear Probing
Name: Bart SimpsonAddress: Springfield
01 Bobby Hill Arlen23
Hash(name)
Hash Table
![Page 8: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/8.jpg)
Linear Probing
Name: Bart SimpsonAddress: Springfield
01 Bobby Hill Arlen2 Bart Simpson Springfield3
Hash(name)
Hash Table
![Page 9: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/9.jpg)
Remove()
Name: Bobby Hill
01 Bobby Hill Arlen2 Bart Simpson Springfield3
Hash Table
![Page 10: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/10.jpg)
Remove()
01 Bobby Hill Arlen2 Bart Simpson Springfield3
Hash(name)
Hash Table
Name: Bobby Hill
![Page 11: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/11.jpg)
Remove()
012 Bart Simpson Springfield3
Hash(name)
Hash Table
Name: Bobby Hill
![Page 12: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/12.jpg)
Lookup()
Name: Bart Simpson
012 Bart Simpson Springfield3
Hash Table
![Page 13: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/13.jpg)
Lookup()
012 Bart Simpson Springfield3
Hash(name)
Hash Table
Name: Bart Simpson
![Page 14: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/14.jpg)
Insert()
Name: Bart SimpsonAddress: Springfield
012 Bart Simpson Springfield3
Hash(name)
Hash Table
![Page 15: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/15.jpg)
Tricky Case…
Name: Bart SimpsonAddress: Springfield
01 Bart Simpson Springfield2 Bart Simpson Springfield3
Hash(name)
Hash Table
Oops…
![Page 16: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/16.jpg)
To Handle Tricky Cases
Insert(…) {if (Lookup(…) == false) {// insert
}}Remove(…) {
if (entry[hash] empty || entry[hash] != key) // linear search for the entry
}}
![Page 17: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/17.jpg)
To Handle Tricky Cases
Lookup(…) {if (entry[hash] empty || entry[hash] != key)
// linear search for the entry}
}
![Page 18: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.](https://reader036.fdocuments.us/reader036/viewer/2022090107/5a4d1be07f8b9ab0599dee98/html5/thumbnails/18.jpg)
How can we make it more efficient?