【问题标题】:Hash Table With Coalesced Hashing具有合并散列的散列表
【发布时间】:2015-09-10 02:24:56
【问题描述】:

我试图弄清楚如何将使用二次探测实现的哈希表类的添加方法转换为使用合并哈希的冲突解决方法。我知道它与链表有关,但是因为我是哈希表的新手,我不知道从哪里开始。

这里是有问题的add方法,

public boolean add(AnyType x) {
    int currentPos = findPos(x);
    if (isActive(array, currentPos))
        return false;
    if (array[currentPos] == null)
        occupied++;
    array[currentPos] = new HashEntry(x, true);
    currentSize++;
    modCount++;
    if (occupied > array.length / 2)
        rehash();
    return true;
}

如果有人能告诉我如何将此方法转换为使用合并散列,我将不胜感激。

【问题讨论】:

    标签: java hashtable hash-collision


    【解决方案1】:

    所以,在我遵循维基百科指南和代码here 之后,我将那里的代码修改为以下内容。我用 cmets 解释每个部分。

    public boolean add(AnyType x) {
        // Finds the place that you want to insert into your table.
        // I believe the function contains the hash mapping
        int currentPos = findPos(x);
        // if the place is empty, you will insert your hash in there
        if (array[currentPos] == null) {
            array[currentPos] = new HashEntry(x, true);
        } else {
            // The place has been occupied (collision occured), so we will 
            // add it as a second item like a link list.
            // But, we do not want to allocate a new space and we use the 
            // empty spaces we have in our hash table instead.
            // That is the differece between coalesced hash and speparate chaining
    
            // the indexes are from 0 to HASH_TABLE_SIZE -1
            int cursor = HASH_TABLE_SIZE - 1; 
            /* Find the first empty bucket for the new entry */
            while ( cursor >= 0 && array[cursor] != NULL )
                --cursor;
    
            /* The table is full, terminate unsuccessfully */
            if ( cursor == -1 )
              return false;
    
            // Initial your new value in an empty bucket we found in the table
            array[cursor] = new HashEntry(x, true);
    
            /* Find the last node in the chain and point to it */
            // Here we need to find the tail of the link list in our hash table
            // We do not want to miss the link list that we made from the past,
            // therefore, we need to do the following
            struct node* it = array[currentPos];
            while ( it->next != NULL )
              it = it->next;
            // Now we assign the 
            it->next = array[cursor];       
        }
        return true;
    }
    

    希望对你有帮助。

    【讨论】:

    • 这一定是用C的吧?我正在使用的语言是 Java。虽然这看起来适合 C,但我不知道如何处理 struct node。你能提供一个Java示例吗?
    • 所以,在java中,你需要带上你拥有的数组类型,而不是struct node*。例如它可以是下一个可以访问的“节点”对象数组。你可以看看crunchify.com/…, 'private class Node' 看看数组的类型如何+如何改变它->next with getNext()。
    • 补充前面的评论:我不认为我有你所说的过去的链接列表。我只有 array 属于 HashEntry 类。
    • 因此,表中的每个元素都是 Node 类的一个实例。我发送给您的链接将帮助您了解节点和链接列表的工作方式。
    • 我添加了我的数组类型 (HashEntry) 的 pastebin。 pastebin.com/CQPsqZMS 看起来是链表的形式,只是无法访问下一个元素。很抱歉我很新手,你可以看到我很难理解。
    猜你喜欢
    • 2016-05-02
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    相关资源
    最近更新 更多