【问题标题】:Hash table(linear probing)哈希表(线性探测)
【发布时间】:2013-03-25 08:16:20
【问题描述】:

我正在使用线性探测制作一个哈希表,当负载因子即(输入哈希表的元素数)/(哈希表的大小)大于 0.5 时,我必须调整数组的大小,我必须调整大小数组。我通过在包含与哈希表相关的函数的类中初始化一个指针来调整大小。我将指针等于大小为 100 的结构数组(结构仅包含一个字符串)。每次加载因子变为大于 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;
}

【问题讨论】:

  • 要说服自己它正在工作(或不工作),请构建一套单元测试。
  • 当我在单词文件上运行此代码时,我有时会得到 key 的负值。我不明白为什么会发生这种情况,因为我没有减去任何东西,我将每个字母都转换为无符号字符首先在计算它的价值之前。
  • 由于负键,程序报错。

标签: c++


【解决方案1】:

【讨论】:

  • 虽然理论上这可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
【解决方案2】:

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

key += (int)power(a,i)*b

【讨论】:

  • 我给出了肯定的意见。我会附上主文件
  • 我不明白你想说什么??
  • 我只想要一个随机值,所以如果我给它任何值也没关系。
  • 是的,我刚刚显示了 key 的值,这正是发生的事情。但是现在在调整数组大小后程序会立即卡住
  • 我没有足够的声誉。只是一个新手。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-17
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    相关资源
    最近更新 更多