Algorithm 联合散列删除算法
有人能给我举一个合并链式哈希表的删除算法的例子吗 我的插入算法如下:Algorithm 联合散列删除算法,algorithm,hash,pseudocode,Algorithm,Hash,Pseudocode,有人能给我举一个合并链式哈希表的删除算法的例子吗 我的插入算法如下: Insert (key) int p = hash(key) if d[p] = NIL then d[p] = key next[p] = NIL else while next[p] != NIL p = next[p] endwhile td[firstEmpty] = key
Insert (key)
int p = hash(key)
if d[p] = NIL then
d[p] = key
next[p] = NIL
else
while next[p] != NIL
p = next[p]
endwhile
td[firstEmpty] = key
next[p] = firstEmpty
next[firstEmpty] = NIL
endif
UpdateFirstEmpty(); //sets firstEmpty to first empty slot with lowest index
endInsert
index| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12|
d| 18| 13| 15| 16| 31| 5| 26| -1| -1| -1| -1| -1| -1|
next| 1| 4| -1| -1| 6| 0| -1| -1| -1| -1| -1| -1| -1|
假设表中的插槽数为13。哈希函数返回键%13
Keys | 5 | 18 | 16 | 15 | 13 | 31 | 26 |
Hash(key)| 5 | 5 | 3 | 2 | 0 | 5 | 0 |
按此顺序插入后,我的表将如下所示:
Insert (key)
int p = hash(key)
if d[p] = NIL then
d[p] = key
next[p] = NIL
else
while next[p] != NIL
p = next[p]
endwhile
td[firstEmpty] = key
next[p] = firstEmpty
next[firstEmpty] = NIL
endif
UpdateFirstEmpty(); //sets firstEmpty to first empty slot with lowest index
endInsert
index| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12|
d| 18| 13| 15| 16| 31| 5| 26| -1| -1| -1| -1| -1| -1|
next| 1| 4| -1| -1| 6| 0| -1| -1| -1| -1| -1| -1| -1|
其中-1=NIL
如果有人能向我解释一下,当我在不打破锁链的情况下取下钥匙时,我应该怎么做,即使是用文字表达,我也会非常感激
谢谢
编辑-:我想我终于得到了。。我使用的技术与从开放寻址哈希表中删除项时使用的技术相同 事情是这样的:
Search for key position and it's predecessor pp
if key is found at position p
if pp != NIL then
next[pp] = NIL
d[p] = NIL //deletes the key
p = next[p] //move position to next value in the chain
UpdateFirstEmpty()
while d[p] != NIL do
temp = d[p] //save value
d[p] = NIL //delete value
p = next[p] //move position to next value in chain
UpdateFirstEmpty()
Insert(temp) //insert the value in the list again
endwhile
endif
endalg
index| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12|
d| 18| 13| 15| 16| 31| 5| 26| -1| -1| -1| -1| -1| -1|
next| 1| 4| -1| -1| 6| 0| -1| -1| -1| -1| -1| -1| -1|
firstFree: 7
delete 18
index| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12|
d| 13| 31| 15| 16| 26| 5| -1| -1| -1| -1| -1| -1| -1|
next| 4| -1| -1| -1| -1| 1| -1| -1| -1| -1| -1| -1| -1|
firstFree: 6
delete 13
index| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12|
d| 26| 31| 15| 16| -1| 5| -1| -1| -1| -1| -1| -1| -1|
next| -1| -1| -1| -1| -1| 1| -1| -1| -1| -1| -1| -1| -1|
firstFree: 4
delete 26
index| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12|
d| -1| 31| 15| 16| -1| 5| -1| -1| -1| -1| -1| -1| -1|
next| -1| -1| -1| -1| -1| 1| -1| -1| -1| -1| -1| -1| -1|
firstFree: 0
我不认为这是正确的方法,但它似乎起了作用。无论如何,我希望将来能帮助到别人。我们可以做一件事来简化删除,如下所示: 假设PP是节点P(要删除)的父节点。因为我们知道联合散列是 线性探测和链接的组合。因此,我们不需要向上吸取P之后的所有链式元素,只需在数据中输入NULL,然后在P的下一部分中输入ppopulate next[PP]到next[P]。因此,下次当哈希函数将某个键映射到此位置时,它可以简单地将其放入其中。算法如下所示: 删除:
Next[PP]=Next[P]; //as simple as deletion in link list without deleting node here
D[P]=NULL;
Next[P]=NULL;
我们完成了。现在,线性探测(在发生碰撞的情况下)将跟随每个碰撞节点中的下一个指针,而不是将其合并到链中的下一个自由位置。让我们看一下插入后得到的表。我想删除键18。是的,PP是5。如果我做Next[PP]=Next[P],这意味着从索引5我可以跳到索引1。好的,到目前为止还可以。但在索引1,我有一个散列值为0的键。如果我想找那把钥匙,我就找不到了。它必须移动到索引0处的正确位置,因为D[0]现在是自由的。如果我在尝试搜索键13(散列(13)=0)时不移动项目,我将首先检查D[hash(13)],它将为零,我将认为该项目不存在。但该项目确实存在于索引1中。