繁体   English   中英

双散列函数返回错误值

[英]Double hashing function returning wrong value

我正在创建一个双哈希映射,但是插入后删除功能不起作用。 我遵循相同的格式来增加索引,但是它没有达到正确的索引。

class RHHM {
    unsigned int hash2( int key ) {

        return key % (M-1) + 1;

    }

    //Private variables

    hashNode ** map;        //Backing array
    unsigned int M;   //Capacity of array

    //If index that key hashes to is empty, insert. Else, replace value at hashed index.
    int insert( int key, char value ) {

        int f = hash( key );
        int f2 = hash2 ( key );
        int p = 0;
        int h = f + (p * f2) % M;

        while( map[h] != NULL ) {

            if( p == M )
                return -1 * p;

            if( map[h]->key == key ) {
                map[h]->value = value;
                return p;
            }
            else {
                ++p;
                h = f + (p * f2) % M;
            }
        }

        map[h] = new hashNode( key, value );
        return p;
    }

int remove( int key, char &value) {

        int f = hash( key );
        int f2 = hash2 ( key );
        int p = 0;                         //Keeps track of how many indexes have been checked
        int h = f + (p * f2) % M;

        while( map[h] != NULL ) {

            if( p  == M )              //If item is not found in table, return false
                return -1 * p;

            if( key == map[h]->key )        //If key is found, break out of loop
                break;
            else {
                ++p;
                h = f + (p * f2) % M;  //Wrap around array if necessary
            }

        }

        if( map[h] == NULL )                //If end of cluster reached, return false
            return -1 * p;

        value = map[h]->value;              //Stores the value of the item to be deleted
        delete map[h];                      //Delete the item the user specified
        map[h] = NULL;
        ++p;
        h = f + (p * f2) % M;
        for( ; map[h] != NULL; h = f + (p * f2) % M) {     //While still in the cluster, remove and     reinsert all items
            int tempKey = map[h]->key;
            char tempValue = map[h]->value;
            delete map[h];
            map[h] = NULL;
            insert(tempKey, tempValue);
            ++p;
        }
        return p;

    }

}

这是我的主要内容:

RHHM bh(10);
bh.insert(0, 'A');
bh.insert(10, 'B');
bh.insert(20, 'C');
bh.print(std::cout);

输出:

<[ 0:A, - , 10:B, 20:C, - , - , - , - , - , - ]>

如您所见,第一个索引哈希为0 由于10键与0冲突,因此double hash( 10 )应该哈希为1 ,但是其哈希为2 为什么返回错误的值?

这是因为hash2函数返回键10的值2。对于hash2(10),hash2可以显示为。

return 10 % (10-1) + 1

这又由运算符优先级评估为值2 as。

return (10 %(10-1))+1

然后在插入函数中发生冲突时,将hash2值添加到哈希值中以获得新的索引,其值为。

h= f + ( P * f2 ) % M
h= 0 + (1 * 2 ) % 10 \\ this evaluates to 2.

这就是为什么您将新索引设置为2的原因。

编辑:该代码对运算符优先级有一些限制。 第一h = f +(p * f2)%M; //必要时环绕数组

这不会环绕数组。 因为%的优先级高于+。 f的值在[0-M-1]范围内,并且(p * f2)%M的值在[0-M-1]范围内。 因此,上面的表达式可以求值为[0-2 * M-2]范围内的值。 对于代码中的其他此类表达式也是如此。 这是哈希表中某些键可能丢失的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM