Week 12 Hashing
Week 12 Hashing
Algorithms
Hashing
Agenda
Hashing
Hashing
Hashing
Hashing
Hashing
In the figure is an ideal Hashtable.
All the Distinct Greek Alphabetical
Names hash to distinct keys.
Beta Hashes to 1.
Theta Hashes to 3.
Epsilon Hashes to 6.
Alpha
Beta
Gamma
Theta
Omega
Delta
Epsilon
Pie
Hashing
Hashing
Hashing
Hashing
Simple Solution:
Hash Function
Hash Function
Hash Function
For the situations like this its preferred to have the table
size as Prime.
When the keys are random integers, this function is
effective in distributing the keys evenly.
When keys are string values an effective hash function can
be adding the ASCII values and using our Mod function to
create mapping.
Hash Function
int HashValue = 0;
While (*Key != \0)
{
HashValue + = *Key;
}
return HashValue % TableSize;
Hash Function
Hash Function
Separate Chaining
Separate Chaining
Separate Chaining
Hashing Implementation
}
struct HashTbl
{
ElementType Element;
Position Next;
int TableSize;
List *TheLists;
Hashing Implementation
HashTable H = NextPrime(TableSize);
HTheLists =
malloc(sizeof(List)*HTableSize);
For (int i =0;i<HTableSize;i++)
{
}
return H;
Hashing Implementation
Position P;
List L = H TheLists[Hash(Key, HTableSize)];
P=L Next;
While (P != NULL && PElement !=Key)
{
// Strcmp
P= PNext;
}
return P;
Hashing Implementation