mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 22:16:29 +08:00
Fixed some problems, indentation and Pointer
This commit is contained in:
parent
148dbaa0a1
commit
1229f98e92
9 changed files with 185 additions and 175 deletions
|
@ -15,17 +15,16 @@ public:
|
||||||
this->key = key;
|
this->key = key;
|
||||||
this->val = val;
|
this->val = val;
|
||||||
}
|
}
|
||||||
Entry()=default;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* 基于数组简易实现的哈希表 */
|
/* 基于数组简易实现的哈希表 */
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 哈希函数 */
|
/* 哈希函数 */
|
||||||
|
@ -37,13 +36,16 @@ public:
|
||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
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;
|
if (pair == nullptr) {
|
||||||
|
return "Not Found";
|
||||||
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
@ -52,14 +54,14 @@ public:
|
||||||
void remove(int key) {
|
void remove(int key) {
|
||||||
int index = hashFunc(key);
|
int index = hashFunc(key);
|
||||||
// 置为空字符,代表删除
|
// 置为空字符,代表删除
|
||||||
bucket[index] = *new Entry();
|
bucket[index] = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 获取所有键值对 */
|
/* 获取所有键值对 */
|
||||||
vector<Entry> entrySet() {
|
vector<Entry*> entrySet() {
|
||||||
vector<Entry> entrySet;
|
vector<Entry*> entrySet;
|
||||||
for (Entry pair: bucket) {
|
for (Entry* pair: bucket) {
|
||||||
if (pair.key != 0) {
|
if (pair != nullptr) {
|
||||||
entrySet.push_back(pair);
|
entrySet.push_back(pair);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,9 +71,9 @@ public:
|
||||||
/* 获取所有键 */
|
/* 获取所有键 */
|
||||||
vector<int> keySet() {
|
vector<int> keySet() {
|
||||||
vector<int> keySet;
|
vector<int> keySet;
|
||||||
for (Entry pair: bucket) {
|
for (Entry* pair: bucket) {
|
||||||
if (pair.key != 0) {
|
if (pair != nullptr) {
|
||||||
keySet.push_back(pair.key);
|
keySet.push_back(pair->key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return keySet;
|
return keySet;
|
||||||
|
@ -80,17 +82,18 @@ public:
|
||||||
/* 获取所有值 */
|
/* 获取所有值 */
|
||||||
vector<string> valueSet() {
|
vector<string> valueSet() {
|
||||||
vector<string> valueSet;
|
vector<string> valueSet;
|
||||||
for (Entry pair: bucket) {
|
for (Entry* pair: bucket) {
|
||||||
if (pair.key != 0)
|
if (pair != nullptr){
|
||||||
valueSet.push_back(pair.val);
|
valueSet.push_back(pair->val);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return valueSet;
|
return valueSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 打印哈希表 */
|
/* 打印哈希表 */
|
||||||
void print() {
|
void print() {
|
||||||
for (Entry kv: entrySet()) {
|
for (Entry* kv: entrySet()) {
|
||||||
cout << kv.key << " -> " << kv.val << endl;
|
cout << kv->key << " -> " << kv->val << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -122,7 +125,7 @@ int main() {
|
||||||
/* 遍历哈希表 */
|
/* 遍历哈希表 */
|
||||||
cout << "\n遍历键值对 Key->Value" << endl;
|
cout << "\n遍历键值对 Key->Value" << endl;
|
||||||
for (auto kv: map.entrySet()) {
|
for (auto kv: map.entrySet()) {
|
||||||
cout << kv.key << " -> " << kv.val << endl;
|
cout << kv->key << " -> " << kv->val << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "\n单独遍历键 Key" << endl;
|
cout << "\n单独遍历键 Key" << endl;
|
||||||
|
|
|
@ -6,90 +6,90 @@ package chapter_hashing
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
// 键值对 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取所有键对
|
/* 获取所有键对 */
|
||||||
func (a *ArrayHashMap) entrySet() []Entry {
|
func (a *ArrayHashMap) entrySet() []*Entry {
|
||||||
var pairs []Entry
|
var pairs []*Entry
|
||||||
for _, pair := range a.bucket {
|
for _, pair := range a.bucket {
|
||||||
if pair.key != 0 {
|
if pair != nil {
|
||||||
pairs = append(pairs, pair)
|
pairs = append(pairs, pair)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return pairs
|
return pairs
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取所有键
|
/* 获取所有键 */
|
||||||
func (a *ArrayHashMap) keySet() []int {
|
func (a *ArrayHashMap) keySet() []int {
|
||||||
var keys []int
|
var keys []int
|
||||||
for _, pair := range a.bucket {
|
for _, pair := range a.bucket {
|
||||||
if pair.key != 0 {
|
if pair != nil {
|
||||||
keys = append(keys, pair.key)
|
keys = append(keys, pair.key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取所有值
|
/* 获取所有值 */
|
||||||
func (a *ArrayHashMap) valueSet() []string {
|
func (a *ArrayHashMap) valueSet() []string {
|
||||||
var values []string
|
var values []string
|
||||||
for _, pair := range a.bucket {
|
for _, pair := range a.bucket {
|
||||||
if pair.key != 0 {
|
if pair != nil {
|
||||||
values = append(values, pair.val)
|
values = append(values, pair.val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return values
|
return values
|
||||||
}
|
}
|
||||||
|
|
||||||
// 打印哈希表
|
/* 打印哈希表 */
|
||||||
func (a *ArrayHashMap) print() {
|
func (a *ArrayHashMap) print() {
|
||||||
for _, pair := range a.bucket {
|
for _, pair := range a.bucket {
|
||||||
if pair.key != 0 {
|
if pair != nil {
|
||||||
fmt.Println(pair.key, "->", pair.val)
|
fmt.Println(pair.key, "->", pair.val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,42 +11,43 @@ import (
|
||||||
|
|
||||||
func TestArrayHashMap(t *testing.T) {
|
func TestArrayHashMap(t *testing.T) {
|
||||||
/* 初始化哈希表 */
|
/* 初始化哈希表 */
|
||||||
map1 := newArrayHashMap()
|
mapp := newArrayHashMap()
|
||||||
|
|
||||||
/* 添加操作 */
|
/* 添加操作 */
|
||||||
// 在哈希表中添加键值对 (key, value)
|
// 在哈希表中添加键值对 (key, value)
|
||||||
map1.put(12836, "小哈")
|
mapp.put(12836, "小哈")
|
||||||
map1.put(15937, "小啰")
|
mapp.put(15937, "小啰")
|
||||||
map1.put(16750, "小算")
|
mapp.put(16750, "小算")
|
||||||
map1.put(13276, "小法")
|
mapp.put(13276, "小法")
|
||||||
map1.put(10583, "小鸭")
|
mapp.put(10583, "小鸭")
|
||||||
fmt.Println("\n添加完成后,哈希表为\nKey -> Value")
|
fmt.Println("\n添加完成后,哈希表为\nKey -> Value")
|
||||||
map1.print()
|
mapp.print()
|
||||||
|
|
||||||
/* 查询操作 */
|
/* 查询操作 */
|
||||||
// 向哈希表输入键 key ,得到值 value
|
// 向哈希表输入键 key ,得到值 value
|
||||||
name := map1.get(15937)
|
name := mapp.get(15937)
|
||||||
fmt.Println("\n输入学号 15937 ,查询到姓名 " + name)
|
fmt.Println("\n输入学号 15937 ,查询到姓名 " + name)
|
||||||
|
|
||||||
/* 删除操作 */
|
/* 删除操作 */
|
||||||
// 在哈希表中删除键值对 (key, value)
|
// 在哈希表中删除键值对 (key, value)
|
||||||
map1.remove(10583)
|
mapp.remove(10583)
|
||||||
fmt.Println("\n删除 10583 后,哈希表为\nKey -> Value")
|
fmt.Println("\n删除 10583 后,哈希表为\nKey -> Value")
|
||||||
map1.print()
|
mapp.print()
|
||||||
|
|
||||||
/* 遍历哈希表 */
|
/* 遍历哈希表 */
|
||||||
fmt.Println("\n遍历键值对 Key->Value")
|
fmt.Println("\n遍历键值对 Key->Value")
|
||||||
for _, kv := range map1.entrySet() {
|
for _, kv := range mapp.entrySet() {
|
||||||
fmt.Println(kv.key, " -> ", kv.val)
|
fmt.Println(kv.key, " -> ", kv.val)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("\n单独遍历键 Key")
|
fmt.Println("\n单独遍历键 Key")
|
||||||
for _, key := range map1.keySet() {
|
for _, key := range mapp.keySet() {
|
||||||
fmt.Println(key)
|
fmt.Println(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("\n单独遍历值 Value")
|
fmt.Println("\n单独遍历值 Value")
|
||||||
for _, val := range map1.valueSet() {
|
for _, val := range mapp.valueSet() {
|
||||||
fmt.Println(val)
|
fmt.Println(val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,48 +10,48 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHashmap(t *testing.T) {
|
func TestHashmap(t *testing.T) {
|
||||||
// 初始化哈希表
|
/* 初始化哈希表 */
|
||||||
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] = "小鸭"
|
||||||
fmt.Println("\n添加完成后,哈希表为\nKey -> Value")
|
fmt.Println("\n添加完成后,哈希表为\nKey -> Value")
|
||||||
for key, value := range Map {
|
for key, value := range mapp {
|
||||||
fmt.Printf("%d -> %s\n", key, value)
|
fmt.Printf("%d -> %s\n", key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询操作
|
/* 查询操作 */
|
||||||
// 向哈希表输入键 key ,得到值 value
|
// 向哈希表输入键 key ,得到值 value
|
||||||
name := Map[15937]
|
name := mapp[15937]
|
||||||
fmt.Println("\n输入学号 15937 ,查询到姓名 ", name)
|
fmt.Println("\n输入学号 15937 ,查询到姓名 ", name)
|
||||||
|
|
||||||
// 删除操作
|
/* 删除操作 */
|
||||||
// 在哈希表中删除键值对 (key, value)
|
// 在哈希表中删除键值对 (key, value)
|
||||||
delete(Map, 10583)
|
delete(mapp, 10583)
|
||||||
fmt.Println("\n删除 10583 后,哈希表为\nKey -> Value")
|
fmt.Println("\n删除 10583 后,哈希表为\nKey -> Value")
|
||||||
for key, value := range Map {
|
for key, value := range mapp {
|
||||||
fmt.Printf("%d -> %s\n", key, value)
|
fmt.Printf("%d -> %s\n", key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 遍历哈希表 */
|
/* 遍历哈希表 */
|
||||||
// 遍历键值对 key->value
|
// 遍历键值对 key->value
|
||||||
fmt.Println("\n遍历键值对 Key->Value")
|
fmt.Println("\n遍历键值对 Key->Value")
|
||||||
for key, value := range Map {
|
for key, value := range mapp {
|
||||||
fmt.Println(key, "->", value)
|
fmt.Println(key, "->", value)
|
||||||
}
|
}
|
||||||
// 单独遍历键 key
|
// 单独遍历键 key
|
||||||
fmt.Println("\n单独遍历键 Key")
|
fmt.Println("\n单独遍历键 Key")
|
||||||
for key := range Map {
|
for key := range mapp {
|
||||||
fmt.Println(key)
|
fmt.Println(key)
|
||||||
}
|
}
|
||||||
// 单独遍历值 value
|
// 单独遍历值 value
|
||||||
fmt.Println("\n单独遍历值 Value")
|
fmt.Println("\n单独遍历值 Value")
|
||||||
for _, value := range Map {
|
for _, value := range mapp {
|
||||||
fmt.Println(value)
|
fmt.Println(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,25 +4,25 @@ Created Time: 2022-12-14
|
||||||
Author: msk397 (machangxinq@gmail.com)
|
Author: msk397 (machangxinq@gmail.com)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# 键值对 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]
|
||||||
|
@ -30,19 +30,19 @@ class ArrayHashMap:
|
||||||
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)
|
||||||
# 置为空字符,代表删除
|
# 置为空字符,代表删除
|
||||||
self.bucket[index] = None
|
self.bucket[index] = None
|
||||||
|
|
||||||
# 获取所有键值对
|
""" 获取所有键值对 """
|
||||||
def entrySet(self):
|
def entrySet(self):
|
||||||
result = []
|
result = []
|
||||||
for pair in self.bucket:
|
for pair in self.bucket:
|
||||||
|
@ -50,7 +50,7 @@ class ArrayHashMap:
|
||||||
result.append(pair)
|
result.append(pair)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# 获取所有键
|
""" 获取所有键 """
|
||||||
def keySet(self):
|
def keySet(self):
|
||||||
result = []
|
result = []
|
||||||
for pair in self.bucket:
|
for pair in self.bucket:
|
||||||
|
@ -58,7 +58,7 @@ class ArrayHashMap:
|
||||||
result.append(pair.key)
|
result.append(pair.key)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# 获取所有值
|
""" 获取所有值 """
|
||||||
def valueSet(self):
|
def valueSet(self):
|
||||||
result = []
|
result = []
|
||||||
for pair in self.bucket:
|
for pair in self.bucket:
|
||||||
|
@ -66,7 +66,7 @@ class ArrayHashMap:
|
||||||
result.append(pair.val)
|
result.append(pair.val)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# 打印哈希表
|
""" 打印哈希表 """
|
||||||
def print(self):
|
def print(self):
|
||||||
for pair in self.bucket:
|
for pair in self.bucket:
|
||||||
if pair is not None:
|
if pair is not None:
|
||||||
|
@ -75,38 +75,38 @@ class ArrayHashMap:
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
""" 初始化哈希表 """
|
""" 初始化哈希表 """
|
||||||
Map = ArrayHashMap()
|
mapp = ArrayHashMap()
|
||||||
|
|
||||||
""" 添加操作 """
|
""" 添加操作 """
|
||||||
# 在哈希表中添加键值对 (key, value)
|
# 在哈希表中添加键值对 (key, value)
|
||||||
Map.put(12836, "小哈")
|
mapp.put(12836, "小哈")
|
||||||
Map.put(15937, "小啰")
|
mapp.put(15937, "小啰")
|
||||||
Map.put(16750, "小算")
|
mapp.put(16750, "小算")
|
||||||
Map.put(13276, "小法")
|
mapp.put(13276, "小法")
|
||||||
Map.put(10583, "小鸭")
|
mapp.put(10583, "小鸭")
|
||||||
print("\n添加完成后,哈希表为\nKey -> Value")
|
print("\n添加完成后,哈希表为\nKey -> Value")
|
||||||
Map.print()
|
mapp.print()
|
||||||
|
|
||||||
""" 查询操作 """
|
""" 查询操作 """
|
||||||
# 向哈希表输入键 key ,得到值 value
|
# 向哈希表输入键 key ,得到值 value
|
||||||
name = Map.get(15937)
|
name = mapp.get(15937)
|
||||||
print("\n输入学号 15937 ,查询到姓名 " + name)
|
print("\n输入学号 15937 ,查询到姓名 " + name)
|
||||||
|
|
||||||
""" 删除操作 """
|
""" 删除操作 """
|
||||||
# 在哈希表中删除键值对 (key, value)
|
# 在哈希表中删除键值对 (key, value)
|
||||||
Map.remove(10583)
|
mapp.remove(10583)
|
||||||
print("\n删除 10583 后,哈希表为\nKey -> Value")
|
print("\n删除 10583 后,哈希表为\nKey -> Value")
|
||||||
Map.print()
|
mapp.print()
|
||||||
|
|
||||||
""" 遍历哈希表 """
|
""" 遍历哈希表 """
|
||||||
print("\n遍历键值对 Key->Value")
|
print("\n遍历键值对 Key->Value")
|
||||||
for pair in Map.entrySet():
|
for pair in mapp.entrySet():
|
||||||
print(pair.key, "->", pair.val)
|
print(pair.key, "->", pair.val)
|
||||||
|
|
||||||
print("\n单独遍历键 Key")
|
print("\n单独遍历键 Key")
|
||||||
for key in Map.keySet():
|
for key in mapp.keySet():
|
||||||
print(key)
|
print(key)
|
||||||
|
|
||||||
print("\n单独遍历值 Value")
|
print("\n单独遍历值 Value")
|
||||||
for val in Map.valueSet():
|
for val in mapp.valueSet():
|
||||||
print(val)
|
print(val)
|
|
@ -4,43 +4,44 @@ Created Time: 2022-12-14
|
||||||
Author: msk397 (machangxinq@gmail.com)
|
Author: msk397 (machangxinq@gmail.com)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import sys, os.path as osp
|
||||||
|
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||||
|
from include import *
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
""" 初始化哈希表 """
|
""" 初始化哈希表 """
|
||||||
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] = "小鸭"
|
||||||
print("\n添加完成后,哈希表为\nKey -> Value")
|
print("\n添加完成后,哈希表为\nKey -> Value")
|
||||||
for key, value in Map.items():
|
print_dict(mapp)
|
||||||
print(key, "->", value)
|
|
||||||
|
|
||||||
""" 查询操作 """
|
""" 查询操作 """
|
||||||
# 向哈希表输入键 key ,得到值 value
|
# 向哈希表输入键 key ,得到值 value
|
||||||
name = Map[15937]
|
name = mapp[15937]
|
||||||
print("\n输入学号 15937 ,查询到姓名 " + name)
|
print("\n输入学号 15937 ,查询到姓名 " + name)
|
||||||
|
|
||||||
""" 删除操作 """
|
""" 删除操作 """
|
||||||
# 在哈希表中删除键值对 (key, value)
|
# 在哈希表中删除键值对 (key, value)
|
||||||
Map.pop(10583)
|
mapp.pop(10583)
|
||||||
print("\n删除 10583 后,哈希表为\nKey -> Value")
|
print("\n删除 10583 后,哈希表为\nKey -> Value")
|
||||||
for key, value in Map.items():
|
print_dict(mapp)
|
||||||
print(key, "->", value)
|
|
||||||
|
|
||||||
""" 遍历哈希表 """
|
""" 遍历哈希表 """
|
||||||
print("\n遍历键值对 Key->Value")
|
print("\n遍历键值对 Key->Value")
|
||||||
for key, value in Map.items():
|
for key, value in mapp.items():
|
||||||
print(key, "->", value)
|
print(key, "->", value)
|
||||||
|
|
||||||
print("\n单独遍历键 Key")
|
print("\n单独遍历键 Key")
|
||||||
for key in Map.keys():
|
for key in mapp.keys():
|
||||||
print(key)
|
print(key)
|
||||||
|
|
||||||
print("\n单独遍历值 Value")
|
print("\n单独遍历值 Value")
|
||||||
for val in Map.values():
|
for val in mapp.values():
|
||||||
print(val)
|
print(val)
|
||||||
|
|
||||||
|
|
|
@ -7,4 +7,4 @@ import collections
|
||||||
from typing import List
|
from typing import List
|
||||||
from .linked_list import ListNode, list_to_linked_list, linked_list_to_list, get_list_node
|
from .linked_list import ListNode, list_to_linked_list, linked_list_to_list, get_list_node
|
||||||
from .binary_tree import TreeNode, list_to_tree, tree_to_list, get_tree_node
|
from .binary_tree import TreeNode, list_to_tree, tree_to_list, get_tree_node
|
||||||
from .print_util import print_matrix, print_linked_list, print_tree
|
from .print_util import print_matrix, print_linked_list, print_tree, print_dict
|
|
@ -1,7 +1,7 @@
|
||||||
'''
|
'''
|
||||||
File: print_util.py
|
File: print_util.py
|
||||||
Created Time: 2021-12-11
|
Created Time: 2021-12-11
|
||||||
Author: Krahets (krahets@163.com)
|
Author: Krahets (krahets@163.com), msk397 (machangxinq@gmail.com)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
@ -72,3 +72,12 @@ def print_tree(root, prev=None, isLeft=False):
|
||||||
prev.str = prev_str
|
prev.str = prev_str
|
||||||
trunk.str = ' |'
|
trunk.str = ' |'
|
||||||
print_tree(root.left, trunk, False)
|
print_tree(root.left, trunk, False)
|
||||||
|
|
||||||
|
def print_dict(d):
|
||||||
|
"""Print a dict
|
||||||
|
|
||||||
|
Args:
|
||||||
|
d ([type]): [description]
|
||||||
|
"""
|
||||||
|
for key, value in d.items():
|
||||||
|
print(key, '->', value)
|
|
@ -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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue