Skip to content

Commit 4c9e5cc

Browse files
authored
Merge pull request krahets#174 from justin-tse/dev-js-hash
Add the JavaScript code and docs for Chapter of Hash Map
2 parents ac90a0f + 60c715b commit 4c9e5cc

File tree

3 files changed

+244
-1
lines changed

3 files changed

+244
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* File: array_hash_map.js
3+
* Created Time: 2022-12-26
4+
* Author: Justin ([email protected])
5+
*/
6+
7+
/* 键值对 Number -> String */
8+
class Entry {
9+
constructor(key, val) {
10+
this.key = key;
11+
this.val = val;
12+
}
13+
}
14+
15+
/* 基于数组简易实现的哈希表 */
16+
class ArrayHashMap {
17+
#bucket;
18+
constructor() {
19+
// 初始化一个长度为 100 的桶(数组)
20+
this.#bucket = new Array(100).fill(null);
21+
}
22+
23+
/* 哈希函数 */
24+
#hashFunc(key) {
25+
return key % 100;
26+
}
27+
28+
/* 查询操作 */
29+
get(key) {
30+
let index = this.#hashFunc(key);
31+
let entry = this.#bucket[index];
32+
if (entry === null) return null;
33+
return entry.val;
34+
}
35+
36+
/* 添加操作 */
37+
set(key, val) {
38+
let index = this.#hashFunc(key);
39+
this.#bucket[index] = new Entry(key, val);
40+
}
41+
42+
/* 删除操作 */
43+
delete(key) {
44+
let index = this.#hashFunc(key);
45+
// 置为 null ,代表删除
46+
this.#bucket[index] = null;
47+
}
48+
49+
/* 获取所有键值对 */
50+
entries() {
51+
let arr = [];
52+
for (let i = 0; i < this.#bucket.length; i++) {
53+
if (this.#bucket[i]) {
54+
arr.push(this.#bucket[i]);
55+
}
56+
}
57+
return arr;
58+
}
59+
60+
/* 获取所有键 */
61+
keys() {
62+
let arr = [];
63+
for (let i = 0; i < this.#bucket.length; i++) {
64+
if (this.#bucket[i]) {
65+
arr.push(this.#bucket[i]?.key);
66+
}
67+
}
68+
return arr;
69+
}
70+
71+
/* 获取所有值 */
72+
values() {
73+
let arr = [];
74+
for (let i = 0; i < this.#bucket.length; i++) {
75+
if (this.#bucket[i]) {
76+
arr.push(this.#bucket[i]?.val);
77+
}
78+
}
79+
return arr;
80+
}
81+
82+
/* 打印哈希表 */
83+
print() {
84+
let entrySet = this.entries();
85+
for (const entry of entrySet) {
86+
if (!entry) continue;
87+
console.info(`${entry.key} -> ${entry.val}`);
88+
}
89+
}
90+
}
91+
92+
/* Driver Code */
93+
/* 初始化哈希表 */
94+
const map = new ArrayHashMap();
95+
/* 添加操作 */
96+
// 在哈希表中添加键值对 (key, value)
97+
map.set(12836, '小哈');
98+
map.set(15937, '小啰');
99+
map.set(16750, '小算');
100+
map.set(13276, '小法');
101+
map.set(10583, '小鸭');
102+
console.info('\n添加完成后,哈希表为\nKey -> Value');
103+
map.print();
104+
105+
/* 查询操作 */
106+
// 向哈希表输入键 key ,得到值 value
107+
let name = map.get(15937);
108+
console.info('\n输入学号 15937 ,查询到姓名 ' + name);
109+
110+
/* 删除操作 */
111+
// 在哈希表中删除键值对 (key, value)
112+
map.delete(10583);
113+
console.info('\n删除 10583 后,哈希表为\nKey -> Value');
114+
map.print();
115+
116+
/* 遍历哈希表 */
117+
console.info('\n遍历键值对 Key->Value');
118+
for (const entry of map.entries()) {
119+
if (!entry) continue;
120+
console.info(entry.key + ' -> ' + entry.val);
121+
}
122+
console.info('\n单独遍历键 Key');
123+
for (const key of map.keys()) {
124+
console.info(key);
125+
}
126+
console.info('\n单独遍历值 Value');
127+
for (const val of map.values()) {
128+
console.info(val);
129+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* File: hash_map.js
3+
* Created Time: 2022-12-26
4+
* Author: Justin ([email protected])
5+
*/
6+
7+
/* Driver Code */
8+
/* 初始化哈希表 */
9+
const map = new Map();
10+
11+
/* 添加操作 */
12+
// 在哈希表中添加键值对 (key, value)
13+
map.set(12836, '小哈');
14+
map.set(15937, '小啰');
15+
map.set(16750, '小算');
16+
map.set(13276, '小法');
17+
map.set(10583, '小鸭');
18+
console.info('\n添加完成后,哈希表为\nKey -> Value');
19+
console.info(map);
20+
21+
/* 查询操作 */
22+
// 向哈希表输入键 key ,得到值 value
23+
let name = map.get(15937);
24+
console.info('\n输入学号 15937 ,查询到姓名 ' + name);
25+
26+
/* 删除操作 */
27+
// 在哈希表中删除键值对 (key, value)
28+
map.delete(10583);
29+
console.info('\n删除 10583 后,哈希表为\nKey -> Value');
30+
console.info(map);
31+
32+
/* 遍历哈希表 */
33+
console.info('\n遍历键值对 Key->Value');
34+
for (const [k, v] of map.entries()) {
35+
console.info(k + ' -> ' + v);
36+
}
37+
console.info('\n单独遍历键 Key');
38+
for (const k of map.keys()) {
39+
console.info(k);
40+
}
41+
console.info('\n单独遍历值 Value');
42+
for (const v of map.values()) {
43+
console.info(v);
44+
}

docs/chapter_hashing/hash_map.md

+71-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,23 @@ comments: true
132132
=== "JavaScript"
133133

134134
```js title="hash_map.js"
135+
/* 初始化哈希表 */
136+
const map = new ArrayHashMap();
137+
/* 添加操作 */
138+
// 在哈希表中添加键值对 (key, value)
139+
map.set(12836, '小哈');
140+
map.set(15937, '小啰');
141+
map.set(16750, '小算');
142+
map.set(13276, '小法');
143+
map.set(10583, '小鸭');
144+
145+
/* 查询操作 */
146+
// 向哈希表输入键 key ,得到值 value
147+
let name = map.get(15937);
135148

149+
/* 删除操作 */
150+
// 在哈希表中删除键值对 (key, value)
151+
map.delete(10583);
136152
```
137153

138154
=== "TypeScript"
@@ -265,7 +281,20 @@ comments: true
265281
=== "JavaScript"
266282

267283
```js title="hash_map.js"
268-
284+
/* 遍历哈希表 */
285+
// 遍历键值对 key->value
286+
for (const entry of map.entries()) {
287+
if (!entry) continue;
288+
console.info(entry.key + ' -> ' + entry.val);
289+
}
290+
// 单独遍历键 key
291+
for (const key of map.keys()) {
292+
console.info(key);
293+
}
294+
// 单独遍历值 value
295+
for (const val of map.values()) {
296+
console.info(val);
297+
}
269298
```
270299

271300
=== "TypeScript"
@@ -533,7 +562,48 @@ $$
533562
=== "JavaScript"
534563

535564
```js title="array_hash_map.js"
565+
/* 键值对 Number -> String */
566+
class Entry {
567+
constructor(key, val) {
568+
this.key = key;
569+
this.val = val;
570+
}
571+
}
572+
573+
/* 基于数组简易实现的哈希表 */
574+
class ArrayHashMap {
575+
#bucket;
576+
constructor() {
577+
// 初始化一个长度为 100 的桶(数组)
578+
this.#bucket = new Array(100).fill(null);
579+
}
580+
581+
/* 哈希函数 */
582+
#hashFunc(key) {
583+
return key % 100;
584+
}
536585

586+
/* 查询操作 */
587+
get(key) {
588+
let index = this.#hashFunc(key);
589+
let entry = this.#bucket[index];
590+
if (entry === null) return null;
591+
return entry.val;
592+
}
593+
594+
/* 添加操作 */
595+
set(key, val) {
596+
let index = this.#hashFunc(key);
597+
this.#bucket[index] = new Entry(key, val);
598+
}
599+
600+
/* 删除操作 */
601+
delete(key) {
602+
let index = this.#hashFunc(key);
603+
// 置为 null ,代表删除
604+
this.#bucket[index] = null;
605+
}
606+
}
537607
```
538608

539609
=== "TypeScript"

0 commit comments

Comments
 (0)