In C++ I am trying to implement a log base 2 hash table utilizing the shift function. So, the code determines the position of where the value will be stored using log2. But am having issues using the shift part as everything just wants to pile on to zero.
//ChainingHash - log2
#include<iostream>
#include<vector>
#include<iterator>
#include<string>
#include<cmath>
using namespace std;
#define BUCKET 10 //no. of buckets
class Hash
{
vector<int>*table; //ptr containing
buckets
public:
Hash();
void insert(int);
//void append();
void search(int);
void remove(int);
void print();
int code(int);
};
Hash::Hash()
{
table = new vector<int>[BUCKET];
}
void Hash::insert(int key)
{
int index = code(key);
table[index].push_back(key);
}
void Hash::search(int key)
{
int index = code(key);
vector<int> :: iterator i;
for(i=table[index].begin(); i!=table[index].end();
i++)
{
if(*i == key)
{
cout <<
key << " was found.\n";
return;
}
}
cout << key << " was not found in the
list.\n";
}
void Hash::remove(int key)
{
int index = code(key);
vector<int> :: iterator i;
for(i=table[index].begin(); i!=table[index].end();
i++)
{
if(*i == key)
{
table[index].erase(i);
return;
}
}
cout << "Could not find value " << key
<< " to delete.\n";
}
void Hash::print()
{
for (int i=0; i<BUCKET; i++)
{
cout << i;
for(auto x : table[i])
cout << "
--> " << x;
cout << endl;
}
}
int Hash::code(int key) //if key = 31
{
int index = pow(key,2); //961
int numBits = ceil(log2(index)+1); //11
int R = ceil(log2(BUCKET)); //4
int lBits = ceil(((pow(numBits,2))-R)/2); //59
int eBits = index >> lBits; // 961 >> 59,
this equals 0??
eBits = eBits &(0xFFFFFFFF >> (32-R));
//Does not shift, still 0
return eBits%BUCKET; //is zero bc eBits is 0
}
int main()
{
int choice, val=0;
Hash h;
do
{
cout << " -- MENU --
\n";
cout << "1 - insert\t2 -
search\n3 - delete\t4 - print\n\n0 - quit\n";
cin >> choice;
switch(choice)
{
case 1:
//insert
{
while(val>=0)
{
cout << "Enter a number
greater than -1 to insert (to exit insert -1): ";
cin >> val;
if(val!=-1)
h.insert(val);
}
break;
}
case 2:
//search
{
cout << "Enter a number you wish to search
for: ";
cin >> val;
h.search(val);
break;
}
case 3:
//delete
{
cout << "Ender the number you wish to
delete: ";
cin >> val;
h.remove(val);
break;
}
case 4:
//print
cout << "All inserted Values:\n";
h.print();
break;
case 0:
break;
default:
cout << "Invalid Response, try
again.\n";
break;
}
}while(choice !=0);
}
I need the code to not insert every value at zero. Thank you.
I have modified the code function so that correctly shifts bits.
So, value 0 and 1 should hash to bucket 0
Value 2 and 3 should hash to bucket 2
Value 4,5,6,7 should hash to bucket 3
Value 222 as log2(222) = 7.79441586635 should hash to bucket 8.
The modified code function is as follows:
int Hash::code(int key) { int count = 0; bool perfectSquare = true; while (key > 0) { if (key & 1) { perfectSquare = false; /* If the number is perfectly divisible by 2, this code will never reach. */ } key = key >> 1; /* this divides a number by 2 */ count++; } if (perfectSquare) { count--; } return count % BUCKET; }
A sample output screen looks like this.

