0

我正在使用线性探测制作一个哈希表,当负载因子即(在哈希表中输入的元素数量)/(哈希表的大小)变得大于 0.5 时,我必须调整数组的大小,我必须调整数组的大小。我正在通过在包含与哈希表相关的函数的类中初始化一个指针来调整大小。我将指针设置为等于大小为 100 的结构(结构仅包含一个字符串)的数组。每次加载因子大于 0.5 ,我通过创建一个两倍于先前大小的新数组并将指针指向新数组来调整数组的大小。我还有一个 int,它存储数组的当前大小,并随着每个使用 resize 函数的实例而更新。每次调用插入函数时,插入的元素数量都会增加。我这样做正确吗?下面是我的代码

#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;
}
4

3 回答 3

3

您可能会在某处发现它有帮助: http ://www.cs.rmit.edu.au/online/blackboard/chapter/05/documents/contribute/chapter/05/linear-probing.html

于 2013-04-18T11:48:42.507 回答
0

看起来负载因子的计算方式是int/int,它会保持 0 直到达到 1。尝试将除法的输入转换为浮点数。

于 2013-04-03T15:36:45.030 回答
0

您正在处理大数字并且变量“key”溢出:

key += (int)power(a,i)*b
于 2013-04-03T15:21:03.587 回答