1 Chapter 9 Hash Table Aree Teeraparbseree, Ph.D.
-
Upload
kellie-mitchell -
Category
Documents
-
view
221 -
download
0
Transcript of 1 Chapter 9 Hash Table Aree Teeraparbseree, Ph.D.
2
What is Hash table? Hash table is an associative array Hash table is made up of 2 parts:
an array a mapping function (hash function)
Hash function convert key to array index
The index is calledthe hash value ofthe key
3
Simple example of Hash table (1)
Hash table: array of string
Hash table size is 12 Hash function takes a
string as input Hash value will be
the sum of the ASCII characters that make up the string mod the size of the table
Hash table (strings)0 NULL
1 NULL
2 NULL
3 NULL
4 NULL
5 NULL
6 NULL
7 NULL
8 NULL
9 NULL
10 NULL
11 NULL
http://www.sparknotes.com/cs/searching/hashtables/
4
Simple example of Hash table (2)
int hash(char *str, int table_size) {
int sum=0;
if (str == NULL)
return -1;
for( ; *str; str++) // *str != ‘\0’
sum += *str;
return sum%table_size;
}
// covert to integer with its Ascii code
http://www.sparknotes.com/cs/searching/hashtables/
5
Simple example of Hash table (3)
Store a string into the table: "Steve".
hash("Steve",12) = 3 Index = 3 Store: "Spark" hash("Spark",12) = 6 Store: "Notes" hash("Notes",12) = 3
Collision with Steve!!
Hash table (strings)0 NULL
1 NULL
2 NULL
3 Steve / Notes !!
4 NULL
5 NULL
6 Spark
7 NULL
8 NULL
9 NULL
10 NULL
11 NULL
http://www.sparknotes.com/cs/searching/hashtables/
6
Implementing A Hash Table In C
Create a Hash function. Insert items in a Hash table. Delete items in the table. Search for a specific data-value in the
table.
8
001 John Smith
005 Joey Bill
007 Lisa Smith
…
100 Sandra dee
Hash001100007
Key
address data
001
100
007
Direct
Key = Hash value
9
001 John Smith
005 Joey Bill
007 Lisa Smith
…
701 Anna Lee
Hash541000154107015410005
Key
address data
001
005
701
hash(5410001) = 1hash(5410701) = 701hash(5410005) = 5
5410001-5410000 = 15410701-5410000 = 7015410005-5410000 = 5
Subtraction
10
001 John Smith
005 Joey Bill
007 Lisa Smith
…
701 Anna Lee
Hash140200700911220
Key
address data
001
005
701
hash(14020) = 1hash(07009) = 701hash(11220) = 5
14020%701 + 1 = 107009%701 + 1 = 70111220%701 + 1 = 5
Modulo-Division
11
000
…
035 Henry James
131 Eddy Brown
…
951 Jamie Ben
…
999
Hash310025239725390980552321310150234821
Key
address data
035
131
951
hash(310025239725) = 035hash(390980552321) = 951hash(310150234821) = 131
Index from 000-999310025239725 = 035390980552321 = 951310150234821 = 131
Digit Extraction
12
Collisions: Solution
1. Open Hashing (separate chaining): keeping the colliding record in a linked list.
2. Close Hashing (Open Addressing): place the colliding record in another location that is still openLinear ProbingDouble Hashing
14
Linear Probing Let i = hash(key) If data[i] does not already contain a
record, then store the record in data[i] If data[i] already contains a record then
try data[i+1]. If that location already contains a record, try data[i+2] and so on.
15
Double Hashing Let i = hash(key) If data[i] does not already contain a record, then store the record
in data[i] If data[i] already contains a record then
let i = (i+hash2(key))%SIZE, and try new data[i].
If that location already contains a record, then
let i = (i+hash2(key))%SIZE, and try that data[i], until a vacant position is found.
Possible implementation of hash2(key) :- hash2(key) = 1 + (key % (SIZE - 2)- hash2(key) = max(1, (key / SIZE) % SIZE)
16
Search for a specific key
Use key to calculate the hash value. Check that location of the array for the
key. Keep moving forward until you find the
key, or you reach an empty spot. (depend on the collision solution)