如何解决线性探测哈希表未插入所有元素
一直试图敲定散列函数并使用线性探测和二次探测实现散列表,但是我遇到了一个问题,即并非所有数据值都被插入。我从一个包含 10,0000 个
在打印 HashTable(或者至少是底部 1000 个节点)时,我注意到有很多 NULL 值,结果使用某个数据集时有 177 个 NULL 值,而使用另一个时大约有 700 个.我真的不确定我哪里出错了,因为我基本上已经查看了带有线性探测的哈希表的每个实现,但它们仍然不能完全工作。
我对 CSV 的解析相当有信心,因为在测试中遍历数组时,我没有看到任何虚假值、空插槽或错误插槽中的值。
我将包括我正在使用的数据样本以及文件,非常感谢任何帮助或提示,因为我似乎无法解决这个问题。
17713,36933,34262,19985,29293,74317,79460,3356,18949,70018,56659,47601,2754,59469,22496,25331,58588,4903,65300,48561,8484,61670,79679,50514,40216,64252,3911,57275,7674,48557,35502,44792,
#ifndef HASHLINEAR_HPP
#define HASHLINEAR_HPP
struct node{
int key;
};
class HashLinear{
node** table;
int tableSize;
int numCollisions = 0;
public:
HashLinear(int bsize);
void insert(int key);
unsigned int hashFunction(int key);
int search(int key);
int getCollisions();
void printTable();
};
#endif
#include "hashlinear.hpp"
#include <iostream>
using namespace std;
HashLinear::HashLinear(int bsize){
this->tableSize = bsize;
table = new node*[tableSize];
for(int i = 0; i < tableSize; i++){
table[i] = NULL;
}
}
int HashLinear::getCollisions(){
return numCollisions;
}
unsigned int HashLinear::hashFunction(int key){
return key % tableSize;
}
// void HashLinear::insert(int key){ //Another method I tried using to insert
// node* newNode = new node;
// newNode->key = key;
// int index = hashFunction(key);
// int func = 1;
// while(table[index] != NULL && table[index]->key != key){
// numCollisions++;
// index = (index + func) % tableSize;
// func++;
// }
// table[index] = newNode;
// }
void HashLinear::insert(int key){
node* newNode = new node;
newNode->key = key;
int index = hashFunction(key);
int func = 1;
if(table[index] == NULL){
table[index] = newNode;
//cout << "Inserted" << endl;
return;
}
else{
numCollisions++;
index = (index + func) % tableSize;
while(table[index] != NULL and table[index]->key != key){
func++;
index = (index + func) % tableSize;
}
if(table[index] == NULL){
table[index] = newNode;
}
}
}
int HashLinear::search(int key){
int value = hashFunction(key);
int num = 0;
while(table[value] != NULL){
num = 0;
if(num++ > tableSize){
break;
}
if(table[value]->key == key){
return value;
}
value++;
value %= tableSize;
}
return -1;
}
void HashLinear::printTable(){
int nullCount = 0;
for(int i = 0; i < tableSize; i++){
cout << i << " || ";
if(table[i] == NULL){
cout << "NULL" << endl;
nullCount++;
}
else{
cout << table[i]->key << endl;
}
}
cout << "Null Count: " << nullCount << endl;
}
#include "hashlinear.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <time.h>
#include <stdlib.h>
#include <chrono>
#include <thread>
#include <vector>
using namespace std;
int main(){
//******Read in data******//
int testData[10000];
float insertTime[100];
float searchTime[100];
int index = 0;
string line,temp,word;
ifstream inputFile;
inputFile.open("dataSetA-updatedhashlinear.csv");
if(inputFile.fail()){
std::cout << "Could not open data." << endl;
return -1;
}
else{
while(inputFile >> temp){
getline(inputFile,temp);
stringstream inStream(temp);
while(getline(inStream,word,',')){
if(word == ""){
index++;
continue;
}
else{
testData[index] = stoi(word);
index++;
}
}
}
inputFile.close();
}
//******Read in data******//
//cout << "Printing random data in range of 0 ~ 10: " << testData[rand() % 10 + 0] << endl;
//******Insert/Search data in Linked List******//
HashLinear table(10009);
int hashIndex = 0;
int insertTimeIndex = 0;
int searchTimeIndex = 0;
int num = 0;
int upperIndex = 99;
while(hashIndex < 10000 && upperIndex < 10000){
//Block for 100 insertions
auto insertionStart = chrono::steady_clock::Now();//Insert time start
for(int i = hashIndex; i < upperIndex; i++){ //Keep track of current index as well as an upper index to control amount of inserts
table.insert(testData[i]);
hashIndex++;
}
auto insertionEnd = chrono::steady_clock::Now();
insertTime[insertTimeIndex] = chrono::duration_cast<chrono::microseconds>(insertionEnd - insertionStart).count() / 100.0;//Insert time end
insertTimeIndex++;
//Block for 100 insertions
//Block for 100 searches
num = 0;
auto searchStart = chrono::steady_clock::Now();//Search time start
while(num < 100){ //Do 100 random searches from 0 index to upperindex
srand((unsigned)time(0));
int searchNode = table.search(testData[rand() % upperIndex + 0]);
num++;
}
auto searchEnd = chrono::steady_clock::Now();
searchTime[searchTimeIndex] = chrono::duration_cast<chrono::microseconds>(searchEnd - searchStart).count() / 100.0;//Search time end
searchTimeIndex++;
//Block for 100 searches
upperIndex += 100;
}
//******Insert/Search data in Linked List******//
//******TESTING******//
//table.printTable();
std::cout << "Search time: " << searchTime[20] << endl;
std::cout << "Insert time: " << insertTime[20] << endl;
std::cout << "Collisons: " << table.getCollisions() << endl;
int testIndex = table.search(34262);
std::cout << "Index of 34262: " << testIndex << endl;
//******TESTING******//
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。