Question
Hi i was wondering if someone could over look my code. Its in c++. I have attached my code and the instructions for this assignment
Hi i was wondering if someone could over look my code. Its in c++.
I have attached my code and the instructions for this assignment
The goal is to implement a hash table in C/C++ using open addressing.
Part A : Hash Table Implementation
Create your own
hash table struct/class
that uses
open addressing
. Do not use any library data
structures for implementing your hash tables.
Use the multiplication method discussed in class for generating your
hash function
and
linear probing to resolve collisions in open addressing.
Part B : Populating the Hash Table
After constructing the hash table, do the following:
Generate a random number within the range (0 to 100000).
Map the generated number to a random integer and insert it into your hash table.
During insertion, measure the number of times that you have to reprobe the hash table
before inserting the value.
Repeat the above operations until your hash table has a load factor of 50% and 90%. Find the
average number of reprobes for inserting a random number into the hash table in both cases.
2
Part C : Search operation
Generate a random number within the range (0 to 100000). Do not seed your random
number generator.
Search for the number in your hash table.
Measure the number of times that you have to reprobe the hash table when it is 50% and
90% full to find the corresponding value.
Repeat the above operations 10000 times and measure the average number of reprobes that have
to be performed for a search operation in your hash table.
Implementation Details
The hash table must be resizable and its design must be independent of the number of
entries inserted into the table.
Design your hash table in such a way that the number of reprobes is very small.
If the number of reprobes is large, then modify your hash table design in such a way that
the issue is resolved
code
class hashInput { public: int key; int value; hashInput(int key,int value) { this->key=key; this->value=value; } };
class hashMap { private: hashInput **table; int size; int capacity; public: //Constructor hashMap() { capacity = 100000; size=0; table = new hashInput*[capacity]; for(int i=0;i //Inserting the hash function into the Hash Table void insertHash(int key,int value) { int index; index=(key%capacity); while(table[index]!=NULL&&table[index]->key!=key) { index++; index%=capacity; } if(table[index]==NULL||table[index]->key==-1) size++; table[index]= new hashInput(key,value); } int searchHash(int key) { int index; int ctr=0; index = (key%capacity); while(table[index]!= NULL) { if(ctr++>capacity) return 0; if(table[index]->key==key) return table[index]->value; index++; index%=capacity; } return 0; } //This function prints the value of the key and the index void display() { for(int i=0;i double loadFactor() { return (double)size/(double)capacity; } }; int main() { hashMap *hash50 = new hashMap(); //insertion 50% int index,ctr=0; int key,size; index = key%size; while(hash50->loadFactor()<0.5) { //insertion index++; int value = rand()%100000; hash50->insertHash(index,value); cout<<"Number of reprobes when load factor 50%:"< There are 3 Steps involved in it See step-by-step solutions with expert insights and AI powered tools for academic success Get the answers you need in no time with our AI-driven, step-by-step assistancekey; cout <
Step by Step Solution
Step: 1
Get Instant Access to Expert-Tailored Solutions
Step: 2
Step: 3
Ace Your Homework with AI