Index | Diary 2024-05-22

思路

将一些键值 存储到一个数组中 - 散列表

方法

将整数打散,常用除留余数法

余数可能会相同,出现 collision

解决冲突

性能表现

哈希表的性能 - 受装因子影响 Load Factor \(\alpha = \frac{N}{M}\)

N is the number of entries occupied in the hash table

M is the number of buckets

coding

拉链法实现开散列

    class Node:
        def __init__(self, key, calue):
            self.key = key
            self.value = value
            self.next = None
            
    class ChainingHashTable:
        def __init__(self, size):
            """
            初始化散列表大小和数组
            """
            self.size = size
            self.hash_table = [None] * size
            
        def _hash(self, key):
            """
            散列函数、将键转换为散列索引
            """
            return key % self.size
            
        def insert(self, key, value):
            """
            向散列表中插入键值对
            """
            index = self._hash(key)
            if self.hash_table[index] is None:
                self.hash_table[index] = Node(key, value)
            else:
                # 索引位置不为空,遍历链表找到合适位置插入新节点
                current = self.hash_table[index]
                while current.next:
                    current = current.next
                current.next = Node(key, value)    
                
        def serch(self, key):        
            """
            散列表中查找给定键的值
            """
            index = self._hash(key)
            current = self.hash_table[index]
            while current:
                if current.key == key:
                    return current.value
                current = current.next
            return None