线性哈希与基本哈希表

时间:2015-12-07 16:22:14

标签: performance hash hashtable linear

基本哈希表通常不会很好地增长或缩小。当数据对于hastable而言变得太大并且其性能开始严重降低时,这就成了问题。

几种解决方案之一是线性散列。但是,我不明白线性哈希如何解决这个特殊问题?

谢谢。

1 个答案:

答案 0 :(得分:0)

当我们能够为每个可能的密钥分配一个位置的数组时,

直接寻址是适用的。假设我们没有足够的空间为每个可能的密钥分配位置,那么我们需要一种机制来处理这种情况 在线性探测中,我们从原始哈希位置开始依次搜索哈希表。如果某个位置被占用,我们会检查下一个位置。
重新散列的功能是:rehash(key)=(n+1)%tablesize

Suppose we have five elements in our array:{2,7,8,10,15}
so 2 will map to index value 2(2%5=2)
now 7%5 also equals to 2 but 2 is occupied so check (7+1)%5=3 so it will map to 3

now 8%5=3 so rehash(8)=(8+1)%5=4
 10 will map to 5 and 15 will map to 0 in the same way because it wrap around from the last table location to the first location if necessary.

要探测的下一个位置由步长确定,其中其他步长(一个)是可能的。步长应该是相对于表格大小的素数,即它们的GCD应该等于1.

相关问题