哈希表


import pprint


class HashTable:
    '''拉链法解决哈希冲突'''
    def __init__(self, elements):
        self.bucket_size = len(elements)
        self.buckets = [[] for _ in range(self.bucket_size)]
        self._assign_buckets(elements)

    def _assign_buckets(self, elements):
        for key, value in elements:
            hashed_key = hash(key)
            index = hashed_key % self.bucket_size
            self.buckets[index].append((key, value))

    def get_value(self, input_key):
        hashed_key = hash(input_key)
        index = hashed_key % self.bucket_size
        for key, vlaue in self.buckets[index]:
            if key == input_key:
                return vlaue
        return None

    def __str__(self):
        return pprint.pformat(self.buckets)


if __name__ == '__main__':
    capitals = [
        ('France', 'Paris'),
        ('United States', 'Washington D.C.'),
        ('Italy', 'Rome'),
        ('Canada', 'Ottawa')
    ]
    hashtable = HashTable(capitals)
    print(hashtable)
    print(f"The capital of Italy is {hashtable.get_value('Italy')}")
    '''
    pprint result
    [[('Canada', 'Ottawa')],
    [('France', 'Paris')],
    [('United States', 'Washington D.C.'), ('Italy', 'Rome')],
    []]
    The capital of Italy is Rome
    '''
import pprint


class HashTable:
    '''
    开放寻址法:线性探测
    缺点:开放式寻址策略的主要问题是,如果您还必须处理表中元素的删除,则需要执行逻辑删除而不是物理删除,因为如果您删除在冲突期间占用存储桶的值,则另一个碰撞的元素将永远不会被发现。
    在我们之前的示例中,它Italy与先前插入的元素(France)相撞,因此已被重新定位到下一个存储桶,因此删除该France元素将使其Italy无法访问,因为它不占用其自然的目标存储桶,对于解释器而言这是空的。
    因此,在使用开放式寻址策略时,要删除一个元素,您必须用一个哑数值替换其存储桶,这向解释器表明,必须考虑删除该元素以进行新的插入,但必须将其占用以进行检索。
    '''

    def __init__(self, elements):
        self.bucket_size = len(elements)
        self.buckets = [None] * self.bucket_size
        self._assign_buckets(elements)

    def _assign_buckets(self, elements):
        for key, value in elements:
            hashed_key = hash(key)
            index = hashed_key % self.bucket_size

            while self.buckets[index] is not None:
                print(f"The key {key} collided with {self.buckets[index]}")
                ###线性探测###
                index = (index + 1) % self.bucket_size

            self.buckets[index] = (key, value)

    def get_value(self, input_key):
        hashed_key = hash(input_key)
        index = hashed_key % self.bucket_size
        while self.buckets[index] is not None:
            key, value = self.buckets[index]
            if key == input_key:
                return value
            index = (index + 1) % self.bucket_size

    def __str__(self):
        return pprint.pformat(self.buckets)


if __name__ == '__main__':
    capitals = [
        ('France', 'Paris'),
        ('United States', 'Washington D.C.'),
        ('Italy', 'Rome'),
        ('Canada', 'Ottawa')
    ]
    hashtable = HashTable(capitals)
    print(hashtable)
    print(f"The capital of Italy is {hashtable.get_value('Italy')}")
    '''
The key United States collided with ('France', 'Paris')
[('Italy', 'Rome'),
 ('France', 'Paris'),
 ('United States', 'Washington D.C.'),
 ('Canada', 'Ottawa')]
The capital of Italy is Rome
    '''
原文地址:https://www.cnblogs.com/liuer-mihou/p/13830141.html