2023-06-14 02:01:06 +08:00
|
|
|
"""
|
|
|
|
File: hash_map_open_addressing.py
|
|
|
|
Created Time: 2023-06-13
|
2024-02-07 22:21:18 +08:00
|
|
|
Author: krahets (krahets@163.com)
|
2023-06-14 02:01:06 +08:00
|
|
|
"""
|
|
|
|
|
2023-10-19 16:21:51 +08:00
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
2023-06-14 02:01:06 +08:00
|
|
|
|
2023-10-19 16:21:51 +08:00
|
|
|
sys.path.append(str(Path(__file__).parent.parent))
|
2023-06-26 23:06:15 +08:00
|
|
|
from chapter_hashing.array_hash_map import Pair
|
2023-06-14 02:01:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
class HashMapOpenAddressing:
|
|
|
|
"""开放寻址哈希表"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""构造方法"""
|
|
|
|
self.size = 0 # 键值对数量
|
|
|
|
self.capacity = 4 # 哈希表容量
|
2023-09-24 20:38:21 +08:00
|
|
|
self.load_thres = 2.0 / 3.0 # 触发扩容的负载因子阈值
|
2023-06-14 02:01:06 +08:00
|
|
|
self.extend_ratio = 2 # 扩容倍数
|
|
|
|
self.buckets: list[Pair | None] = [None] * self.capacity # 桶数组
|
2023-09-21 17:43:15 +08:00
|
|
|
self.TOMBSTONE = Pair(-1, "-1") # 删除标记
|
2023-06-14 02:01:06 +08:00
|
|
|
|
|
|
|
def hash_func(self, key: int) -> int:
|
|
|
|
"""哈希函数"""
|
|
|
|
return key % self.capacity
|
|
|
|
|
|
|
|
def load_factor(self) -> float:
|
|
|
|
"""负载因子"""
|
|
|
|
return self.size / self.capacity
|
|
|
|
|
2023-09-21 17:43:15 +08:00
|
|
|
def find_bucket(self, key: int) -> int:
|
|
|
|
"""搜索 key 对应的桶索引"""
|
2023-06-14 02:01:06 +08:00
|
|
|
index = self.hash_func(key)
|
2023-09-21 17:43:15 +08:00
|
|
|
first_tombstone = -1
|
|
|
|
# 线性探测,当遇到空桶时跳出
|
|
|
|
while self.buckets[index] is not None:
|
2023-12-28 18:06:09 +08:00
|
|
|
# 若遇到 key ,返回对应的桶索引
|
2023-09-21 17:43:15 +08:00
|
|
|
if self.buckets[index].key == key:
|
2023-12-28 18:06:09 +08:00
|
|
|
# 若之前遇到了删除标记,则将键值对移动至该索引处
|
2023-09-21 17:43:15 +08:00
|
|
|
if first_tombstone != -1:
|
|
|
|
self.buckets[first_tombstone] = self.buckets[index]
|
|
|
|
self.buckets[index] = self.TOMBSTONE
|
|
|
|
return first_tombstone # 返回移动后的桶索引
|
|
|
|
return index # 返回桶索引
|
|
|
|
# 记录遇到的首个删除标记
|
|
|
|
if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:
|
|
|
|
first_tombstone = index
|
2023-12-28 18:06:09 +08:00
|
|
|
# 计算桶索引,越过尾部则返回头部
|
2023-09-21 17:43:15 +08:00
|
|
|
index = (index + 1) % self.capacity
|
|
|
|
# 若 key 不存在,则返回添加点的索引
|
|
|
|
return index if first_tombstone == -1 else first_tombstone
|
|
|
|
|
|
|
|
def get(self, key: int) -> str:
|
|
|
|
"""查询操作"""
|
|
|
|
# 搜索 key 对应的桶索引
|
|
|
|
index = self.find_bucket(key)
|
|
|
|
# 若找到键值对,则返回对应 val
|
|
|
|
if self.buckets[index] not in [None, self.TOMBSTONE]:
|
|
|
|
return self.buckets[index].val
|
|
|
|
# 若键值对不存在,则返回 None
|
|
|
|
return None
|
2023-06-14 02:01:06 +08:00
|
|
|
|
|
|
|
def put(self, key: int, val: str):
|
|
|
|
"""添加操作"""
|
|
|
|
# 当负载因子超过阈值时,执行扩容
|
|
|
|
if self.load_factor() > self.load_thres:
|
|
|
|
self.extend()
|
2023-09-21 17:43:15 +08:00
|
|
|
# 搜索 key 对应的桶索引
|
|
|
|
index = self.find_bucket(key)
|
|
|
|
# 若找到键值对,则覆盖 val 并返回
|
|
|
|
if self.buckets[index] not in [None, self.TOMBSTONE]:
|
|
|
|
self.buckets[index].val = val
|
|
|
|
return
|
|
|
|
# 若键值对不存在,则添加该键值对
|
|
|
|
self.buckets[index] = Pair(key, val)
|
|
|
|
self.size += 1
|
2023-06-14 02:01:06 +08:00
|
|
|
|
|
|
|
def remove(self, key: int):
|
|
|
|
"""删除操作"""
|
2023-09-21 17:43:15 +08:00
|
|
|
# 搜索 key 对应的桶索引
|
|
|
|
index = self.find_bucket(key)
|
|
|
|
# 若找到键值对,则用删除标记覆盖它
|
|
|
|
if self.buckets[index] not in [None, self.TOMBSTONE]:
|
|
|
|
self.buckets[index] = self.TOMBSTONE
|
|
|
|
self.size -= 1
|
2023-06-14 02:01:06 +08:00
|
|
|
|
|
|
|
def extend(self):
|
|
|
|
"""扩容哈希表"""
|
|
|
|
# 暂存原哈希表
|
|
|
|
buckets_tmp = self.buckets
|
|
|
|
# 初始化扩容后的新哈希表
|
|
|
|
self.capacity *= self.extend_ratio
|
|
|
|
self.buckets = [None] * self.capacity
|
|
|
|
self.size = 0
|
|
|
|
# 将键值对从原哈希表搬运至新哈希表
|
|
|
|
for pair in buckets_tmp:
|
2023-09-21 17:43:15 +08:00
|
|
|
if pair not in [None, self.TOMBSTONE]:
|
2023-06-14 02:01:06 +08:00
|
|
|
self.put(pair.key, pair.val)
|
|
|
|
|
2023-07-24 22:34:05 +08:00
|
|
|
def print(self):
|
2023-06-14 02:01:06 +08:00
|
|
|
"""打印哈希表"""
|
|
|
|
for pair in self.buckets:
|
2023-09-21 17:43:15 +08:00
|
|
|
if pair is None:
|
2023-06-14 02:01:06 +08:00
|
|
|
print("None")
|
2023-09-21 17:43:15 +08:00
|
|
|
elif pair is self.TOMBSTONE:
|
|
|
|
print("TOMBSTONE")
|
|
|
|
else:
|
|
|
|
print(pair.key, "->", pair.val)
|
2023-06-14 02:01:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
"""Driver Code"""
|
|
|
|
if __name__ == "__main__":
|
2023-09-21 17:43:15 +08:00
|
|
|
# 初始化哈希表
|
2023-06-14 02:01:06 +08:00
|
|
|
hashmap = HashMapOpenAddressing()
|
|
|
|
|
|
|
|
# 添加操作
|
|
|
|
# 在哈希表中添加键值对 (key, val)
|
|
|
|
hashmap.put(12836, "小哈")
|
|
|
|
hashmap.put(15937, "小啰")
|
|
|
|
hashmap.put(16750, "小算")
|
|
|
|
hashmap.put(13276, "小法")
|
|
|
|
hashmap.put(10583, "小鸭")
|
|
|
|
print("\n添加完成后,哈希表为\nKey -> Value")
|
|
|
|
hashmap.print()
|
|
|
|
|
|
|
|
# 查询操作
|
2023-12-28 18:06:09 +08:00
|
|
|
# 向哈希表中输入键 key ,得到值 val
|
2023-06-14 02:01:06 +08:00
|
|
|
name = hashmap.get(13276)
|
|
|
|
print("\n输入学号 13276 ,查询到姓名 " + name)
|
|
|
|
|
|
|
|
# 删除操作
|
|
|
|
# 在哈希表中删除键值对 (key, val)
|
|
|
|
hashmap.remove(16750)
|
|
|
|
print("\n删除 16750 后,哈希表为\nKey -> Value")
|
|
|
|
hashmap.print()
|