Exp A-2
Exp A-2
Exp A-2
Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys must
be unique
Objectives:
Learning Objectives:
To understand Dictionary(ADT)
To understand concept of hashing
To understand concept & features like searching using hash function.
Learning Outcome:
Theory:
Dictionary ADT
Dictionary create()
creates empty dictionary
boolean isEmpty(Dictionary d)
tells whether the dictionary d is empty
remove(Dictionary d, Key k)
removes key k and associated value
destroy(Dictionary d)
destroys dictionary d
Hash Table is a data structure which stores data in an associative manner. In a hash table,
data is stored in an array format, where each data value has its own unique index value. Access
of data becomes very fast if we know the index of the desired data.
Thus, it becomes a data structure in which insertion and search operations are very fast
irrespective of the size of the data. Hash Table uses an array as a storage medium and uses hash
technique to generate an index where an element is to be inserted or is to be located from.
Hashing
Hashing is a technique to convert a range of key values into a range of indexes of an
array. We're going to use modulo operator to get a range of key values. Consider an example
of hash table of size 20, and the following items are to be stored. Item are in the (key,value)
format.
Basic Operations of hash table
1. DataItem
Define a data item having some data and key, based on which the search is to be conducted in a
hash table.
struct DataItem {
int data;
int key;
};
2. Hash Method
Define a hashing method to compute the hash code of the key of the data item.
3. Search Operation
Whenever an element is to be searched, compute the hash code of the key passed and locate the
element using that hash code as index in the array. Use linear probing to get the element ahead if
the element is not found at the computed hash code.
Example
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
return NULL;
}
4. Insert Operation
Whenever an element is to be inserted, compute the hash code of the key passed and locate the
index using that hash code as an index in the array. Use linear probing for empty location, if an
element is found at the computed hash code.
Example
hashArray[hashIndex] = item;
}
5. Delete Operation
Whenever an element is to be deleted, compute the hash code of the key passed and locate the
index using that hash code as an index in the array. Use linear probing to get the element ahead if
an element is not found at the computed hash code. When found, store a dummy item there to
keep the performance of the hash table intact.
Example
if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];
return NULL;
}
Output: Create dictionary using hash table and search the elements in table.
OUTCOME
Code :-
/*
* hash.cpp
*
* Created on: Feb 7, 2019
* Author: student
*/
#include<iostream>
using namespace std;
#define h(x) x%10
void wochain();
void woreplacement();
void wreplacement();
int data[10],flag[10],chain[10];
int main()
{
int ch;
do
{
void wochain()
{
int loc,key,in;
int i,j;
for(i=0;i<10;i++)
{
flag[i]=0;
}
for(i=1;i<=5;i++)
{
cout<<"\nenter data";
cin>>key;
loc=h(key);
if(flag[loc]==0)
{
data[loc]=key;
flag[loc]=1;
}
else
{
j=loc;in=0;
while(flag[j]==1 && in<10)
{
j=(j+1)%10;
in++;
}
data[j]=key;
flag[j]=1;
}
}
cout<<"\n"<<"Data"<<"||"<<"flag";
for(i=0;i<10;i++)
{
cout<<"\n"<<data[i]<<" ||"<<flag[i];
}
int x;
cout<<"enter data to search";
cin>>x;
for(i=0;i<10;i++)
{
loc=h(x);j=0;
while(data[loc]!=x && j<10)
{
loc=(loc+1)%10;
j++;
}
}
if(j==10)
{
cout<<"\n data not present in hash table";
}
else
cout<<"\n data present at"<<loc;
void woreplacement()
{
int loc,key,in;
int i,j;
for(i=0;i<10;i++)
{
flag[i]=0;
chain[i]=-1;
}
for(i=1;i<=5;i++)
{
cout<<"\n enter data";
cin>>key;
loc=h(key);
if(flag[loc]==0)
{
data[loc]=key;
flag[loc]=1;
}
else
{
j=loc;in=0;
while(flag[j]==1 && in<10)
{
j=(j+1)%10;
in++;
}
data[j]=key;
flag[j]=1;
if(h(key)==h(data[loc]))
{
while(chain[loc]!=-1)
loc=chain[loc];
chain[loc]=j;
}
}
}
cout<<"\n"<<"Data"<<"||"<<"flag"<<"||"<<"chain";
for(i=0;i<10;i++)
{
cout<<"\n"<<data[i]<<" ||"<<flag[i]<<" ||"<<chain[i];
}
int x;
cout<<"enter data to search";
cin>>x;
//for(i=0;i<10;i++)
loc=h(x);j=0;int flag=0;
while(data[loc]!=x && j<10 )
{
if(h(key)==h(data[loc]))
{
if(chain[loc]!=-1)
loc=chain[loc];
else
flag=1;
}
else
loc=(loc+1)%10;
j++;
}
if(j==10 || flag==1)
{
cout<<"\n data not present in hash table";
}
else
cout<<"\n data present at"<<loc;
}
void wreplacement()
{
int loc,key,in;
int i,j;
for(i=0;i<10;i++)
{
flag[i]=0;
chain[i]=-1;
}
for(i=1;i<=5;i++)
{
cout<<"\n enter data";
cin>>key;
loc=h(key);
if(flag[loc]==0)
{
data[loc]=key;
flag[loc]=1;
}
else
{
j=loc;in=0;
while(flag[j]==1 && in<10)
{
j=(j+1)%10;
in++;
}
if(in==10)
cout<<"hashtable is full";
else
{
if(h(key)==h(data[loc]))
{
data[j]=key;
flag[j]=1;
while(chain[loc]!=-1)
loc=chain[loc];
chain[loc]=j;
}
else
{
data[j]=data[loc];
flag[j]=1;
data[loc]=key;
int n;
n=h(data[j]);
while(chain[n]!=loc)
n=chain[n];
chain[n]=chain[loc];
chain[loc]=-1;
n=h(data[j]);
while(chain[n]!=-1)
n=chain[n];
chain[n]=j;
}
}
}
cout<<"\n"<<"Data"<<"||"<<"flag"<<"||"<<"chain";
for(i=0;i<10;i++)
{
cout<<"\n"<<data[i]<<" ||"<<flag[i]<<" ||"<<chain[i];
}
int x;
cout<<"enter data to search";
cin>>x;
//for(i=0;i<10;i++)
loc=h(x);j=0;int flag=0;
while(data[loc]!=x && j<10 )
{
if(h(key)==h(data[loc]))
{
if(chain[loc]!=-1)
loc=chain[loc];
else
{
cout<<"data not present";
flag=1;
break;
}
}
else
{