CS 225
Data Structures
Mar March h 15 15 – Hash T Table C e Collisions
Wa Wade Fa Fagen-Ul Ulmsch schnei eider er, , Cra Craig Zi Zilles
CS 225 Data Structures Mar March h 15 15 Hash T Table C e - - PowerPoint PPT Presentation
CS 225 Data Structures Mar March h 15 15 Hash T Table C e Collisions Wade Fa Wa Fagen-Ul Ulmsch schnei eider er, , Cra Craig Zi Zilles (Example of open hashing) Collision Handling: Sep eparate e Chaining S = { 16, 8, 4, 13,
Data Structures
Mar March h 15 15 – Hash T Table C e Collisions
Wa Wade Fa Fagen-Ul Ulmsch schnei eider er, , Cra Craig Zi Zilles
S = { 16, 8, 4, 13, 29, 11, 22 } |S| = n h(k) = k % 7 |Array| = N
1 2 3 4 5 6 Worst Case SUHA Insert Remove/Find (Example of open hashing)
S = { 16, 8, 4, 13, 29, 11, 22 } |S| = n h(k) = k % 7 |Array| = N
1 2 3 4 5 6 (Example of closed hashing)
S = { 16, 8, 4, 13, 29, 11, 22 } |S| = n h(k) = k % 7 |Array| = N Try h(k) = (k + 0) % 7, if full… Try h(k) = (k + 1) % 7, if full… Try h(k) = (k + 2) % 7, if full… Try …
1 2 3 4 5 6 (Example of closed hashing) Worst Case SUHA Insert Remove/Find
Primary clustering: Description: Remedy:
S = { 16, 8, 4, 13, 29, 11, 22 } |S| = n h(k) = k % 7 |Array| = N Try h(k) = (k + 0*h2(k)) % 7, if full… Try h(k) = (k + 1*h2(k)) % 7, if full… Try h(k) = (k + 2*h2(k)) % 7, if full… Try … h(k, i) = (h1(k) + i*h2(k)) % 7
1 2 3 4 5 6 (Example of closed hashing)
Linear Probing:
Double Hashing:
Separate Chaining:
The expected number of probes for find(key) under SUHA
(Don’t memorize these equations, no need.) Instead, observe:
Linear Probing:
Double Hashing:
The expected number of probes for find(key) under SUHA
What if the array fills?
Which collision resolution strategy is better?
What structure do hash tables replace? What constraint exists on hashing that doesn’t exist with BSTs? Why talk about BSTs at all?