Data Structures in C++, Testing Hash Functions Hello Chegg expert, I am a comput
ID: 3835930 • Letter: D
Question
Data Structures in C++, Testing Hash Functions
Hello Chegg expert, I am a computer science student and I'm currently learning about hash functions in C++.
This is an assignment that I struggled on which I need to implement some of the hash functions and test them to see which one is the more efficient one.
Here is the assignment:
This assignment is for my class server so you will probably need the links for some of the information below.
1. Text file containing 100000 English words: http://staffwww.fullcoll.edu/aclifton/files/words.txt
- You can just pretend that I downloaded the text file to my server and copied them to my code using ifstream dictionary("word.txt");
2. This is the link for the full distribution if you would need them: https://sourceforge.net/projects/boost/files/boost/1.63.0/
I know that this is definitely not an easy assignment which may take some time, but I really would like to get this one down before I end the semester.
I will really appreciate your help and effort if you could code them successfully.
Thank you in advance!
In this assignment you are going to test various hash functions to see how good they are, in terms of how many collisions they have. Your input will be strings, in fact, all the strings that are defined in the systems dictionary. This is located in /usr/share/dict/words you can download a copy to your local computer for testing here. (If you try to download it your browser may say that its a binary file, but it is in fact a text file, with one word per line.) Note that if you try to open the system copy of the dictionary in your code on the server, you will have to open it for reading only, otherwise opening the file will fail. This file contains just under 100,ooo English words, which were going to use to test the uniformity of various hash functions. Your hash functions will hash strings into 16-bit (not 32-bit) int s. This is important, because were going to keep a table of the number of collisions for each hash value. With 16-bit ints there are only 65,536 possible hash values, so this table will easily fit in memory. If we used 32-bit ints then there would be 4,294,967,296 possble hashes, a more troublesome amount. For C++, you can get a 16-bit unsigned int type by doing Hinclude cstdint uint16_t x; x has exactly 16 bits and is signedExplanation / Answer
#include <iostream>
#include <cstdio>
#include <cstdlib>
utilizing namespace sexually transmitted disease;
const int TABLE_SIZE = 5;
/*
* HashNode Class Declaration
*/
class HashNode
{
open:
int key;
int esteem;
HashNode(int key, int esteem)
{
this->key = key;
this->value = esteem;
}
};
/*
* DeletedNode Class Declaration
*/
class DeletedNode:public HashNode
{
private:
static DeletedNode *entry;
DeletedNode():HashNode(- 1, - 1)
{}
open:
static DeletedNode *getNode()
{
on the off chance that (section == NULL)
passage = new DeletedNode();
return passage;
}
};
DeletedNode *DeletedNode::entry = NULL;
/*
* HashMap Class Declaration
*/
class HashMap
{
private:
HashNode **htable;
open:
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++)
{
on the off chance that (htable[i] != NULL && htable[i] != DeletedNode::getNode())
erase htable[i];
}
delete[] htable;
}
/*
* Hash Function
*/
int HashFunc(int key)
{
return key % TABLE_SIZE;
}
/*
* Insert Element at a key
*/
void Insert(int key, int esteem)
{
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))
{
in the event that (init == - 1)
init = hash_val;
in the event that (htable[hash_val] == DeletedNode::getNode())
deletedindex = hash_val;
hash_val = HashFunc(hash_val + 1);
}
in the event that (htable[hash_val] == NULL || hash_val == init)
{
if(deletedindex != - 1)
htable[deletedindex] = new HashNode(key, esteem);
else
htable[hash_val] = new HashNode(key, esteem);
}
if(init != hash_val)
{
in the event that (htable[hash_val] != DeletedNode::getNode())
{
in the event that (htable[hash_val] != NULL)
{
in the event that (htable[hash_val]->key == key)
htable[hash_val]->value = esteem;
}
}
else
htable[hash_val] = new HashNode(key, esteem);
}
}
/*
* Search Element at a key
*/
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))
{
on the off chance that (init == - 1)
init = hash_val;
hash_val = HashFunc(hash_val + 1);
}
on the off chance that (htable[hash_val] == NULL || hash_val == init)
return - 1;
else
return htable[hash_val]->value;
}
/*
* Remove Element at a key
*/
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))
{
in the event that (init == - 1)
init = hash_val;
hash_val = HashFunc(hash_val + 1);
}
in the event that (hash_val != init && htable[hash_val] != NULL)
{
erase htable[hash_val];
htable[hash_val] = DeletedNode::getNode();
}
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.