Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++: struct valueType //the data type for the information { double data1; dou

ID: 3871088 • Letter: I

Question

IN C++:

struct valueType //the data type for the information

{

double data1;

double data2;

};

struct itemType //the data type for each entry in the dictionary

{

int key; //the search key

valueType value; //the data

};

Dictionary implementation using linear and quadratic probing

(If linearProbing is set to "true" use linear probing to resolve collisions, otherwise use quadratic probing.)

Implement functions “search” and “remove” functions.

bool search(int key, int &comp);

* search for key in the dictionary, returns true if succesful.

* comp returns the number of entries checked.

bool remove(int key);

* remove entry with key "key" from dictionary

Explanation / Answer

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int TABLE_SIZE = 5;

class HashNode
{
public:
int key;
int value;
HashNode(int key, int value)
{
this->key = key;
this->value = value;
}
};

class DeletedNode:public HashNode
{
private:
static DeletedNode *entry;
DeletedNode():HashNode(-1, -1)
{}
public:
static DeletedNode *getNode()
{
if (entry == NULL)
entry = new DeletedNode();
return entry;
}
};
DeletedNode *DeletedNode::entry = NULL;

class HashMap
{
private:
HashNode **htable;
public:
HashMap()
{
htable = new HashNode* [TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
{
htable[i] = NULL;
}
}

~HashMap()
{
for (int i = 0; i < TABLE_SIZE; i++)
{
if (htable[i] != NULL && htable[i] != DeletedNode::getNode())
delete htable[i];
}
delete[] htable;
}
  
int HashFunc(int key)
{
return key % TABLE_SIZE;
}
  
void Insert(int key, int value)
{
int hash_val = HashFunc(key);
int init = -1;
int deletedindex = -1;
while (hash_val != init && (htable[hash_val]
== DeletedNode::getNode() || htable[hash_val]
!= NULL && htable[hash_val]->key != key))
{
if (init == -1)
init = hash_val;
if (htable[hash_val] == DeletedNode::getNode())
deletedindex = hash_val;
hash_val = HashFunc(hash_val + 1);
}
if (htable[hash_val] == NULL || hash_val == init)
{
if(deletedindex != -1)
htable[deletedindex] = new HashNode(key, value);
else
htable[hash_val] = new HashNode(key, value);
}
if(init != hash_val)
{
if (htable[hash_val] != DeletedNode::getNode())
{
if (htable[hash_val] != NULL)
{
if (htable[hash_val]->key == key)
htable[hash_val]->value = value;
}
}
else
htable[hash_val] = new HashNode(key, value);
}
}

int Search(int key)
{
int hash_val = HashFunc(key);
int init = -1;
while (hash_val != init && (htable[hash_val]
== DeletedNode::getNode() || htable[hash_val]
!= NULL && htable[hash_val]->key != key))
{
if (init == -1)
init = hash_val;
hash_val = HashFunc(hash_val + 1);
}
if (htable[hash_val] == NULL || hash_val == init)
return -1;
else
return htable[hash_val]->value;
}

void Remove(int key)
{
int hash_val = HashFunc(key);
int init = -1;
while (hash_val != init && (htable[hash_val]
== DeletedNode::getNode() || htable[hash_val]
!= NULL && htable[hash_val]->key != key))
{
if (init == -1)
init = hash_val;
hash_val = HashFunc(hash_val + 1);
}
if (hash_val != init && htable[hash_val] != NULL)
{
delete htable[hash_val];
htable[hash_val] = DeletedNode::getNode();
}
}
};

int main()
{
HashMap hash;
int key, value;
int choice;
while(1)
{
cout<<" ----------------------"<<endl;
cout<<"Operations on Hash Table"<<endl;
cout<<" ----------------------"<<endl;
cout<<"1.Insert element into the table"<<endl;
cout<<"2.Search element from the key"<<endl;
cout<<"3.Delete element at a key"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter element to be inserted: ";
cin>>value;
cout<<"Enter key at which element to be inserted: ";
cin>>key;
hash.Insert(key, value);
break;
case 2:
cout<<"Enter key of the element to be searched: ";
cin>>key;
if(hash.Search(key) == -1)
{
cout<<"No element found at key "<<key<<endl;
continue;
}
else
{
cout<<"Element at key "<<key<<" : ";
cout<<hash.Search(key)<<endl;
}
break;
case 3:
cout<<"Enter key of the element to be deleted: ";
cin>>key;
hash.Remove(key);
break;
case 4:
exit(1);
default:
cout<<" Enter correct option ";
}
}
return 0;
}

