View
569
Download
2
Category
Preview:
DESCRIPTION
Courtesy: Sir Nabeel Sabir's Share Folder
Citation preview
An array in which TableNodes are not stored consecutively
Their place of storage is calculated using the key and a hash function
Keys and entries are scattered throughout the array.
Implementation 6: Hashing
key entry
Key hash function
array index
4
10
123
insert: calculate place of storage, insert TableNode; (1)
find: calculate place of storage, retrieve entry; (1)
remove: calculate place of storage, set it to null; (1)
Hashing
key entry
4
10
123
All are constant time (1) !
Hashing
We use an array of some fixed size T to hold the data. T is typically prime.
Each key is mapped into some number in the range 0 to T-1 using a hash function, which ideally should be efficient to compute.
Example: fruits
Suppose our hash function gave us the following values: hashCode("apple") = 5
hashCode("watermelon") = 3hashCode("grapes") = 8hashCode("cantaloupe") = 7hashCode("kiwi") = 0hashCode("strawberry") = 9hashCode("mango") = 6hashCode("banana") = 2
kiwi
bananawatermelon
applemango
cantaloupegrapes
strawberry
0
1
2
3
4
5
6
7
8
9
Example
Store data in a table array: table[5] = "apple"
table[3] = "watermelon" table[8] = "grapes" table[7] = "cantaloupe" table[0] = "kiwi" table[9] = "strawberry" table[6] = "mango" table[2] = "banana"
kiwi
bananawatermelon
applemango
cantaloupegrapes
strawberry
0
1
2
3
4
5
6
7
8
9
Example
Associative array: table["apple"]
table["watermelon"] table["grapes"] table["cantaloupe"] table["kiwi"] table["strawberry"] table["mango"] table["banana"]
kiwi
bananawatermelon
applemango
cantaloupegrapes
strawberry
0
1
2
3
4
5
6
7
8
9
Example Hash Functions
If the keys are strings the hash function is some function of the characters in the strings.
One possibility is to simply add the ASCII values of the characters:
TableSizeABChExample
TableSizeistrstrhlength
i
)%676665()(:
%][)(1
0
Finding the hash function
int hashCode( char* s ){
int i, sum;sum = 0;for(i=0; i < strlen(s); i++ ) sum = sum + s[i]; // ascii value
return sum % TABLESIZE;
}
Example Hash Functions
Another possibility is to convert the string into some number in some arbitrary base b (b also might be a prime number):
TbbbABChExample
Tbistrstrhlength
i
i
)%676665()(:
%][)(
210
1
0
Example Hash Functions
If the keys are integers then key%T is generally a good hash function, unless the data has some undesirable features.
For example, if T = 10 and all keys end in zeros, then key%T = 0 for all keys.
In general, to avoid situations like this, T should be a prime number.
Recommended