麻烦让我的哈希算法工作

时间:2013-04-09 22:50:47

标签: java hashtable

问题已解决(暂时无法接受答案,我改变了我的while循环,现在它可以正常工作,我在下面为任何未来的观众做了回答):我的哈希algortihm让我头疼。我从一个文件中读取键的输入和少量的键,我的代码工作,一旦我把它变成300个单词,我就有问题了。散列函数位于底部,而while循环位于我的main函数体中,它是用java编写的。散列函数运行良好,直到我愚蠢地改变了主体并丢失了原始代码。我想我已经解决了溢出问题,但是非常感谢任何帮助,谢谢!

如何计算tSize:

//Calcking tSize
tSize = (int)(items*tSizeFactor);

//Making tSize prime
while(!isPrime((int)tSize))
    tSize++;

当我从文件中读取时循环:

while(line != null) {
                //Getting the address to place the value in
                position = hash(line.toCharArray(), (int)tSize);

                //If there is something there we enter the if statement
                if(hashTable[position][0] != null) {
                    //while we haven't found a spot and i < tableSize we update the last position we were at and move through the array
                    for(int i = 1; i < (int)tSize && hashTable[position][0] != null; i++) {
                        //prevPosition is used to update the link in the spot just before our final destination, allows wrap around in the array
                        prevPosition = position;
                        //we add +i to the original position and modulo the table size allowing wrap around in the array
                        position = (position+i)%(int)tSize;
                    }
                    //finally when we found a spot we update the previous position to link to the new item
                    hashTable[prevPosition][1] = Integer.toString(position);
                }

                //Adding the values to the hash table and setting the link to -1
                hashTable[position][0] = new String(line);
                hashTable[position][1] = new String(Integer.toString(-1));

                line = reader.readLine();
            }

public static int hash(char ch[],final int TSIZE) {
        int sum = 7;
        for(int i = 0; i < ch.length; i++) {
            sum = sum*31+ch[i];
            sum <<= 3;
        }

        if(sum < 0)
            sum *= -1;

        return sum%TSIZE;
    }

3 个答案:

答案 0 :(得分:0)

  1. new String(line)没用。

  2. for(int i = 1; i&lt;(int)tSize&amp;&amp; hashTable [position] [0]!= null; i ++)

    位置0未使用?

  3. prevPosition可以是if(hashTable[position][0] != null) {阻止的本地。

  4. 将Prev位置存储到哈希表看起来没用。

    <强> UPD

  5. position =(position + i)%(int)tSize;

  6. 试试这个 http://en.wikipedia.org/wiki/Quadratic_probing

答案 1 :(得分:0)

首先,一个问题,为什么不使用内置哈希表?

通过阅读你的代码,我发现了一些问题。它们可能无法更正,因为您当前的代码无法了解更多信息。例如tSize

  • 你有一个tSize,我认为它是哈希表的容量。但是当你增加它时我没有看到。这将是问题,至少是性能问题。但在你的实施中,它是功能问题。例如,如果您的tSize为100.那么您的哈希表中最多可以包含100个元素。

  • 看看这个循环:

    for(int i = 1; i < (int)tSize && hashTable[position][0] != null; i++) {
        prevPosition = position;
        position = (hash(line.toCharArray(), (int)tSize)+i)%((int)tSize);
    }
    

这将在发生冲突时执行。(我不明白,为什么你再次调用哈希函数。你可以循环找到一个空闲槽。)你想保留原始的key,让它引用自由职位。但是,如果情况更糟,再次调用哈希函数后,新的position仍然被占用(再次发生碰撞),则会覆盖prePosition,以便丢失原始key。从哈希表中获取数据时会出现问题。

  • 由于以上两点。当您的哈希表已满时,您的代码只会覆盖最后一个元素。

答案 2 :(得分:0)

我改变了while循环解决了问题:

while(line != null) {
                    //Getting the address to place the value in
                    position = hash(line.toCharArray(), (int)tSize);

                    //If there is something there we enter the if statement
                    if(hashTable[position][0] != null) {

                        //Go to the end of the chain
                        while(hashTable[position][1].compareTo("-1") != 0)
                            position = Integer.parseInt(hashTable[position][1]);

                        //Save the position of the end of the chain
                        prevPosition = position;

                        //while we haven't found a spot and i < tableSize we update the last position we were at and move through the array
                        for(int i = 1; i < (int)tSize && hashTable[position][0] != null; i++) {
                            //we add +i to the original position and modulo the table size allowing wrap around in the array
                            position = (position+i)%(int)tSize;
                            System.out.println("Position: " + position);
                        }

                        //finally when we found a spot we update the previous position to link to the new item
                        hashTable[prevPosition][1] = Integer.toString(position);
                    }

                    //Adding the values to the hash table and setting the link to -1
                    hashTable[position][0] = new String(line);
                    hashTable[position][1] = new String(Integer.toString(-1));

                    line = reader.readLine();
                }