The entire code is a follows:
//ChainingHash - log2 #include<iostream> #include<vector> #include<iterator> #include<string> #include<cmath> using namespace std; #define BUCKET 10 //no. of buckets class Hash { vector<int>*table; //ptr containing buckets public: Hash(); void insert(int); //void append(); void search(int); void remove(int); void print(); int code(int); }; Hash::Hash() { table = new vector<int>[BUCKET]; } void Hash::insert(int key) { int index = code(key); table[index].push_back(key); } void Hash::search(int key) { int index = code(key); vector<int> ::iterator i; for (i = table[index].begin(); i != table[index].end(); i++) { if (*i == key) { cout << key << " was found.\n"; return; } } cout << key << " was not found in the list.\n"; } void Hash::remove(int key) { int index = code(key); vector<int> ::iterator i; for (i = table[index].begin(); i != table[index].end(); i++) { if (*i == key) { table[index].erase(i); return; } } cout << "Could not find value " << key << " to delete.\n"; } void Hash::print() { for (int i = 0; i < BUCKET; i++) { cout << i; for (auto x : table[i]) cout << " --> " << x; cout << endl; } } int Hash::code(int key) { int count = 0; bool perfectSquare = true; while (key > 0) { if (key & 1) { perfectSquare = false; /* If the number is perfectly divisible by 2, this code will never reach. */ } key = key >> 1; /* this divides a number by 2 */ count++; } if (perfectSquare) { count--; } return count % BUCKET; } int main() { int choice, val = 0; Hash h; do { cout << " -- MENU -- \n"; cout << "1 - insert\t2 - search\n3 - delete\t4 - print\n\n0 - quit\n"; cin >> choice; switch (choice) { case 1: //insert { while (val >= 0) { cout << "Enter a number greater than -1 to insert (to exit insert -1): "; cin >> val; if (val != -1) h.insert(val); } break; } case 2: //search { cout << "Enter a number you wish to search for: "; cin >> val; h.search(val); break; } case 3: //delete { cout << "Ender the number you wish to delete: "; cin >> val; h.remove(val); break; } case 4: //print cout << "All inserted Values:\n"; h.print(); break; case 0: break; default: cout << "Invalid Response, try again.\n"; break; } } while (choice != 0); }
In C++ I am trying to implement a log base 2 hash table utilizing the shift...
Type up and get the Hash table on pages 19 - 22 to work. Show
your sample test run at the bottom of your code using a multiline
comment
I typed everything but the code is not working please help me
fix the mistakes.
These are pages 19-22.
The code I have:
#include <iostream>
#inlcude <iomanip>
#include <stack>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
//////////////////////////////HASH
TABLE///////////////////////////////////////////////
//hash.cpp
//demonstrate hash table with linear probing
///////////////////////////////////////////////////////////////////////////////////////
class DataItem
{...
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...
Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...
Implement an eager delete() method for linear probing hash
table. The eager delete will delete the pair from the hash table,
not just set the value to null.
*Algorithhms*
In Java Language Please
I want to add the delete method to this hash table
code.
public class Linear ProbingHashST<Key, Value> private int M = 30001; private Value [] vals = (Value[]) new Object[M]: private Key [] keys = (Key []) new Object[M]: array doubling and halving code omitted private int...
Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function has already been implemented for you. // hash.CPP program to implement hashing with chaining #include<iostream> #include "hash.hpp" using namespace std; node* HashTable::createNode(int key, node* next) { node* nw = new node; nw->key = key; nw->next = next; return nw; } HashTable::HashTable(int bsize) { this->tableSize= bsize; table = new node*[tableSize]; for(int i=0;i<bsize;i++) table[i] = nullptr; } //function to calculate hash function unsigned int HashTable::hashFunction(int key)...
Identify the letters and anything associated with it. It is a hash map so please feel free to point out the other important parts beside the arrows. I apologize for the order of the pictures class HashMap private: HashEntry table ; public: HashMap() table-new HashEntry * [TABLE_SIZE]; for (int 1-0; 1< TABLE SIZE: i++) table [i] = NULL; Hash Function int HashFunc(int key) return key % TABLE-SIZE; Insert Element at a key void Insert(int key, int value) int hashHashFunc(key); while...
This is the incomplete example from class last week, where we started implementing a hash table as a vector of queues. In order for this example to work, the Queue struct needs 3 more functions to be implemented. A find function, which tells us it a given value appears in the queue, without being destructive. A function called print, which prints out the contents of the queue, again without being destructive. Finally, there is a need for a destructor. To...
PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and use the multiplication method to generate hash keys. You may use your own linked list code from a previous assignment or you can use a standard sequence container. Partial definitions for the hash table class and a generic data class are provided (C++ and Java versions). You may use them as a starting point if you wish. If you choose to design your own...
Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...
C programming Problem 3 [Set via Hashing] As mentioned in the lecture, a hash table can be used to implement a Set ADT. Let's try to use the template below to implement a Set with double hashing. Here we assume the Set contains names with 3 characters long. Since it is a Set, we do not need to have an explicit value for each key. We will use a token value of 1 if a name belongs to the Set....