fixed code fir linear_probing....

This commit is contained in:
Krishna Vedala 2020-07-23 18:16:56 -04:00
parent abfe9ad081
commit d955a49de9
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -1,24 +1,30 @@
// Copyright 2019
#include <stdlib.h>
#include <functional>
/**
* @file
* @author [achance6](https://github.com/achance6)
* @author [Krishna Vedala](https://github.com/kvedala)
* @brief
*/
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
namespace { // keep the code local to this file by assigning them to an unnamed
// namespace
// fwd declarations
struct Entry;
using Entry = struct Entry;
bool putProber(Entry entry, int key);
bool searchingProber(Entry entry, int key);
void add(int key);
// globals
int notPresent;
struct Entry* table;
std::vector<Entry> table;
int totalSize;
int tomb = -1;
int size;
@ -31,18 +37,18 @@ struct Entry {
};
// Hash a key
int hashFxn(int key) {
size_t hashFxn(int key) {
std::hash<int> hash;
return hash(key);
}
// Performs linear probing to resolve collisions
int linearProbe(int key, bool searching) {
int hash = static_cast<int>(fabs(hashFxn(key)));
int hash = static_cast<int>(hashFxn(key));
int i = 0;
Entry entry;
do {
int index = static_cast<int>(fabs((hash + i) % totalSize));
int index = static_cast<int>((hash + i) % totalSize);
entry = table[index];
if (searching) {
if (entry.key == notPresent) {
@ -56,12 +62,14 @@ int linearProbe(int key, bool searching) {
i++;
} else {
if (putProber(entry, key)) {
if (!rehashing)
if (!rehashing) {
cout << "Spot found!" << endl;
}
return index;
}
if (!rehashing)
if (!rehashing) {
cout << "Spot taken, looking at next" << endl;
}
i++;
}
if (i == totalSize) {
@ -82,8 +90,9 @@ bool putProber(Entry entry, int key) {
// Looks for a matching key
bool searchingProber(Entry entry, int key) {
if (entry.key == key)
if (entry.key == key) {
return true;
}
return false;
}
@ -108,27 +117,26 @@ void rehash() {
// Necessary so wall of add info isn't printed all at once
rehashing = true;
int oldSize = totalSize;
Entry* oldTable = table;
// Really this should use the next prime number greater than totalSize * 2
table = new Entry[totalSize * 2];
std::vector<Entry> oldTable(table);
// Really this should use the next prime number greater than totalSize *
// 2
totalSize *= 2;
table = std::vector<Entry>(totalSize);
for (int i = 0; i < oldSize; i++) {
if (oldTable[i].key != -1 && oldTable[i].key != notPresent) {
size--; // Size stays the same (add increments size)
add(oldTable[i].key);
}
}
delete[] oldTable;
// delete[] oldTable;
rehashing = false;
cout << "Table was rehashed, new size is: " << totalSize << endl;
}
// Adds entry using linear probing. Checks for load factor here
void add(int key) {
Entry* entry = new Entry();
entry->key = key;
int index = linearProbe(key, false);
table[index] = *entry;
table[index].key = key;
// Load factor greater than 0.5 causes resizing
if (++size / static_cast<double>(totalSize) >= 0.5) {
rehash();
@ -152,7 +160,7 @@ void addInfo(int key) {
display();
cout << endl;
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize
<< " == " << fabs(hashFxn(key) % totalSize);
<< " == " << hashFxn(key) % totalSize;
cout << endl;
add(key);
cout << "New table: ";
@ -171,13 +179,14 @@ void removalInfo(int key) {
cout << "New table: ";
display();
}
} // namespace
// I/O
int main(void) {
int cmd, hash, key;
int main() {
int cmd = 0, hash = 0, key = 0;
cout << "Enter the initial size of Hash Table. = ";
cin >> totalSize;
table = new Entry[totalSize];
table = std::vector<Entry>(totalSize);
bool loop = true;
while (loop) {
system("pause");
@ -213,7 +222,7 @@ int main(void) {
case 4:
cout << "Enter element to generate hash = ";
cin >> key;
cout << "Hash of " << key << " is = " << fabs(hashFxn(key));
cout << "Hash of " << key << " is = " << hashFxn(key);
break;
case 5:
display();
@ -221,7 +230,7 @@ int main(void) {
default:
loop = false;
break;
delete[] table;
// delete[] table;
}
cout << endl;
}