Question

C++ question: Using a standard dictionary, and a table size that approximates a load factor of...

C++ question:

Using a standard dictionary, and a table size that approximates a load factor of 1,
compare the number of collisions produced by the hash function in Figure 5.4 and
the hash function in Figure 5.55. (Please program in C++ with explanations)

/** Figure 5.4
* A hash routine for string objects.
*/
unsigned int hash( const string & key, int tableSize )
{
unsigned int hashVal = 0;

for( char ch : key )
hashVal = 37 * hashVal + ch;

return hashVal % tableSize;
}

/** Figure 5.55
* FNV-1a hash routine for string objects.
*/
unsigned int hash( const string & key, int tableSize )
{
unsigned int hashVal = 2166136261;

for( char ch : key )
hashVal = ( hashVal ˆ ch )* 16777619;

return hashVal % tableSize;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<cstdio>
  5. using namespace std;
  6. const int TABLE_SIZE = 128;
  7.  
  8. /*
  9.  * HashEntry Class Declaration
  10.  */
  11. class HashEntry
  12. {
  13.     public:
  14.         int key;
  15.         int value;
  16.         HashEntry(int key, int value)
  17.         {
  18.             this->key = key;
  19.             this->value = value;
  20.         }
  21. };
  22.  
  23. /*
  24.  * HashMap Class Declaration
  25.  */
  26. class HashMap
  27. {
  28.     private:
  29.         HashEntry **table;
  30.     public:   
  31.         HashMap()
  32.         {
  33.             table = new HashEntry * [TABLE_SIZE];
  34.             for (int i = 0; i< TABLE_SIZE; i++)
  35.             {
  36.                 table[i] = NULL;
  37.             }
  38.         }
  39.         /*
  40.          * Hash Function
  41.          */
  42.         int HashFunc(int key)
  43.         {
  44.             return key % TABLE_SIZE;
  45.         }
  46.         /*
  47.          * Insert Element at a key
  48.          */
  49.         void Insert(int key, int value)
  50.         {
  51.             int hash = HashFunc(key);
  52.             while (table[hash] != NULL && table[hash]->key != key)
  53.             {
  54.                 hash = HashFunc(hash + 1);
  55.             }
  56.             if (table[hash] != NULL)
  57.                 delete table[hash];
  58.             table[hash] = new HashEntry(key, value);
  59.         }
  60.         /*
  61.          * Search Element at a key
  62.          */
  63.         int Search(int key)
  64.         {
  65.             int  hash = HashFunc(key);
  66.             while (table[hash] != NULL && table[hash]->key != key)
  67.             {
  68.                 hash = HashFunc(hash + 1);
  69.             }
  70.             if (table[hash] == NULL)
  71.                 return -1;
  72.             else
  73.                 return table[hash]->value;
  74.         }
  75.  
  76.         /*
  77.          * Remove Element at a key
  78.          */
  79.         void Remove(int key)
  80.         {
  81.             int hash = HashFunc(key);
  82.             while (table[hash] != NULL)
  83.             {
  84.                 if (table[hash]->key == key)
  85.                     break;
  86.                 hash = HashFunc(hash + 1);
  87.             }
  88.             if (table[hash] == NULL)
  89.             {
  90.                 cout<<"No Element found at key "<<key<<endl;
  91.                 return;
  92.             }
  93.             else
  94.             {
  95.                 delete table[hash];
  96.             }
  97.             cout<<"Element Deleted"<<endl;
  98.         }
  99.         ~HashMap()
  100.         {
  101.             for (int i = 0; i < TABLE_SIZE; i++)
  102.             {
  103.                 if (table[i] != NULL)
  104.                     delete table[i];
  105.                 delete[] table;
  106.             }
  107.         }
  108. };
  109. /*
  110.  * Main Contains Menu
  111.  */
  112. int main()
  113. {
  114.     HashMap hash;
  115.     int key, value;
  116.     int choice;
  117.     while (1)
  118.     {
  119.         cout<<"\n----------------------"<<endl;
  120.         cout<<"Operations on Hash Table"<<endl;
  121.         cout<<"\n----------------------"<<endl;
  122.         cout<<"1.Insert element into the table"<<endl;
  123.         cout<<"2.Search element from the key"<<endl;
  124.         cout<<"3.Delete element at a key"<<endl;
  125.         cout<<"4.Exit"<<endl;
  126.         cout<<"Enter your choice: ";
  127.         cin>>choice;
  128.         switch(choice)
  129.         {
  130.         case 1:
  131.             cout<<"Enter element to be inserted: ";
  132.             cin>>value;
  133.             cout<<"Enter key at which element to be inserted: ";
  134.             cin>>key;
  135.             hash.Insert(key, value);
  136.             break;
  137.         case 2:
  138.             cout<<"Enter key of the element to be searched: ";
  139.             cin>>key;
  140.             if (hash.Search(key) == -1)
  141.             {
  142.                 cout<<"No element found at key "<<key<<endl;
  143.                 continue;
  144.             }
  145.             else
  146.             {
  147.                 cout<<"Element at key "<<key<<" : ";
  148.                 cout<<hash.Search(key)<<endl;
  149.             }
  150.             break;
  151.         case 3:
  152.             cout<<"Enter key of the element to be deleted: ";
  153.             cin>>key;
  154.             hash.Remove(key);
  155.             break;
  156.         case 4:
  157.             exit(1);
  158.         default:
  159.            cout<<"\nEnter correct option\n";
  160.        }
  161.     }
  162.     return 0;
  163. }
Add a comment
Know the answer?
Add Answer to:
C++ question: Using a standard dictionary, and a table size that approximates a load factor of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. Hashing and Hash Tables. You need to use the ASCII table in the last page for this question. Study the following hash functions for ASCII C strings that are at least 3-char long unsigned hash1(con...

    4. Hashing and Hash Tables. You need to use the ASCII table in the last page for this question. Study the following hash functions for ASCII C strings that are at least 3-char long unsigned hash1(const char, unsigned unsigned vto]+01997 return (v % m); unsigned hash2Cconst char unsigned) unsigned v-o]k(2] 877 return 1 + (v % ( -1)); (a) Given that m-, 7, compute the hash values and fill the following table (3%) String k hash1k, ) hash2(k, 7) aph...

  • In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits&gt...

    In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits> #include <iostream> #include <string> // atoi #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ const unsigned int DEFAULT_SIZE = 179; // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() {...

  • Following class is only part of my program that uses a hash table to simulate a...

    Following class is only part of my program that uses a hash table to simulate a market's client database, client class is my main class which asks user to input client names and then creates a hash table which then users can look up client names. wasn't able to upload everything as it is too much code, but what I need is to modify my client class instead of me inputting data line by line, the program should read from...

  • Java using data structures The objective is to create your own Hash Table class to hold...

    Java using data structures The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in. Employees.txt: (No WhiteSpace...

  • 1 Overview For this assignment you are required to write a Java program that plays (n,...

    1 Overview For this assignment you are required to write a Java program that plays (n, k)-tic-tac-toe; (n, k)-tic- tac-toe is played on a board of size n x n and to win the game a player needs to put k symbols on adjacent positions of the same row, column, or diagonal. The program will play against a human opponent. You will be given code for displaying the gameboard on the screen. 2 The Algorithm for Playing (n, k)-Tic-Tac-Toe The...

  • Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of...

    Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of bitmap (in bits) to be used */ int k, /* chunk length to be matched */ const char *qs, /* query docoument (X)*/ int m, /* query document length */ const char *ts, /* to-be-matched document (Y) */ int n /* to-be-matched document length*/) { /*if the to-be-matched document is less than k length, return false*/ if (n < k) return 0; /*start our...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • QUIZ 9 QUESTION 1 An enumeration contains enumerators that represent a. the functions for the enumeration...

    QUIZ 9 QUESTION 1 An enumeration contains enumerators that represent a. the functions for the enumeration b. the constants for the enumeration c. the operators for the enumeration d. the variables for the enumeration 1 points    QUESTION 2 Which of the following problems is not a problem with using unscoped enumerations? a. The code for using them is more complex. b. They allow you to accidentally convert an enumerator to an integer type. c. They can lead to name...

  • My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

    My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT