From 6da6d241936de7779b80f252f9dc6418d42ad054 Mon Sep 17 00:00:00 2001 From: krahets Date: Tue, 1 Aug 2023 17:05:40 +0800 Subject: [PATCH] Fix the code of hash map chaining. --- .../chapter_hashing/hash_map_chaining.cs | 5 +- .../chapter_hashing/hash_map_chaining.dart | 9 ++- .../go/chapter_hashing/hash_collision_test.go | 62 +++++++++++++++++++ codes/go/chapter_hashing/hash_map_chaining.go | 2 +- codes/go/chapter_hashing/hash_map_test.go | 54 +--------------- .../chapter_hashing/hash_map_chaining.java | 6 +- .../chapter_hashing/hash_map_chaining.py | 2 +- 7 files changed, 79 insertions(+), 61 deletions(-) create mode 100644 codes/go/chapter_hashing/hash_collision_test.go diff --git a/codes/csharp/chapter_hashing/hash_map_chaining.cs b/codes/csharp/chapter_hashing/hash_map_chaining.cs index d728aea42..a05edf574 100644 --- a/codes/csharp/chapter_hashing/hash_map_chaining.cs +++ b/codes/csharp/chapter_hashing/hash_map_chaining.cs @@ -72,12 +72,13 @@ class HashMapChaining { public void remove(int key) { int index = hashFunc(key); // 遍历桶,从中删除键值对 - foreach (Pair pair in buckets[index].ToList()) { + foreach (Pair pair in buckets[index].ToList()) { if (pair.key == key) { buckets[index].Remove(pair); + size--; + break; } } - size--; } /* 扩容哈希表 */ diff --git a/codes/dart/chapter_hashing/hash_map_chaining.dart b/codes/dart/chapter_hashing/hash_map_chaining.dart index 91649d5dc..1804ab7e8 100644 --- a/codes/dart/chapter_hashing/hash_map_chaining.dart +++ b/codes/dart/chapter_hashing/hash_map_chaining.dart @@ -78,8 +78,13 @@ class HashMapChaining { int index = hashFunc(key); List bucket = buckets[index]; // 遍历桶,从中删除键值对 - bucket.removeWhere((Pair pair) => pair.key == key); - size--; + for (Pair pair in bucket) { + if (pair.key == key) { + bucket.remove(pair); + size--; + break; + } + } } /* 扩容哈希表 */ diff --git a/codes/go/chapter_hashing/hash_collision_test.go b/codes/go/chapter_hashing/hash_collision_test.go new file mode 100644 index 000000000..46ec26ebc --- /dev/null +++ b/codes/go/chapter_hashing/hash_collision_test.go @@ -0,0 +1,62 @@ +// File: hash_collision_test.go +// Created Time: 2022-12-14 +// Author: msk397 (machangxinq@gmail.com) + +package chapter_hashing + +import ( + "fmt" + "testing" +) + +func TestHashMapChaining(t *testing.T) { + /* 初始化哈希表 */ + hmap := newHashMapChaining() + + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + hmap.put(12836, "小哈") + hmap.put(15937, "小啰") + hmap.put(16750, "小算") + hmap.put(13276, "小法") + hmap.put(10583, "小鸭") + fmt.Println("\n添加完成后,哈希表为\nKey -> Value") + hmap.print() + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + name := hmap.get(15937) + fmt.Println("\n输入学号 15937 ,查询到姓名 ", name) + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + hmap.remove(12836) + fmt.Println("\n删除 12836 后,哈希表为\nKey -> Value") + hmap.print() +} + +func TestHashMapOpenAddressing(t *testing.T) { + /* 初始化哈希表 */ + hmap := newHashMapOpenAddressing() + + /* 添加操作 */ + // 在哈希表中添加键值对 (key, value) + hmap.put(12836, "小哈") + hmap.put(15937, "小啰") + hmap.put(16750, "小算") + hmap.put(13276, "小法") + hmap.put(10583, "小鸭") + fmt.Println("\n添加完成后,哈希表为\nKey -> Value") + hmap.print() + + /* 查询操作 */ + // 向哈希表输入键 key ,得到值 value + name := hmap.get(13276) + fmt.Println("\n输入学号 13276 ,查询到姓名 ", name) + + /* 删除操作 */ + // 在哈希表中删除键值对 (key, value) + hmap.remove(16750) + fmt.Println("\n删除 16750 后,哈希表为\nKey -> Value") + hmap.print() +} diff --git a/codes/go/chapter_hashing/hash_map_chaining.go b/codes/go/chapter_hashing/hash_map_chaining.go index 002f1a778..c0b59ea73 100644 --- a/codes/go/chapter_hashing/hash_map_chaining.go +++ b/codes/go/chapter_hashing/hash_map_chaining.go @@ -89,10 +89,10 @@ func (m *hashMapChaining) remove(key int) { if p.key == key { // 切片删除 m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...) + m.size -= 1 break } } - m.size -= 1 } /* 扩容哈希表 */ diff --git a/codes/go/chapter_hashing/hash_map_test.go b/codes/go/chapter_hashing/hash_map_test.go index 69e7195a8..b4168fd0b 100644 --- a/codes/go/chapter_hashing/hash_map_test.go +++ b/codes/go/chapter_hashing/hash_map_test.go @@ -12,7 +12,7 @@ import ( . "github.com/krahets/hello-algo/pkg" ) -func TestHashmap(t *testing.T) { +func TestHashMap(t *testing.T) { /* 初始化哈希表 */ hmap := make(map[int]string) @@ -55,58 +55,6 @@ func TestHashmap(t *testing.T) { } } -func TestHashMapChaining(t *testing.T) { - /* 初始化哈希表 */ - hmap := newHashMapChaining() - - /* 添加操作 */ - // 在哈希表中添加键值对 (key, value) - hmap.put(12836, "小哈") - hmap.put(15937, "小啰") - hmap.put(16750, "小算") - hmap.put(13276, "小法") - hmap.put(10583, "小鸭") - fmt.Println("\n添加完成后,哈希表为\nKey -> Value") - hmap.print() - - /* 查询操作 */ - // 向哈希表输入键 key ,得到值 value - name := hmap.get(15937) - fmt.Println("\n输入学号 15937 ,查询到姓名 ", name) - - /* 删除操作 */ - // 在哈希表中删除键值对 (key, value) - hmap.remove(12836) - fmt.Println("\n删除 12836 后,哈希表为\nKey -> Value") - hmap.print() -} - -func TestHashMapOpenAddressing(t *testing.T) { - /* 初始化哈希表 */ - hmap := newHashMapOpenAddressing() - - /* 添加操作 */ - // 在哈希表中添加键值对 (key, value) - hmap.put(12836, "小哈") - hmap.put(15937, "小啰") - hmap.put(16750, "小算") - hmap.put(13276, "小法") - hmap.put(10583, "小鸭") - fmt.Println("\n添加完成后,哈希表为\nKey -> Value") - hmap.print() - - /* 查询操作 */ - // 向哈希表输入键 key ,得到值 value - name := hmap.get(13276) - fmt.Println("\n输入学号 13276 ,查询到姓名 ", name) - - /* 删除操作 */ - // 在哈希表中删除键值对 (key, value) - hmap.remove(16750) - fmt.Println("\n删除 16750 后,哈希表为\nKey -> Value") - hmap.print() -} - func TestSimpleHash(t *testing.T) { var hash int diff --git a/codes/java/chapter_hashing/hash_map_chaining.java b/codes/java/chapter_hashing/hash_map_chaining.java index d1e455fae..9f77421af 100644 --- a/codes/java/chapter_hashing/hash_map_chaining.java +++ b/codes/java/chapter_hashing/hash_map_chaining.java @@ -80,10 +80,12 @@ class HashMapChaining { List bucket = buckets.get(index); // 遍历桶,从中删除键值对 for (Pair pair : bucket) { - if (pair.key == key) + if (pair.key == key) { bucket.remove(pair); + size--; + break; + } } - size--; } /* 扩容哈希表 */ diff --git a/codes/python/chapter_hashing/hash_map_chaining.py b/codes/python/chapter_hashing/hash_map_chaining.py index 1667ad0d0..fdbaa28b6 100644 --- a/codes/python/chapter_hashing/hash_map_chaining.py +++ b/codes/python/chapter_hashing/hash_map_chaining.py @@ -66,7 +66,7 @@ class HashMapChaining: if pair.key == key: bucket.remove(pair) self.size -= 1 - return + break def extend(self): """扩容哈希表"""