Python MyHashTable类:带有线性探测的搜索方法

Python MyHashTable类:带有线性探测的搜索方法,python,class,hashtable,linear-probing,Python,Class,Hashtable,Linear Probing,我需要帮助为我的“MyHashTable”类实现一个方法: 该方法应使用线性探测来处理碰撞分辨率。如果search_键位于哈希表中,则该方法返回包含该search_键的插槽的插槽号。如果搜索键不在哈希表中,则该方法返回-1 我的班级是这样的: class MyHashTable: def __init__(self, capacity): self.capacity = capacity self.slots = [None] * self.capacity def __st

我需要帮助为我的“MyHashTable”类实现一个方法:

该方法应使用线性探测来处理碰撞分辨率。如果search_键位于哈希表中,则该方法返回包含该search_键的插槽的插槽号。如果搜索键不在哈希表中,则该方法返回-1

我的班级是这样的:

class MyHashTable:

def __init__(self, capacity):
    self.capacity = capacity
    self.slots = [None] * self.capacity

def __str__(self):
    return str(self.slots )

def __len__(self):
    count = 0
    for i in self.slots:
        if i != None:
            count += 1
    return count

def hash_function(self, key):
    i = key % self.capacity
    return i

def insert(self, key):
    slot = self.hash_function(key)
    orig = slot
    while True:
        if self.slots[slot] is None:
            self.slots[slot] = key
            return slot

        if self.slots[slot] == key:
            return -2

        slot = (slot + 1) % self.capacity
        if slot == orig:
            return -1

def search(self, search_key):
任何帮助或教程链接都会很棒。
感谢您只使用一个列表来存储所有值,如果您想要一个哈希表,您可以使用一个列表列表,其中每个列表都是一个bucket,但是如果您只想用自己的代码检查元素是否在哈希表中:

def search(self, search_key):
    hsh = self.hash_function(search_key)
    if self.slots[hsh] is None:
        return -1
    while hsh < self.capacity:
        if self.slots[hsh] == search_key:
            return hsh
        hsh += 1
    return -1
第一个while循环将一次探测一个值,但是如果我们从多个冲突中环绕列表,它将在开始时丢失元素,因此使用
range
mod=(hsh+i)%self。capacity
确保我们检查所有条目,如下面的示例所示

m = MyHashTable(5)

m.insert(13) # 13 % 5 = 3
m.insert(73) # 83 % 5 = 3
m.insert(93) # 93 & 5 = 3
print(m.search(13)) # 3
print(m.search(73)) # 4
print(m.search(93)) # 0
print(m.search(2)) # -1
通过跟踪向哈希表添加唯一值的时间,您可以创建len方法
O(1)
。此外,还有一个很好的wiki页面,您可以将其应用到代码中,它将帮助您创建键到值的正确映射,并在需要时调整哈希表的大小。如果您想存储的不仅仅是数字,您需要使用不同的哈希函数,我只使用哈希函数,但您可以使用任何您喜欢的函数。当哈希表已满且密钥不存在时,在中使用
,也会导致无限循环,因此需要处理这种情况:

class MyHashTable:
    def __init__(self, capacity):
        self.capacity = capacity
        self.slots = [None] * self.capacity
        self.count = 0

    def __str__(self):
        return str(self.slots)

    def __contains__(self, item):
        return self.search(item) != -1

    def __len__(self):
        return self.count

    def hash_function(self, key):
        return hash(key) % self.capacity

    def find_slot(self, key):
        slot = self.hash_function(key)
        while self.slots[slot] is not None and self.slots[slot] != key:
            slot = (slot + 1) % self.capacity
        return slot

    def insert(self, key):
        slot = self.find_slot(key)
        if self.slots[slot] != key:
            self.slots[slot] = key
            self.count += 1

    def search(self, key):
        i = self.find_slot(key)
        if self.slots[i] is not None:  
            return i
        return -1
添加
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

m = MyHashTable(5)

m.insert("foo")
m.insert(73)
m.insert(93)
m.insert(1)
print(m.search(73))
print(m.search(93))
print(m.search(1))
print(m.search("foo"))
m.insert(73)
print(m.slots)
print(len(m))
print("foo" in m)
print(5 in m)
输出:

3
4
1
0
['foo', 1, None, 73, 93]
4
True
False
m = MyHashTable(5)

m.insert("foo")
m.insert(73)
m.insert(93)
m.insert(1)
print(m.search(73))
print(m.search(93))
print(m.search(1))
print(m.search("foo"))
m.insert(73)
print(m.slots)
print(len(m))
print("foo" in m)
print(5 in m)
3
4
1
0
['foo', 1, None, 73, 93]
4
True
False