|
|
@ -86,47 +86,47 @@ comments: true
|
|
|
|
=== "Python"
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
|
|
```python title="hash_map.py"
|
|
|
|
```python title="hash_map.py"
|
|
|
|
# 初始化哈希表
|
|
|
|
""" 初始化哈希表 """
|
|
|
|
Map = {}
|
|
|
|
mapp = {}
|
|
|
|
|
|
|
|
|
|
|
|
# 添加操作
|
|
|
|
""" 添加操作 """
|
|
|
|
# 在哈希表中添加键值对 (key, value)
|
|
|
|
# 在哈希表中添加键值对 (key, value)
|
|
|
|
Map[12836] = "小哈"
|
|
|
|
mapp[12836] = "小哈"
|
|
|
|
Map[15937] = "小啰"
|
|
|
|
mapp[15937] = "小啰"
|
|
|
|
Map[16750] = "小算"
|
|
|
|
mapp[16750] = "小算"
|
|
|
|
Map[13276] = "小法"
|
|
|
|
mapp[13276] = "小法"
|
|
|
|
Map[10583] = "小鸭"
|
|
|
|
mapp[10583] = "小鸭"
|
|
|
|
|
|
|
|
|
|
|
|
# 查询操作
|
|
|
|
""" 查询操作 """
|
|
|
|
# 向哈希表输入键 key ,得到值 value
|
|
|
|
# 向哈希表输入键 key ,得到值 value
|
|
|
|
name = Map[15937]
|
|
|
|
name = mapp[15937]
|
|
|
|
|
|
|
|
|
|
|
|
# 删除操作
|
|
|
|
""" 删除操作 """
|
|
|
|
# 在哈希表中删除键值对 (key, value)
|
|
|
|
# 在哈希表中删除键值对 (key, value)
|
|
|
|
Map.pop(10583)
|
|
|
|
mapp.pop(10583)
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
|
|
```go title="hash_map_test.go"
|
|
|
|
```go title="hash_map_test.go"
|
|
|
|
// 初始化哈希表
|
|
|
|
/* 初始化哈希表 */
|
|
|
|
Map := make(map[int]string)
|
|
|
|
mapp := make(map[int]string)
|
|
|
|
|
|
|
|
|
|
|
|
// 添加操作
|
|
|
|
/* 添加操作 */
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
// 在哈希表中添加键值对 (key, value)
|
|
|
|
Map[12836] = "小哈"
|
|
|
|
mapp[12836] = "小哈"
|
|
|
|
Map[15937] = "小啰"
|
|
|
|
mapp[15937] = "小啰"
|
|
|
|
Map[16750] = "小算"
|
|
|
|
mapp[16750] = "小算"
|
|
|
|
Map[13276] = "小法"
|
|
|
|
mapp[13276] = "小法"
|
|
|
|
Map[10583] = "小鸭"
|
|
|
|
mapp[10583] = "小鸭"
|
|
|
|
|
|
|
|
|
|
|
|
// 查询操作
|
|
|
|
/* 查询操作 */
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
|
|
|
// 向哈希表输入键 key ,得到值 value
|
|
|
|
name := Map[15937]
|
|
|
|
name := mapp[15937]
|
|
|
|
|
|
|
|
|
|
|
|
// 删除操作
|
|
|
|
/* 删除操作 */
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
// 在哈希表中删除键值对 (key, value)
|
|
|
|
delete(Map, 10583)
|
|
|
|
delete(mapp, 10583)
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
=== "JavaScript"
|
|
|
@ -196,13 +196,13 @@ comments: true
|
|
|
|
```python title="hash_map.py"
|
|
|
|
```python title="hash_map.py"
|
|
|
|
""" 遍历哈希表 """
|
|
|
|
""" 遍历哈希表 """
|
|
|
|
# 遍历键值对 key->value
|
|
|
|
# 遍历键值对 key->value
|
|
|
|
for key, value in Map.items():
|
|
|
|
for key, value in mapp.items():
|
|
|
|
print(key, "->", value)
|
|
|
|
print(key, "->", value)
|
|
|
|
# 单独遍历键 key
|
|
|
|
# 单独遍历键 key
|
|
|
|
for key in Map.keys():
|
|
|
|
for key in mapp.keys():
|
|
|
|
print(key)
|
|
|
|
print(key)
|
|
|
|
# 单独遍历值 value
|
|
|
|
# 单独遍历值 value
|
|
|
|
for value in Map.values():
|
|
|
|
for value in mapp.values():
|
|
|
|
print(value)
|
|
|
|
print(value)
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -211,15 +211,15 @@ comments: true
|
|
|
|
```go title="hash_map_test.go"
|
|
|
|
```go title="hash_map_test.go"
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
/* 遍历哈希表 */
|
|
|
|
// 遍历键值对 key->value
|
|
|
|
// 遍历键值对 key->value
|
|
|
|
for key, value := range Map {
|
|
|
|
for key, value := range mapp {
|
|
|
|
fmt.Println(key, "->", value)
|
|
|
|
fmt.Println(key, "->", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 单独遍历键 key
|
|
|
|
// 单独遍历键 key
|
|
|
|
for key := range Map {
|
|
|
|
for key := range mapp {
|
|
|
|
fmt.Println(key)
|
|
|
|
fmt.Println(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 单独遍历值 value
|
|
|
|
// 单独遍历值 value
|
|
|
|
for _, value := range Map {
|
|
|
|
for _, value := range mapp {
|
|
|
|
fmt.Println(value)
|
|
|
|
fmt.Println(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
@ -335,20 +335,16 @@ $$
|
|
|
|
this->key = key;
|
|
|
|
this->key = key;
|
|
|
|
this->val = val;
|
|
|
|
this->val = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Entry(){
|
|
|
|
|
|
|
|
this->key = -1;
|
|
|
|
|
|
|
|
this->val = "";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* 基于数组简易实现的哈希表 */
|
|
|
|
/* 基于数组简易实现的哈希表 */
|
|
|
|
class ArrayHashMap {
|
|
|
|
class ArrayHashMap {
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
vector<Entry> bucket;
|
|
|
|
vector<Entry*> bucket;
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
ArrayHashMap() {
|
|
|
|
ArrayHashMap() {
|
|
|
|
// 初始化一个长度为 100 的桶(数组)
|
|
|
|
// 初始化一个长度为 100 的桶(数组)
|
|
|
|
bucket= vector<Entry>(100);
|
|
|
|
bucket= vector<Entry*>(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 哈希函数 */
|
|
|
|
/* 哈希函数 */
|
|
|
@ -360,13 +356,13 @@ $$
|
|
|
|
/* 查询操作 */
|
|
|
|
/* 查询操作 */
|
|
|
|
string get(int key) {
|
|
|
|
string get(int key) {
|
|
|
|
int index = hashFunc(key);
|
|
|
|
int index = hashFunc(key);
|
|
|
|
Entry pair = bucket[index];
|
|
|
|
Entry* pair = bucket[index];
|
|
|
|
return pair.val;
|
|
|
|
return pair->val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
/* 添加操作 */
|
|
|
|
void put(int key, string val) {
|
|
|
|
void put(int key, string val) {
|
|
|
|
Entry pair = Entry(key, val);
|
|
|
|
Entry* pair = new Entry(key, val);
|
|
|
|
int index = hashFunc(key);
|
|
|
|
int index = hashFunc(key);
|
|
|
|
bucket[index] = pair;
|
|
|
|
bucket[index] = pair;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -374,8 +370,8 @@ $$
|
|
|
|
/* 删除操作 */
|
|
|
|
/* 删除操作 */
|
|
|
|
void remove(int key) {
|
|
|
|
void remove(int key) {
|
|
|
|
int index = hashFunc(key);
|
|
|
|
int index = hashFunc(key);
|
|
|
|
// 置为空,代表删除
|
|
|
|
// 置为空字符,代表删除
|
|
|
|
bucket[index] = *new Entry();
|
|
|
|
bucket[index] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
```
|
|
|
|
```
|
|
|
@ -383,24 +379,24 @@ $$
|
|
|
|
=== "Python"
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
|
|
```python title="array_hash_map.py"
|
|
|
|
```python title="array_hash_map.py"
|
|
|
|
# 键值对 int->String
|
|
|
|
""" 键值对 int->String """
|
|
|
|
class Entry:
|
|
|
|
class Entry:
|
|
|
|
def __init__(self, key, val):
|
|
|
|
def __init__(self, key, val):
|
|
|
|
self.key = key
|
|
|
|
self.key = key
|
|
|
|
self.val = val
|
|
|
|
self.val = val
|
|
|
|
|
|
|
|
|
|
|
|
# 基于数组简易实现的哈希表
|
|
|
|
""" 基于数组简易实现的哈希表 """
|
|
|
|
class ArrayHashMap:
|
|
|
|
class ArrayHashMap:
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self):
|
|
|
|
# 初始化一个长度为 100 的桶(数组)
|
|
|
|
# 初始化一个长度为 100 的桶(数组)
|
|
|
|
self.bucket = [None] * 100
|
|
|
|
self.bucket = [None] * 100
|
|
|
|
|
|
|
|
|
|
|
|
# 哈希函数
|
|
|
|
""" 哈希函数 """
|
|
|
|
def hashFunc(self, key):
|
|
|
|
def hashFunc(self, key):
|
|
|
|
index = key % 100
|
|
|
|
index = key % 100
|
|
|
|
return index
|
|
|
|
return index
|
|
|
|
|
|
|
|
|
|
|
|
# 查询操作
|
|
|
|
""" 查询操作 """
|
|
|
|
def get(self, key):
|
|
|
|
def get(self, key):
|
|
|
|
index = self.hashFunc(key)
|
|
|
|
index = self.hashFunc(key)
|
|
|
|
pair = self.bucket[index]
|
|
|
|
pair = self.bucket[index]
|
|
|
@ -408,13 +404,13 @@ $$
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
return pair.val
|
|
|
|
return pair.val
|
|
|
|
|
|
|
|
|
|
|
|
# 添加操作
|
|
|
|
""" 添加操作 """
|
|
|
|
def put(self, key, val):
|
|
|
|
def put(self, key, val):
|
|
|
|
pair = Entry(key, val)
|
|
|
|
pair = Entry(key, val)
|
|
|
|
index = self.hashFunc(key)
|
|
|
|
index = self.hashFunc(key)
|
|
|
|
self.bucket[index] = pair
|
|
|
|
self.bucket[index] = pair
|
|
|
|
|
|
|
|
|
|
|
|
# 删除操作
|
|
|
|
""" 删除操作 """
|
|
|
|
def remove(self, key):
|
|
|
|
def remove(self, key):
|
|
|
|
index = self.hashFunc(key)
|
|
|
|
index = self.hashFunc(key)
|
|
|
|
# 置为空字符,代表删除
|
|
|
|
# 置为空字符,代表删除
|
|
|
@ -424,51 +420,51 @@ $$
|
|
|
|
=== "Go"
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
|
|
```go title="array_hash_map.go"
|
|
|
|
```go title="array_hash_map.go"
|
|
|
|
// 键值对 int->String
|
|
|
|
/* 键值对 int->String */
|
|
|
|
type Entry struct {
|
|
|
|
type Entry struct {
|
|
|
|
key int
|
|
|
|
key int
|
|
|
|
val string
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 基于数组简易实现的哈希表
|
|
|
|
/* 基于数组简易实现的哈希表 */
|
|
|
|
type ArrayHashMap struct {
|
|
|
|
type ArrayHashMap struct {
|
|
|
|
bucket []Entry
|
|
|
|
bucket []*Entry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func newArrayHashMap() *ArrayHashMap {
|
|
|
|
func newArrayHashMap() *ArrayHashMap {
|
|
|
|
// 初始化一个长度为 100 的桶(数组)
|
|
|
|
// 初始化一个长度为 100 的桶(数组)
|
|
|
|
bucket := make([]Entry, 100)
|
|
|
|
bucket := make([]*Entry, 100)
|
|
|
|
return &ArrayHashMap{bucket: bucket}
|
|
|
|
return &ArrayHashMap{bucket: bucket}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 哈希函数
|
|
|
|
/* 哈希函数 */
|
|
|
|
func (a *ArrayHashMap) hashFunc(key int) int {
|
|
|
|
func (a *ArrayHashMap) hashFunc(key int) int {
|
|
|
|
index := key % 100
|
|
|
|
index := key % 100
|
|
|
|
return index
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查询操作
|
|
|
|
/* 查询操作 */
|
|
|
|
func (a *ArrayHashMap) get(key int) string {
|
|
|
|
func (a *ArrayHashMap) get(key int) string {
|
|
|
|
index := a.hashFunc(key)
|
|
|
|
index := a.hashFunc(key)
|
|
|
|
pair := a.bucket[index]
|
|
|
|
pair := a.bucket[index]
|
|
|
|
if pair.key == 0 {
|
|
|
|
if pair == nil {
|
|
|
|
return ""
|
|
|
|
return "Not Found"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pair.val
|
|
|
|
return pair.val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 添加操作
|
|
|
|
/* 添加操作 */
|
|
|
|
func (a *ArrayHashMap) put(key int, val string) {
|
|
|
|
func (a *ArrayHashMap) put(key int, val string) {
|
|
|
|
pair := Entry{key: key, val: val}
|
|
|
|
pair := &Entry{key: key, val: val}
|
|
|
|
index := a.hashFunc(key)
|
|
|
|
index := a.hashFunc(key)
|
|
|
|
a.bucket[index] = pair
|
|
|
|
a.bucket[index] = pair
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 删除操作
|
|
|
|
/* 删除操作 */
|
|
|
|
func (a *ArrayHashMap) remove(key int) {
|
|
|
|
func (a *ArrayHashMap) remove(key int) {
|
|
|
|
index := a.hashFunc(key)
|
|
|
|
index := a.hashFunc(key)
|
|
|
|
// 置为空字符,代表删除
|
|
|
|
// 置为空字符,代表删除
|
|
|
|
a.bucket[index] = Entry{}
|
|
|
|
a.bucket[index] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|