Exp A-2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Assignment No:- C-15

Implement all the functions of a dictionary (ADT) using


hashing.
Data: Set of (key, value) pairs, Keys are mapped to
values, Keys must be comparable, Keys must be unique
Standard Operations: Insert (key, value), Find(key),
Delete(key)
Title:
Implement all the functions of a dictionary (ADT) using hashing.

Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys must
be unique

Standard Operations: Insert (key, value), Find(key), Delete(key)

Objectives:

1. To understand Dictionary (ADT)


2. To understand concept of hashing
3. To understand concept & features like searching using hash function.

Learning Objectives:
 To understand Dictionary(ADT)
 To understand concept of hashing
 To understand concept & features like searching using hash function.

Learning Outcome:

 Define class for Dictionary using Object Oriented features.


 Analyze working of hash function.

Theory:

Dictionary ADT

Dictionary (map, association list) is a data structure, which is generally an association of


unique keys with some values. One may bind a value to a key, delete a key (and naturally an
associated value) and lookup for a value by the key. Values are not required to be unique. Simple
usage example is an explanatory dictionary. In the example, words are keys and explanations are
values.
Dictionary Operations

 Dictionary create()
creates empty dictionary

 boolean isEmpty(Dictionary d)
tells whether the dictionary d is empty

 put(Dictionary d, Key k, Value v)


associates key k with a value v; if key k already presents in the dictionary old value is
replaced by v

 Value get(Dictionary d, Key k)


returns a value, associated with key kor null, if dictionary contains no such key

 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

Following are the basic primary operations of a hash table.

 Search − Searches an element in a hash table.


 Insert − inserts an element in a hash table.
 delete − Deletes an element from a 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.

int hashCode(int key){


return key % SIZE;
}

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

struct DataItem *search(int key) {


//get the hash
int hashIndex = hashCode(key);

//move in array until an empty


while(hashArray[hashIndex] != NULL) {

if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];

//go to next cell


++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

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

void insert(int key,int data) {


struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;

//get the hash


int hashIndex = hashCode(key);

//move in array until an empty or deleted cell


while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
//go to next cell
++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

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

struct DataItem* delete(struct DataItem* item) {


int key = item->key;

//get the hash


int hashIndex = hashCode(key);

//move in array until an empty


while(hashArray[hashIndex] !=NULL) {

if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];

//assign a dummy item at deleted position


hashArray[hashIndex] = dummyItem;
return temp;
}

//go to next cell


++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

return NULL;
}

Software Required: g++ / gcc compiler- / 64 bit Fedora, eclipse IDE

Input: No. of. elements with key and value pair.

Output: Create dictionary using hash table and search the elements in table.

Conclusion: This program gives us the knowledge of dictionary(ADT).

OUTCOME

Upon completion Students will be able to:

ELO1: Learn object oriented Programming features.


ELO2: Understand & implement Dictionary (ADT) using hashing.

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
{

cout<<"\n 1. without chain";


cout<<"\n 2. with chain without replacement";
cout<<"\n 3. with chain without replacement";
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1: wochain();
break;
case 2: woreplacement();
break;
case 3: wreplacement();
break;
}
}while(ch<4);
}

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
{

cout<<"\n data not present in hash table";


flag=1;
break;
}
}
if(flag==0)
cout<<"data present at"<<loc;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy