Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;ikey !=-1) { cout<<"key: " <key; cout < "<value; cout <

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%:"<display(); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions

Question

=+Describe an important trade-off you recently faced

Answered: 1 week ago