【发布时间】:2018-10-30 07:07:04
【问题描述】:
您好,我是 python 新手,我有一个使用线性探测来解决冲突的哈希表。我知道线性探针是当 N+1 ,N+2, N+3 时,但二次探针是当 n+1, n+4, n+9 ... 这是我的线性探针设置项功能
def __setitem__(self, key, value):
position = self.hash_value(key)
for _ in range(self.table_size):
if self.array[position] is None:#found empty slot
self.array[position] = (key, value)
self.count += 1
return
elif self.array[position][0] == key:#found key
self.array[position] = (key, value)#update value
return
else:#not found try next
position = (position+1) % self.table_size
raise ValueError("Table is Full!")
为了将其转换为二次探头,我尝试将位置更改为
position = (position+(i+1)**2) % self.table_size
但显然这是错误的,因为二次索引被添加到最后一个位置而不是原始位置?任何帮助将不胜感激!
【问题讨论】:
标签: python hashtable quadratic-probing