#include <iostream>

#include <cstdlib>

#define MIN_TABLE_SIZE 10

using namespace std;

enum EntryType {Legitimate, Empty, Deleted};

struct HashNode

{

int element;

enum EntryType info;

};

struct HashTable

{

int size;

HashNode *table;

};

bool isPrime (int n)

{

if (n == 2 || n == 3)

return true;

if (n == 1 || n % 2 == 0)

return false;

for (int i = 3; i * i <= n; i += 2)

if (n % i == 0)

return false;

return true;

}

int nextPrime(int n)

{

if (n <= 0)

n == 3;

if (n % 2 == 0)

n++;

for (; !isPrime( n ); n += 2);

return n;

}

int HashFunc(int key, int size)

{

return key % size;

}

HashTable *initializeTable(int size)

{

HashTable *htable;

if (size < MIN_TABLE_SIZE)

{

cout<<"Table Size Too Small"<<endl;

return NULL;

}

htable = new HashTable;

if (htable == NULL)

{

cout<<"Out of Space"<<endl;

return NULL;

}

htable->size = nextPrime(size);

htable->table = new HashNode [htable->size];

if (htable->table == NULL)

{

cout<<"Table Size Too Small"<<endl;

return NULL;

}

for (int i = 0; i < htable->size; i++)

{

htable->table[i].info = Empty;

htable->table[i].element = NULL;

}

return htable;

}

int Find(int key, HashTable *htable)

{

int pos = HashFunc(key, htable->size);

int collisions = 0;

while (htable->table[pos].info != Empty &&

htable->table[pos].element != key)

{

pos = pos + 2 * ++collisions -1;

if (pos >= htable->size)

pos = pos - htable->size;

}

return pos;

}

void Insert(int key, HashTable *htable)

{

int pos = Find(key, htable);

if (htable->table[pos].info != Legitimate)

{

htable->table[pos].info = Legitimate;

htable->table[pos].element = key;

}

}

HashTable *Rehash(HashTable *htable)

{

int size = htable->size;

HashNode *table = htable->table;

htable = initializeTable(2 * size);

for (int i = 0; i < size; i++)

{

if (table[i].info == Legitimate)

Insert(table[i].element, htable);

}

free(table);

return htable;

}

void Retrieve(HashTable *htable)

{

for (int i = 0; i < htable->size; i++)

{

int value = htable->table[i].element;

if (!value)

cout<<"Position: "<<i + 1<<" Element: Null"<<endl;

else

cout<<"Position: "<<i + 1<<" Element: "<<value<<endl;

}

}

int main()

{

int value, size, pos, i = 1;

int choice;

HashTable *htable;

while(1)

{

cout<<" ----------------------"<<endl;

cout<<"Operations on Quadratic Probing"<<endl;

cout<<" ----------------------"<<endl;

cout<<"1.Initialize size of the table"<<endl;

cout<<"2.Insert element into the table"<<endl;

cout<<"3.Display Hash Table"<<endl;

cout<<"4.Rehash The Table"<<endl;

cout<<"5.Exit"<<endl;

cout<<"Enter your choice: ";

cin>>choice;

switch(choice)

{

case 1:

cout<<"Enter size of the Hash Table: ";

cin>>size;

htable = initializeTable(size);

cout<<"Size of Hash Table: "<<nextPrime(size);

break;

case 2:

if (i > htable->size)

{

cout<<"Table is Full, Rehash the table"<<endl;

continue;

}

cout<<"Enter element to be inserted: ";

cin>>value;

Insert(value, htable);

i++;

break;

case 3:

Retrieve(htable);

break;

case 4:

htable = Rehash(htable);

break;

case 5:

exit(1);

default:

cout<<" Enter correct option ";

}

}

return 0;

}