2022-12-14 17:18:32 +08:00
|
|
|
"""
|
|
|
|
File: array_hash_map.py
|
|
|
|
Created Time: 2022-12-14
|
|
|
|
Author: msk397 (machangxinq@gmail.com)
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Entry:
|
2023-03-03 03:07:22 +08:00
|
|
|
""" 键值对 int->String """
|
2023-03-12 18:49:52 +08:00
|
|
|
def __init__(self, key: int, val: str):
|
2022-12-14 17:18:32 +08:00
|
|
|
self.key = key
|
|
|
|
self.val = val
|
|
|
|
|
|
|
|
class ArrayHashMap:
|
2023-03-03 03:07:22 +08:00
|
|
|
""" 基于数组简易实现的哈希表 """
|
2022-12-14 17:18:32 +08:00
|
|
|
def __init__(self):
|
2023-03-03 03:07:22 +08:00
|
|
|
""" 构造方法 """
|
2023-03-15 03:48:31 +08:00
|
|
|
# 初始化数组,包含 100 个桶
|
2023-03-23 18:51:56 +08:00
|
|
|
self.buckets: list[Entry | None] = [None] * 100
|
2022-12-14 17:18:32 +08:00
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
def hash_func(self, key: int) -> int:
|
2023-03-03 03:07:22 +08:00
|
|
|
""" 哈希函数 """
|
2023-03-12 18:49:52 +08:00
|
|
|
index: int = key % 100
|
2022-12-14 17:18:32 +08:00
|
|
|
return index
|
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
def get(self, key: int) -> str:
|
2023-03-03 03:07:22 +08:00
|
|
|
""" 查询操作 """
|
2023-03-12 18:49:52 +08:00
|
|
|
index: int = self.hash_func(key)
|
2023-03-15 03:11:43 +08:00
|
|
|
pair: Entry = self.buckets[index]
|
2022-12-14 17:18:32 +08:00
|
|
|
if pair is None:
|
|
|
|
return None
|
|
|
|
return pair.val
|
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
def put(self, key: int, val: str) -> None:
|
2023-03-03 03:07:22 +08:00
|
|
|
""" 添加操作 """
|
2022-12-14 17:18:32 +08:00
|
|
|
pair = Entry(key, val)
|
2023-03-12 18:49:52 +08:00
|
|
|
index: int = self.hash_func(key)
|
2023-03-15 03:11:43 +08:00
|
|
|
self.buckets[index] = pair
|
2022-12-14 17:18:32 +08:00
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
def remove(self, key: int) -> None:
|
2023-03-03 03:07:22 +08:00
|
|
|
""" 删除操作 """
|
2023-03-12 18:49:52 +08:00
|
|
|
index: int = self.hash_func(key)
|
2023-02-03 20:03:30 +08:00
|
|
|
# 置为 None ,代表删除
|
2023-03-15 03:11:43 +08:00
|
|
|
self.buckets[index] = None
|
2022-12-14 17:18:32 +08:00
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
def entry_set(self) -> list[Entry]:
|
2023-03-03 03:07:22 +08:00
|
|
|
""" 获取所有键值对 """
|
2023-03-23 18:51:56 +08:00
|
|
|
result: list[Entry] = []
|
2023-03-15 03:11:43 +08:00
|
|
|
for pair in self.buckets:
|
2022-12-14 17:18:32 +08:00
|
|
|
if pair is not None:
|
|
|
|
result.append(pair)
|
|
|
|
return result
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
def key_set(self) -> list[int]:
|
2023-03-03 03:07:22 +08:00
|
|
|
""" 获取所有键 """
|
2023-03-23 18:51:56 +08:00
|
|
|
result: list[int] = []
|
2023-03-15 03:11:43 +08:00
|
|
|
for pair in self.buckets:
|
2022-12-14 17:18:32 +08:00
|
|
|
if pair is not None:
|
|
|
|
result.append(pair.key)
|
|
|
|
return result
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
def value_set(self) -> list[str]:
|
2023-03-03 03:07:22 +08:00
|
|
|
""" 获取所有值 """
|
2023-03-23 18:51:56 +08:00
|
|
|
result: list[str] = []
|
2023-03-15 03:11:43 +08:00
|
|
|
for pair in self.buckets:
|
2022-12-14 17:18:32 +08:00
|
|
|
if pair is not None:
|
|
|
|
result.append(pair.val)
|
|
|
|
return result
|
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
def print(self) -> None:
|
2023-03-03 03:07:22 +08:00
|
|
|
""" 打印哈希表 """
|
2023-03-15 03:11:43 +08:00
|
|
|
for pair in self.buckets:
|
2022-12-14 17:18:32 +08:00
|
|
|
if pair is not None:
|
|
|
|
print(pair.key, "->", pair.val)
|
|
|
|
|
|
|
|
|
2022-12-15 23:08:07 +08:00
|
|
|
""" Driver Code """
|
2022-12-14 17:18:32 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
""" 初始化哈希表 """
|
2022-12-15 09:43:20 +08:00
|
|
|
mapp = ArrayHashMap()
|
2022-12-14 17:18:32 +08:00
|
|
|
|
|
|
|
""" 添加操作 """
|
|
|
|
# 在哈希表中添加键值对 (key, value)
|
2022-12-15 09:43:20 +08:00
|
|
|
mapp.put(12836, "小哈")
|
|
|
|
mapp.put(15937, "小啰")
|
|
|
|
mapp.put(16750, "小算")
|
|
|
|
mapp.put(13276, "小法")
|
|
|
|
mapp.put(10583, "小鸭")
|
2022-12-14 17:18:32 +08:00
|
|
|
print("\n添加完成后,哈希表为\nKey -> Value")
|
2022-12-15 09:43:20 +08:00
|
|
|
mapp.print()
|
2022-12-14 17:18:32 +08:00
|
|
|
|
|
|
|
""" 查询操作 """
|
|
|
|
# 向哈希表输入键 key ,得到值 value
|
2022-12-15 09:43:20 +08:00
|
|
|
name = mapp.get(15937)
|
2022-12-14 17:18:32 +08:00
|
|
|
print("\n输入学号 15937 ,查询到姓名 " + name)
|
|
|
|
|
|
|
|
""" 删除操作 """
|
|
|
|
# 在哈希表中删除键值对 (key, value)
|
2022-12-15 09:43:20 +08:00
|
|
|
mapp.remove(10583)
|
2022-12-14 17:18:32 +08:00
|
|
|
print("\n删除 10583 后,哈希表为\nKey -> Value")
|
2022-12-15 09:43:20 +08:00
|
|
|
mapp.print()
|
2022-12-14 17:18:32 +08:00
|
|
|
|
|
|
|
""" 遍历哈希表 """
|
|
|
|
print("\n遍历键值对 Key->Value")
|
2023-02-03 20:03:30 +08:00
|
|
|
for pair in mapp.entry_set():
|
2022-12-14 17:18:32 +08:00
|
|
|
print(pair.key, "->", pair.val)
|
|
|
|
|
|
|
|
print("\n单独遍历键 Key")
|
2023-02-03 20:03:30 +08:00
|
|
|
for key in mapp.key_set():
|
2022-12-14 17:18:32 +08:00
|
|
|
print(key)
|
|
|
|
|
|
|
|
print("\n单独遍历值 Value")
|
2023-02-03 20:03:30 +08:00
|
|
|
for val in mapp.value_set():
|
2022-12-15 23:08:07 +08:00
|
|
|
print(val)
|