Lab 09 - Hashing
Lab 09 - Hashing
Overview
• Introduction
• Advantages
• Hash Function
• Hash Table
• Collision Resolution Techniques
• Separate Chaining
• Linear Chaining
• Quadratic Probing
• Double Hashing
• Application
• Reference
Introduction
• Hashing is a technique that is used to uniquely identify a specific
object from a group of similar objects.
• Some examples of how hashing is used in our lives include:
• In universities, each student is assigned a unique roll number that can
be used to retrieve information about them.
• In libraries, each book is assigned a unique number that can be used
to determine information about the book, such as its exact position in
the library or the users it has been issued to etc.
• In both these examples the students and books were hashed to a
unique number.
Introduction
Assume that you have an object and you want to assign a key to it to
make searching easy.
To store the key/value pair, you can use a simple array like a data
structure where keys (integers) can be used directly as an index to store
values. However, in cases where the keys are large and cannot be used
directly as an index, you should use hashing.
Introduction
• A hash function is any function that can be used to map a data set
of an arbitrary size to a data set of a fixed size, which falls into the
hash table.
• The values returned by a hash function are called hash values, hash
codes, hash sums, or simply hashes.
Hash function
• To store an element in the hash table you must insert it into a specific
linked list.
• If there is any collision (i.e. two different elements have same hash
value) then store both the elements in the same linked list.
Separate Chaining (Open Hashing)
Linear Probing
• In open addressing, instead of in linked lists, all entry records
are stored in the array itself.
f(i) = i2
• Probe sequence:
0th probe = h(k) mod TableSize
1th probe = (h(k) + 1) mod TableSize
2th probe = (h(k) + 4) mod TableSize
3th probe = (h(k) + 9) mod TableSize
...
ith probe = (h(k) + i2) mod TableSize
Quadratic Probing
Double hashing
• Double hashing is similar to linear probing and the only
difference is the interval between successive probes. Here, the
interval between probes is computed by using two hash
functions.
• Let us say that the hashed index for an entry record is an index
that is computed by one hashing function and the slot at that
index is already occupied. You must start traversing in a specific
probing sequence to look for an unoccupied slot. The probing
sequence will be:
Double hashing
Separate Chaining-
NOTE-
Separate Chaining is advantageous when it is
•Deletion is easier in separate
required to perform all the following operations
chaining.
on the keys stored in the hash table-
•This is because deleting a key from
•Insertion Operation
the hash table does not affect the
•Deletion Operation
other keys stored in the hash table.
•Searching Operation
Summary
}
C++ Program to Implement Hash
void delete_element(int key)//function used to delete elements from the hashtable
{
int indexkey=hashFunction(key);
list<int>::iterator i=hashtable[indexkey].begin();
for(;i!=hashtable[indexkey].end();i++)
{
if(*i==key)
break; }
if(i!=hashtable[indexkey].end())
{
hashtable[indexkey].erase(i); }
C++ Program to Implement Hash
}
C++ Program to Implement Hash
void display_table()//used to display hashtable values
{
for(int i=0;i<bucket;i++)
{
cout<<i;
list<int>::iterator j=hashtable[i].begin();
for(;j!=hashtable[i].end();j++)
{
cout<<"---->"<<*j;
}
cout<<endl;
}
C++ Program to Implement Hash
void search_element(int key)//used to search
element
{
if(a==1)
int a=0;//will be 1 if element exist
{
otherwise 0
cout<<"The element you wanted to
int indexkey=hashFunction(key);
search is present in the hashtable."<<endl;
list<int>::iterator
}
i=hashtable[indexkey].begin();
else
for(;i!=hashtable[indexkey].end();i++)
{
{
cout<<"Element not present"<<endl;
if(*i==key)
}
{
a=1;
}
break; }}
C++ Program to Implement Hash
~hashclass()//destructor
{
delete[]hashtable;
} };
C++ Program to Implement Hash
int main()
{
int bucketn,ch,element;
while(1)
{
cout<<"1.insert element to the hashtable"<<endl;
cout<<"2.search element from the hashtable"<<endl;
cout<<"3.delete element from the hashtable"<<endl;
cout<<"4.display elements of the hashtable"<<endl;
cout<<"5.exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>ch;
C++ Program to Implement Hash
switch(ch)
{
case 1:cout<<"enter the element"<<endl;
cin>>element;
hashelement.insert_element(element);
break;
case 2:cout<<"enter the element you want to search"<<endl;
cin>>element;
hashelement.search_element(element);
break;
case 3:cout<<"enter the element to be deleted"<<endl;
cin>>element;
hashelement.delete_element(element);
break;
C++ Program to Implement Hash
case 4:hashelement.display_table();
break;
case 5:return 0;
default:cout<<"enter a valid value"<<endl;
}
}
return 0;
}
OUTPUT: