我正在使用线性探测制作哈希表,并且我必须在加载因子即(哈希表中输入的元素数量)/(哈希表的大小)变得大于0.5时调整数组的大小,我必须调整大小我正在通过在包含与hashtable相关的函数的类中初始化一个指针来调整大小。我将指针放在一个大小为100的struct(struct只包含一个字符串)的数组中。每次加载因子变为大于0.5,我通过创建一个前一个大小加倍的新数组来调整数组的大小,并将指针指向新数组。我还有一个int,它存储数组的当前大小,并使用调整大小函数的每个实例更新随着插入函数的每次调用,插入的元素数量都会增加。我这样做是否正确?下面是我的代码
#include <cstring>
#include <vector>
#include <math.h>
#include <iomanip>
using namespace std;
int power(int a,int b)
{
for (int i=0;i<b;i++)
{
a*=a;
}
return a;
};
struct Bucket
{
string word;
};
const int size=100;
class LProbing
{
private:
int a; //a constant which is used in hashing
int cursize; //current size of hash table
Bucket *Table; //pointer to array of struct
int loadfactor; //ratio of number of elements entered over size of hashtable
int n; //number of elements entered
Bucket table[size]; //array of structs
public:
LProbing(int A); //constant is decided by user
void resize();
void insert(string word);
void Lookup(string word);
};
LProbing::LProbing(int A)
{
cursize=size;
a=A;
Table=table;
loadfactor=0; //initially loadfactor is 0 as number of elements entered are 0
n=0;
}
void LProbing::resize()
{
cout<<"resize"<<endl;
loadfactor=n/cursize; //ensuring if resize needs to be done
if (loadfactor<=0.5)
{
return;
}
const int s=2*cursize;
Bucket PTable[s];
for (int i=0;i<cursize;i++)
{
if (Table[i].word.empty())
continue;
//rehashing the word onto the new array
string w=Table[i].word;
int key=0;
for (int j=0;j<w.size();j++)
{
unsigned char b=(unsigned char)w[j];
key+=(int)power(a,i)*b;
}
key=key%(2*cursize);
PTable[key].word=w; //entering the word in the new array
}
Table=PTable; //putting pointer equal to new array
cursize=2*cursize; //doubling the current size of array
}
void LProbing::insert(string word)
{
cout<<"1"<<endl;
n++; //incrementing the number of elements entered with every call to insert
//if loadfactor is greater than 0.5, resize array
loadfactor=n/cursize;
if (loadfactor>0.5)
{
resize();
}
//hashing the word
int k=0;
for (int i=0;i<word.size();i++)
{
unsigned char b=(unsigned char)word[i];
int c=(int)((power(a,i))*b);
k+=c;
cout<<c<<endl;
}
int key=0;
key=k%cursize;
cout<<key<<endl;
//if the respective key index is empty enter the word in that slot
if (Table[key].word.empty()==1)
{
cout<<"initial empty slot"<<endl;
Table[key].word=word;
}
else //otherwise enter in the next slot
{
//searching array for empty slot
while (Table[key].word.empty()==0)
{
k++;
key=k%cursize;
}
//when empty slot found,entering the word in that bucket
Table[key].word=word;
cout<<"word entered"<<endl;
}
}
#include "Linear Probing.cpp"
#include <fstream>
using namespace std;
int main()
{
LProbing H(35);
ifstream fin;
fin.open("dict.txt");
vector<string> D;
string d;
while (getline(fin,d))
{
if (!d.empty())
{
D.push_back(d);
}
}
fin.close();
for (int i=0;i<D.size();i++)
{
H.insert(D[i]);
}
system("PAUSE");
return 0;
}
答案 0 :(得分:3)
您可能会发现它在某处有用: http://www.cs.rmit.edu.au/online/blackboard/chapter/05/documents/contribute/chapter/05/linear-probing.html
答案 1 :(得分:0)
你正在处理大数字和变量“key”溢出:
key += (int)power(a,i)*b
答案 2 :(得分:0)
看起来loadfactor计算为int/int
所以它将保持为0直到达到1.尝试将分区的输入转换为浮点数。