Beruflich Dokumente
Kultur Dokumente
Dictionary
• A Dictionary is a collection of elements.
• Each element has a field called key.
– (Key, Data)
• Every key is usually distinct.
• Division Methods
– The key k is divided by some number m and the remainder
is used as the hash address of k.
–h(k) = K mod m
This gives the indexes in the range 0 to m-1 so the
hash table should be of size m.
– For example:
Consider a hash table with 9 elements i.e., m=9.
Then the hash function will map the key 132 to slot 6.
h(132) = 132 % 9 = 6
Division Methods
• Example:
Let m=100 and k= 2345.
h(2345) = 2345 % 100 = 45
Consider another key k=2445
h(2445) = 2445 % 100 = 45
• Example:
K: 3205 7148 2345
k2: 10272025 51093904 5499025
h(k): 72 93 99
Mid-Square Method
• Note: If a two digit address is required,
positions 4 to 5 (from right to left) could be
chosen.
Folding Method
• In the first step, the key k is divided into number of parts
k1,k2,k3,…,kn (from left to right), where each part has the same
number of digits except the last part, which can has lesser digits.
• In the second step, these parts are added together and the hash
value is obtained by ignoring the last carry(if any).
• Example:
K: 9235 714 71458
Parts: 92, 35 71, 4 71, 45, 8
Sum
Of
parts : 127 75 124
h(k): 27 75 24
Disadvantage of Hashing
• When an element is inserted, if it hashes to the same value as
an already inserted element, then we have a collision.
• Chaining:
– This is a technique used to avoid collisions.
– The idea is to store the items that has hash value to
the same value in the sorted list.