2022-12-23 18:28:17 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: array_hash_map.cs
|
|
|
|
|
* Created Time: 2022-12-23
|
|
|
|
|
* Author: haptear (haptear@hotmail.com)
|
|
|
|
|
*/
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
namespace hello_algo.chapter_hashing;
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-04-21 14:59:22 +08:00
|
|
|
|
/* 键值对 int->string */
|
2023-06-14 02:01:06 +08:00
|
|
|
|
class Pair {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
public int key;
|
2023-04-21 14:59:22 +08:00
|
|
|
|
public string val;
|
2023-06-14 02:01:06 +08:00
|
|
|
|
public Pair(int key, string val) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
this.key = key;
|
|
|
|
|
this.val = val;
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 基于数组简易实现的哈希表 */
|
2023-04-23 03:03:12 +08:00
|
|
|
|
class ArrayHashMap {
|
2023-10-08 01:33:46 +08:00
|
|
|
|
private readonly List<Pair?> buckets;
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public ArrayHashMap() {
|
2023-03-15 03:48:31 +08:00
|
|
|
|
// 初始化数组,包含 100 个桶
|
2023-03-15 03:11:43 +08:00
|
|
|
|
buckets = new();
|
2023-04-23 03:03:12 +08:00
|
|
|
|
for (int i = 0; i < 100; i++) {
|
2023-03-15 03:11:43 +08:00
|
|
|
|
buckets.Add(null);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 哈希函数 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
private int HashFunc(int key) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
int index = key % 100;
|
|
|
|
|
return index;
|
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 查询操作 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public string? Get(int key) {
|
|
|
|
|
int index = HashFunc(key);
|
2023-06-14 02:01:06 +08:00
|
|
|
|
Pair? pair = buckets[index];
|
2023-02-08 22:18:02 +08:00
|
|
|
|
if (pair == null) return null;
|
|
|
|
|
return pair.val;
|
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 添加操作 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public void Put(int key, string val) {
|
|
|
|
|
Pair pair = new(key, val);
|
|
|
|
|
int index = HashFunc(key);
|
2023-03-15 03:11:43 +08:00
|
|
|
|
buckets[index] = pair;
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 删除操作 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public void Remove(int key) {
|
|
|
|
|
int index = HashFunc(key);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
// 置为 null ,代表删除
|
2023-03-15 03:11:43 +08:00
|
|
|
|
buckets[index] = null;
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 获取所有键值对 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public List<Pair> PairSet() {
|
2023-06-14 02:01:06 +08:00
|
|
|
|
List<Pair> pairSet = new();
|
|
|
|
|
foreach (Pair? pair in buckets) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
if (pair != null)
|
2023-06-14 02:01:06 +08:00
|
|
|
|
pairSet.Add(pair);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-06-14 02:01:06 +08:00
|
|
|
|
return pairSet;
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 获取所有键 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public List<int> KeySet() {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
List<int> keySet = new();
|
2023-06-14 02:01:06 +08:00
|
|
|
|
foreach (Pair? pair in buckets) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
if (pair != null)
|
|
|
|
|
keySet.Add(pair.key);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
return keySet;
|
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 获取所有值 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public List<string> ValueSet() {
|
2023-04-21 14:59:22 +08:00
|
|
|
|
List<string> valueSet = new();
|
2023-06-14 02:01:06 +08:00
|
|
|
|
foreach (Pair? pair in buckets) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
if (pair != null)
|
|
|
|
|
valueSet.Add(pair.val);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
return valueSet;
|
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 打印哈希表 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public void Print() {
|
|
|
|
|
foreach (Pair kv in PairSet()) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
Console.WriteLine(kv.key + " -> " + kv.val);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|
2022-12-23 15:42:02 +08:00
|
|
|
|
|
|
|
|
|
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public class array_hash_map {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[Test]
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public void Test() {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
/* 初始化哈希表 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
ArrayHashMap map = new();
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
2023-10-08 01:33:46 +08:00
|
|
|
|
map.Put(12836, "小哈");
|
|
|
|
|
map.Put(15937, "小啰");
|
|
|
|
|
map.Put(16750, "小算");
|
|
|
|
|
map.Put(13276, "小法");
|
|
|
|
|
map.Put(10583, "小鸭");
|
2023-02-08 22:18:02 +08:00
|
|
|
|
Console.WriteLine("\n添加完成后,哈希表为\nKey -> Value");
|
2023-10-08 01:33:46 +08:00
|
|
|
|
map.Print();
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
2023-10-08 01:33:46 +08:00
|
|
|
|
string? name = map.Get(15937);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name);
|
|
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
2023-10-08 01:33:46 +08:00
|
|
|
|
map.Remove(10583);
|
2023-02-08 22:18:02 +08:00
|
|
|
|
Console.WriteLine("\n删除 10583 后,哈希表为\nKey -> Value");
|
2023-10-08 01:33:46 +08:00
|
|
|
|
map.Print();
|
2023-02-08 22:18:02 +08:00
|
|
|
|
|
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
|
Console.WriteLine("\n遍历键值对 Key->Value");
|
2023-10-08 01:33:46 +08:00
|
|
|
|
foreach (Pair kv in map.PairSet()) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
Console.WriteLine(kv.key + " -> " + kv.val);
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("\n单独遍历键 Key");
|
2023-10-08 01:33:46 +08:00
|
|
|
|
foreach (int key in map.KeySet()) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
Console.WriteLine(key);
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("\n单独遍历值 Value");
|
2023-10-08 01:33:46 +08:00
|
|
|
|
foreach (string val in map.ValueSet()) {
|
2023-02-08 22:18:02 +08:00
|
|
|
|
Console.WriteLine(val);
|
2022-12-23 15:42:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 22:18:02 +08:00
|
|
|
|
}
|