Hash Table - Prince of Songkla University
Download
Report
Transcript Hash Table - Prince of Songkla University
Chapter 9
Hash Table
Aree Teeraparbseree, Ph.D
1
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 called
the hash value of
the key
2
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
http://www.sparknotes.com/cs/searching/hashtables/
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
3
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/
4
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!!
http://www.sparknotes.com/cs/searching/hashtables/
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
5
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.
6
Hash function
Direct
Subtraction
Modulo-Division
Digit Extraction
Etc..
7
address
Direct
data
001
John Smith
005
Joey Bill
007
Lisa Smith
Key
001
100
007
001
Hash
100
007
Key = Hash value
…
100
Sandra dee
8
address
Subtraction
data
001
John Smith
005
Joey Bill
007
Lisa Smith
5410001-5410000 = 1
5410701-5410000 = 701
5410005-5410000 = 5
Key
5410001
5410701
5410005
Hash
001
701
005
hash(5410001) = 1
hash(5410701) = 701
hash(5410005) = 5
…
701
Anna Lee
9
address
Modulo-Division
data
001
John Smith
005
Joey Bill
007
Lisa Smith
14020%701 + 1 = 1
07009%701 + 1 = 701
11220%701 + 1 = 5
Key
14020
07009
11220
Hash
001
701
005
hash(14020) = 1
hash(07009) = 701
hash(11220) = 5
…
701
Anna Lee
10
address
Digit Extraction
data
000
…
Index from 000-999
310025239725 = 035
390980552321 = 951
310150234821 = 131
035
Henry James
131
Eddy Brown
Key
310025239725
390980552321
310150234821
Hash
035
951
131
…
951
hash(310025239725) = 035
hash(390980552321) = 951
hash(310150234821) = 131
Jamie Ben
…
999
11
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 open
Linear
Probing
Double Hashing
12
Separate Chaining
13
http://www.sparknotes.com/cs/searching/hashtables/section1.rhtml
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.
14
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)
15
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)
16
Deleting a record
Records may also be deleted from a hash
table.
But the location must not be left as an
ordinary "empty spot" since that could
interfere with searches.
The location must be marked in some
special way so that a search can tell that
the spot used to have something in it.
17