C++ You will write the code to resize a hash table. Modify the code below. Increase the hash table size from 10 to 50. Write a function to calculate the greatest number of key-value pairs in any bucket in the hash table. Write a function to resize the hash table. Calculate the size of the resized hash table by selecting the next prime number that is greater than or equal to the maximum number of buckets in the original hash table multiplied by 2. (next prime number ≥ (N * 2)). Resize when any list in any bucket contains more than four key-value pairs. Update the control function to randomly generate 200 key-value pairs and add them to the hash table. If any bucket contains more than four key-value pairs, resize the hash table. Print the hash table before it is resized and after it is resized. using namespace std; class HashTable{ private: static int const BUCKET_CNT = 10; list> bucket[BUCKET_CNT]; public: bool isEmpty(); int hashFunction(int key); void InsertItem(int key, string value); void RemoveItem(int key); void PrintTable(); }; bool HashTable::isEmpty() { int sum{}; for (int i{}; i < BUCKET_CNT; i++) { sum += bucket[i].size(); } if (!sum) { return true; } return false; } int HashTable::hashFunction(int key) { return key % BUCKET_CNT; } void HashTable::InsertItem(int key, string value) { int hashValue = hashFunction(key); auto& buList = bucket[hashValue]; auto buItr = begin(buList); bool keyExists = false; for (; buItr != end(buList); buItr++) { if (buItr->first == key) { keyExists = true; buItr->second = value; cout << "[WARNING] Key exists. Value replaced." << endl; break; } } if (!keyExists) { buList.emplace_back(key, value); } return; } void HashTable::RemoveItem(int key) { int hashValue = hashFunction(key); auto& buList = bucket[hashValue]; auto buItr = begin(buList); bool keyExists = false; for (; buItr != end(buList); buItr++) { if (buItr->first == key) { keyExists = true; buItr = buList.erase(buItr); break; } } if (!keyExists) { cout << "[WARNING] Key not found. Pair not removed." << endl; } return; } void HashTable::PrintTable() { for (int i{}; i < BUCKET_CNT; i++) { if (bucket[i].size() == 0) continue; auto buItr = bucket[i].begin(); for (; buItr != bucket[i].end(); buItr++) { cout << "[INFO] Bucket: " << i; cout << " Key: " << buItr->first; cout << " Value: " << buItr->second << endl; } } return; } int main() { HashTable HT; HT.InsertItem(101, "Dennis"); HT.InsertItem(201, "Wendy"); HT.InsertItem(102, "Travis"); HT.InsertItem(202, "Lexi"); HT.InsertItem(302, "Addie"); HT.InsertItem(402, "Jaxon"); HT.InsertItem(103, "Scott"); HT.InsertItem(203, "Emily"); HT.InsertItem(303, "Owen"); HT.InsertItem(403, "Greg"); HT.InsertItem(104, "Tyler"); HT.InsertItem(204, "Jasmine"); HT.PrintTable(); HT.RemoveItem(204); HT.RemoveItem(100); HT.PrintTable(); return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

C++

You will write the code to resize a hash table. Modify the code below.

  1. Increase the hash table size from 10 to 50.
  2. Write a function to calculate the greatest number of key-value pairs in any bucket in the hash table.
  3. Write a function to resize the hash table. Calculate the size of the resized hash table by selecting the next prime number that is greater than or equal to the maximum number of buckets in the original hash table multiplied by 2. (next prime number ≥ (N * 2)).
  4. Resize when any list in any bucket contains more than four key-value pairs.
  5. Update the control function to randomly generate 200 key-value pairs and add them to the hash table. If any bucket contains more than four key-value pairs, resize the hash table. Print the hash table before it is resized and after it is resized.

using namespace std;

class HashTable{ private: static int const BUCKET_CNT = 10; list<pair<int, string>> bucket[BUCKET_CNT]; public: bool isEmpty(); int hashFunction(int key); void InsertItem(int key, string value); void RemoveItem(int key); void PrintTable(); }; bool HashTable::isEmpty() { int sum{}; for (int i{}; i < BUCKET_CNT; i++) { sum += bucket[i].size(); } if (!sum) { return true; } return false; } int HashTable::hashFunction(int key) { return key % BUCKET_CNT; } void HashTable::InsertItem(int key, string value) { int hashValue = hashFunction(key); auto& buList = bucket[hashValue]; auto buItr = begin(buList); bool keyExists = false; for (; buItr != end(buList); buItr++) { if (buItr->first == key) { keyExists = true; buItr->second = value; cout << "[WARNING] Key exists. Value replaced." << endl; break; } } if (!keyExists) { buList.emplace_back(key, value); } return; } void HashTable::RemoveItem(int key) { int hashValue = hashFunction(key); auto& buList = bucket[hashValue]; auto buItr = begin(buList); bool keyExists = false; for (; buItr != end(buList); buItr++) { if (buItr->first == key) { keyExists = true; buItr = buList.erase(buItr); break; } } if (!keyExists) { cout << "[WARNING] Key not found. Pair not removed." << endl; } return; } void HashTable::PrintTable() { for (int i{}; i < BUCKET_CNT; i++) { if (bucket[i].size() == 0) continue; auto buItr = bucket[i].begin(); for (; buItr != bucket[i].end(); buItr++) { cout << "[INFO] Bucket: " << i; cout << " Key: " << buItr->first; cout << " Value: " << buItr->second << endl; } } return; } int main() { HashTable HT; HT.InsertItem(101, "Dennis"); HT.InsertItem(201, "Wendy"); HT.InsertItem(102, "Travis"); HT.InsertItem(202, "Lexi"); HT.InsertItem(302, "Addie"); HT.InsertItem(402, "Jaxon"); HT.InsertItem(103, "Scott"); HT.InsertItem(203, "Emily"); HT.InsertItem(303, "Owen"); HT.InsertItem(403, "Greg"); HT.InsertItem(104, "Tyler"); HT.InsertItem(204, "Jasmine"); HT.PrintTable(); HT.RemoveItem(204); HT.RemoveItem(100); HT.PrintTable(); return 0; }

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Hash Table
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education