Question

In: Computer Science

Design a hashtable with fundamental operations by using two types of hashing and collision avoidance. (a)...

Design a hashtable with fundamental operations by using two types of hashing and collision avoidance. (a) Design two types of hashing, the first is a simple hash that can be completed with O(1), but it may cause collisions; the second hash is a double hash can be completed with O(1). Put the details of your hash function and analysis in codes comments. (b) When your first hash function has collisions, using separate chaining to solve the collision.

Solutions

Expert Solution

CODE:-

#include <bits/stdc++.h>
using namespace std;
class singlehash{
//Key is added in the Hash table
//Index to which a key is stored is decide via hash function
//defined in line index = hash(key ) => index = key % Size of table
//if collision is detected Seperate chaining method is used
//In this method a node is appended to the desired index
//Time Complexity is O(1) for adding key to the table
//Space Complexity is O(N) in worst case with all values chained to same index


private:
int N;
vector< vector< int> > h; // This will store hash table entries in form of 2d vector

public:
singlehash(int N){ //hash table constructor with particular size N
h= vector< vector < int > >(N);
this->N=N;
}
int getIndex(int key){ //Hash function
return key % N;
}
void add(int key){
if(h[getIndex(key)].size()){
cout<<"Collision is there for "<< key <<" while adding key using Separate chaining \n";
}
h[getIndex(key)].push_back(key);
}
void displayHashTable(){
for(int i=0;i<h.size();i++){
cout<<i; //Index
for(int j=0;j<h[i].size();j++){
cout<<" => "<<h[i][j]; //value
}
cout<<"\n";
}
}

};
class doublehash{
//Key is added in the Hash table
//Index to which a key is stored is decide via hash function
//defined in line index = hash(key ) => hash1(key) + i*hash2(key) where i is the iteration 0,1,2,.. N
//If Collision is detected index is recalculated for next i value till size of N in worst case
//Time Complexity is O(1) is Average case and O(N) where N is size of table and index is recalculated till it find an empty spot
//Space complexity is O(1) even with worst case.
private:
int N;
vector< int > h; // This will store hash table entries in form of 1d vector

public:
doublehash(int N){ //hash table constructor with particular size N
h= vector< int>(N,-1);
this->N=N;
}
int getIndex(int key){ //First Hash function
return key % N;
}
int getIndex2(int key){ // Second Hash Function
return 3- (key % 3);
}
void add(int key){ //Adding key into Hash table
int target= getIndex(key); //Calculating Hash for target index
if(h[target]!=-1){ //Collision
int i=1;
while(1){
cout<<"Collision is there for "<< key << " while adding going for next iteration in double hashing \n";
int next= (target + i * getIndex2(key)) % N;
if(h[next]==-1){
h[next]=key; //adding entry
break;
}
i++;
}
}
else{
h[target]= key; //adding entry
}
}
void displayHashTable(){
for(int i=0;i<h.size();i++){
cout<<i; //Index
cout<<" => "<<h[i]; //value
cout<<"\n";
}
}

};
int main() {
//Program Starts here
int keys[] = { 2, 7, 16, 9 , 27, 11 }; //keys stored in an array
int n=6; //No of keys
singlehash sh =singlehash(6); //creating a Single Hash table
for(int i=0;i<n;i++){
sh.add(keys[i]);
}
cout<<"Display Single Hash table \n";
sh.displayHashTable(); //display Single Hash

doublehash dh =doublehash(6); //Creating a double hash table
for(int i=0;i<n;i++){
dh.add(keys[i]);
}
cout<<"Display Double Hash table \n";
dh.displayHashTable(); //display Double Hash

}

OUTPUT:-


Related Solutions

Two fundamental types of cells are known to exist in nature:
Part B - Comparing eukaryotic and prokaryotic cells  Two fundamental types of cells are known to exist in nature: prokaryotic cells and eukaryotic cells (like the one shown in the Tour of an Animal Cell animation). Both prokaryotic and eukaryotic cells carry out all of the processes necessary for life, but they differ in some important ways. In this activity, you will identify which cell structures are found only in prokaryotic cells, only in eukaryotic cells, or in both types...
compare and contrast two fundamental security design principles. Analyze how these principles and how they impact...
compare and contrast two fundamental security design principles. Analyze how these principles and how they impact an organizations security posture.
There are two major design types for operating system kernels: monolithic kernels and microkernels. Which design...
There are two major design types for operating system kernels: monolithic kernels and microkernels. Which design better satisfies the following requirements, monolithic kernel, microkernels, or both? Justify your answers. 1. Convenient access to operating system data structures by the kernel-level process. 2. Adding/modifying operating system components by kernel developers. 3. Strong security and reliability.
how would you design a operations system fir an athletic apparel manufacturer using the approach if...
how would you design a operations system fir an athletic apparel manufacturer using the approach if globalization,utilizing forecasting, creating a culture and structuring human resource sytems to achieve culture, developing a sustainable supply chain for the company, utilizing planning and methodology and using the lean operation to reduce waste and improve productivity
Describe the principles and operations of four types of concentrating solar power (CSP) technologies using a...
Describe the principles and operations of four types of concentrating solar power (CSP) technologies using a systems diagram and discuss any potential environmental implications of concentrating solar power systems. Please include appropriate references to support your argument?
Two types of flow regime are normally used in the design of an activated sludge process,...
Two types of flow regime are normally used in the design of an activated sludge process, namely completely mixed and plug flow (both are continuous). Using first order kinetic reaction, demonstrate which flow regime provide a better performance, given the followings: Q = 550 m3/d t = 6 hours Co = 225 mg/L k = 0.15 d-1
Competitive markets generate a Pareto efficient outcome. Illustrate and explain, using the two fundamental theorems of...
Competitive markets generate a Pareto efficient outcome. Illustrate and explain, using the two fundamental theorems of welfare economics.
Identify the two general types of operations. What are theircharacteristics? Find a company to represent...
Identify the two general types of operations. What are their characteristics? Find a company to represent each type.
Techstreet.com is a small web design business that provides services for two main types of websites:...
Techstreet.com is a small web design business that provides services for two main types of websites: brochure sites and e-commerce sites. One package involves an up-front payment of $90,000 and monthly payments of 1.4¢ per “hit.” Kathy Cutler has a new eBay franchise and is considering the e-commerce package. She expects to have at least 6000 hits per month, and hopes that 1.5% of the hits will result in a sale. If the average income from sales (after fees and...
For energy engineers; Review the mechanical design features materials, construction and so on of two types...
For energy engineers; Review the mechanical design features materials, construction and so on of two types of energy storage system. please talk about it deeply like an article. much explanation needed.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT