|
|
|
@ -472,13 +472,217 @@ comments: true
|
|
|
|
|
=== "JS"
|
|
|
|
|
|
|
|
|
|
```javascript title="hash_map_chaining.js"
|
|
|
|
|
[class]{HashMapChaining}-[func]{}
|
|
|
|
|
/* 链式地址哈希表 */
|
|
|
|
|
class HashMapChaining {
|
|
|
|
|
#size; // 键值对数量
|
|
|
|
|
#capacity; // 哈希表容量
|
|
|
|
|
#loadThres; // 触发扩容的负载因子阈值
|
|
|
|
|
#extendRatio; // 扩容倍数
|
|
|
|
|
#buckets; // 桶数组
|
|
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
|
constructor() {
|
|
|
|
|
this.#size = 0;
|
|
|
|
|
this.#capacity = 4;
|
|
|
|
|
this.#loadThres = 2 / 3.0;
|
|
|
|
|
this.#extendRatio = 2;
|
|
|
|
|
this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哈希函数 */
|
|
|
|
|
#hashFunc(key) {
|
|
|
|
|
return key % this.#capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 负载因子 */
|
|
|
|
|
#loadFactor() {
|
|
|
|
|
return this.#size / this.#capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
|
get(key) {
|
|
|
|
|
const index = this.#hashFunc(key);
|
|
|
|
|
const bucket = this.#buckets[index];
|
|
|
|
|
// 遍历桶,若找到 key 则返回对应 val
|
|
|
|
|
for (const pair of bucket) {
|
|
|
|
|
if (pair.key === key) {
|
|
|
|
|
return pair.val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 若未找到 key 则返回 null
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
|
put(key, val) {
|
|
|
|
|
// 当负载因子超过阈值时,执行扩容
|
|
|
|
|
if (this.#loadFactor() > this.#loadThres) {
|
|
|
|
|
this.#extend();
|
|
|
|
|
}
|
|
|
|
|
const index = this.#hashFunc(key);
|
|
|
|
|
const bucket = this.#buckets[index];
|
|
|
|
|
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
|
|
|
|
|
for (const pair of bucket) {
|
|
|
|
|
if (pair.key === key) {
|
|
|
|
|
pair.val = val;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 若无该 key ,则将键值对添加至尾部
|
|
|
|
|
const pair = new Pair(key, val);
|
|
|
|
|
bucket.push(pair);
|
|
|
|
|
this.#size++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
|
remove(key) {
|
|
|
|
|
const index = this.#hashFunc(key);
|
|
|
|
|
let bucket = this.#buckets[index];
|
|
|
|
|
// 遍历桶,从中删除键值对
|
|
|
|
|
for (let i = 0; i < bucket.length; i++) {
|
|
|
|
|
if (bucket[i].key === key) {
|
|
|
|
|
bucket.splice(i, 1);
|
|
|
|
|
this.size--;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 扩容哈希表 */
|
|
|
|
|
#extend() {
|
|
|
|
|
// 暂存原哈希表
|
|
|
|
|
const bucketsTmp = this.#buckets;
|
|
|
|
|
// 初始化扩容后的新哈希表
|
|
|
|
|
this.#capacity *= this.#extendRatio;
|
|
|
|
|
this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);
|
|
|
|
|
this.#size = 0;
|
|
|
|
|
// 将键值对从原哈希表搬运至新哈希表
|
|
|
|
|
for (const bucket of bucketsTmp) {
|
|
|
|
|
for (const pair of bucket) {
|
|
|
|
|
this.put(pair.key, pair.val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 打印哈希表 */
|
|
|
|
|
print() {
|
|
|
|
|
for (const bucket of this.#buckets) {
|
|
|
|
|
let res = [];
|
|
|
|
|
for (const pair of bucket) {
|
|
|
|
|
res.push(pair.key + ' -> ' + pair.val);
|
|
|
|
|
}
|
|
|
|
|
console.log(res);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TS"
|
|
|
|
|
|
|
|
|
|
```typescript title="hash_map_chaining.ts"
|
|
|
|
|
[class]{HashMapChaining}-[func]{}
|
|
|
|
|
/* 链式地址哈希表 */
|
|
|
|
|
class HashMapChaining {
|
|
|
|
|
#size: number; // 键值对数量
|
|
|
|
|
#capacity: number; // 哈希表容量
|
|
|
|
|
#loadThres: number; // 触发扩容的负载因子阈值
|
|
|
|
|
#extendRatio: number; // 扩容倍数
|
|
|
|
|
#buckets: Pair[][]; // 桶数组
|
|
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
|
constructor() {
|
|
|
|
|
this.#size = 0;
|
|
|
|
|
this.#capacity = 4;
|
|
|
|
|
this.#loadThres = 2 / 3.0;
|
|
|
|
|
this.#extendRatio = 2;
|
|
|
|
|
this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哈希函数 */
|
|
|
|
|
#hashFunc(key: number): number {
|
|
|
|
|
return key % this.#capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 负载因子 */
|
|
|
|
|
#loadFactor(): number {
|
|
|
|
|
return this.#size / this.#capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
|
get(key: number): string | null {
|
|
|
|
|
const index = this.#hashFunc(key);
|
|
|
|
|
const bucket = this.#buckets[index];
|
|
|
|
|
// 遍历桶,若找到 key 则返回对应 val
|
|
|
|
|
for (const pair of bucket) {
|
|
|
|
|
if (pair.key === key) {
|
|
|
|
|
return pair.val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 若未找到 key 则返回 null
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
|
put(key: number, val: string): void {
|
|
|
|
|
// 当负载因子超过阈值时,执行扩容
|
|
|
|
|
if (this.#loadFactor() > this.#loadThres) {
|
|
|
|
|
this.#extend();
|
|
|
|
|
}
|
|
|
|
|
const index = this.#hashFunc(key);
|
|
|
|
|
const bucket = this.#buckets[index];
|
|
|
|
|
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
|
|
|
|
|
for (const pair of bucket) {
|
|
|
|
|
if (pair.key === key) {
|
|
|
|
|
pair.val = val;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 若无该 key ,则将键值对添加至尾部
|
|
|
|
|
const pair = new Pair(key, val);
|
|
|
|
|
bucket.push(pair);
|
|
|
|
|
this.#size++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
|
remove(key: number): void {
|
|
|
|
|
const index = this.#hashFunc(key);
|
|
|
|
|
let bucket = this.#buckets[index];
|
|
|
|
|
// 遍历桶,从中删除键值对
|
|
|
|
|
for (let i = 0; i < bucket.length; i++) {
|
|
|
|
|
if (bucket[i].key === key) {
|
|
|
|
|
bucket.splice(i, 1);
|
|
|
|
|
this.#size--;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 扩容哈希表 */
|
|
|
|
|
#extend(): void {
|
|
|
|
|
// 暂存原哈希表
|
|
|
|
|
const bucketsTmp = this.#buckets;
|
|
|
|
|
// 初始化扩容后的新哈希表
|
|
|
|
|
this.#capacity *= this.#extendRatio;
|
|
|
|
|
this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);
|
|
|
|
|
this.#size = 0;
|
|
|
|
|
// 将键值对从原哈希表搬运至新哈希表
|
|
|
|
|
for (const bucket of bucketsTmp) {
|
|
|
|
|
for (const pair of bucket) {
|
|
|
|
|
this.put(pair.key, pair.val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 打印哈希表 */
|
|
|
|
|
print(): void {
|
|
|
|
|
for (const bucket of this.#buckets) {
|
|
|
|
|
let res = [];
|
|
|
|
|
for (const pair of bucket) {
|
|
|
|
|
res.push(pair.key + ' -> ' + pair.val);
|
|
|
|
|
}
|
|
|
|
|
console.log(res);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
@ -1455,13 +1659,257 @@ comments: true
|
|
|
|
|
=== "JS"
|
|
|
|
|
|
|
|
|
|
```javascript title="hash_map_open_addressing.js"
|
|
|
|
|
[class]{HashMapOpenAddressing}-[func]{}
|
|
|
|
|
/* 开放寻址哈希表 */
|
|
|
|
|
class HashMapOpenAddressing {
|
|
|
|
|
#size; // 键值对数量
|
|
|
|
|
#capacity; // 哈希表容量
|
|
|
|
|
#loadThres; // 触发扩容的负载因子阈值
|
|
|
|
|
#extendRatio; // 扩容倍数
|
|
|
|
|
#buckets; // 桶数组
|
|
|
|
|
#removed; // 删除标记
|
|
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
|
constructor() {
|
|
|
|
|
this.#size = 0;
|
|
|
|
|
this.#capacity = 4;
|
|
|
|
|
this.#loadThres = 2.0 / 3.0;
|
|
|
|
|
this.#extendRatio = 2;
|
|
|
|
|
this.#buckets = new Array(this.#capacity).fill(null);
|
|
|
|
|
this.#removed = new Pair(-1, '-1');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哈希函数 */
|
|
|
|
|
#hashFunc(key) {
|
|
|
|
|
return key % this.#capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 负载因子 */
|
|
|
|
|
#loadFactor() {
|
|
|
|
|
return this.#size / this.#capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
|
get(key) {
|
|
|
|
|
const index = this.#hashFunc(key);
|
|
|
|
|
// 线性探测,从 index 开始向后遍历
|
|
|
|
|
for (let i = 0; i < this.#capacity; i++) {
|
|
|
|
|
// 计算桶索引,越过尾部返回头部
|
|
|
|
|
const j = (index + i) % this.#capacity;
|
|
|
|
|
// 若遇到空桶,说明无此 key ,则返回 null
|
|
|
|
|
if (this.#buckets[j] === null) return null;
|
|
|
|
|
// 若遇到指定 key ,则返回对应 val
|
|
|
|
|
if (
|
|
|
|
|
this.#buckets[j].key === key &&
|
|
|
|
|
this.#buckets[j][key] !== this.#removed.key
|
|
|
|
|
)
|
|
|
|
|
return this.#buckets[j].val;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
|
put(key, val) {
|
|
|
|
|
// 当负载因子超过阈值时,执行扩容
|
|
|
|
|
if (this.#loadFactor() > this.#loadThres) {
|
|
|
|
|
this.#extend();
|
|
|
|
|
}
|
|
|
|
|
const index = this.#hashFunc(key);
|
|
|
|
|
// 线性探测,从 index 开始向后遍历
|
|
|
|
|
for (let i = 0; i < this.#capacity; i++) {
|
|
|
|
|
// 计算桶索引,越过尾部返回头部
|
|
|
|
|
let j = (index + i) % this.#capacity;
|
|
|
|
|
// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶
|
|
|
|
|
if (
|
|
|
|
|
this.#buckets[j] === null ||
|
|
|
|
|
this.#buckets[j][key] === this.#removed.key
|
|
|
|
|
) {
|
|
|
|
|
this.#buckets[j] = new Pair(key, val);
|
|
|
|
|
this.#size += 1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 若遇到指定 key ,则更新对应 val
|
|
|
|
|
if (this.#buckets[j].key === key) {
|
|
|
|
|
this.#buckets[j].val = val;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
|
remove(key) {
|
|
|
|
|
const index = this.#hashFunc(key);
|
|
|
|
|
// 线性探测,从 index 开始向后遍历
|
|
|
|
|
for (let i = 0; i < this.#capacity; i++) {
|
|
|
|
|
// 计算桶索引,越过尾部返回头部
|
|
|
|
|
const j = (index + i) % this.#capacity;
|
|
|
|
|
// 若遇到空桶,说明无此 key ,则直接返回
|
|
|
|
|
if (this.#buckets[j] === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 若遇到指定 key ,则标记删除并返回
|
|
|
|
|
if (this.#buckets[j].key === key) {
|
|
|
|
|
this.#buckets[j] = this.#removed;
|
|
|
|
|
this.#size -= 1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 扩容哈希表 */
|
|
|
|
|
#extend() {
|
|
|
|
|
// 暂存原哈希表
|
|
|
|
|
const bucketsTmp = this.#buckets;
|
|
|
|
|
// 初始化扩容后的新哈希表
|
|
|
|
|
this.#capacity *= this.#extendRatio;
|
|
|
|
|
this.#buckets = new Array(this.#capacity).fill(null);
|
|
|
|
|
this.#size = 0;
|
|
|
|
|
// 将键值对从原哈希表搬运至新哈希表
|
|
|
|
|
for (const pair of bucketsTmp) {
|
|
|
|
|
if (pair !== null && pair.key !== this.#removed.key) {
|
|
|
|
|
this.put(pair.key, pair.val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 打印哈希表 */
|
|
|
|
|
print() {
|
|
|
|
|
for (const pair of this.#buckets) {
|
|
|
|
|
if (pair !== null) {
|
|
|
|
|
console.log(pair.key + ' -> ' + pair.val);
|
|
|
|
|
} else {
|
|
|
|
|
console.log('null');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TS"
|
|
|
|
|
|
|
|
|
|
```typescript title="hash_map_open_addressing.ts"
|
|
|
|
|
[class]{HashMapOpenAddressing}-[func]{}
|
|
|
|
|
/* 开放寻址哈希表 */
|
|
|
|
|
class HashMapOpenAddressing {
|
|
|
|
|
#size: number; // 键值对数量
|
|
|
|
|
#capacity: number; // 哈希表容量
|
|
|
|
|
#loadThres: number; // 触发扩容的负载因子阈值
|
|
|
|
|
#extendRatio: number; // 扩容倍数
|
|
|
|
|
#buckets: Pair[]; // 桶数组
|
|
|
|
|
#removed: Pair; // 删除标记
|
|
|
|
|
|
|
|
|
|
/* 构造方法 */
|
|
|
|
|
constructor() {
|
|
|
|
|
this.#size = 0;
|
|
|
|
|
this.#capacity = 4;
|
|
|
|
|
this.#loadThres = 2.0 / 3.0;
|
|
|
|
|
this.#extendRatio = 2;
|
|
|
|
|
this.#buckets = new Array(this.#capacity).fill(null);
|
|
|
|
|
this.#removed = new Pair(-1, '-1');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 哈希函数 */
|
|
|
|
|
#hashFunc(key: number): number {
|
|
|
|
|
return key % this.#capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 负载因子 */
|
|
|
|
|
#loadFactor(): number {
|
|
|
|
|
return this.#size / this.#capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 查询操作 */
|
|
|
|
|
get(key: number): string | null {
|
|
|
|
|
const index = this.#hashFunc(key);
|
|
|
|
|
// 线性探测,从 index 开始向后遍历
|
|
|
|
|
for (let i = 0; i < this.#capacity; i++) {
|
|
|
|
|
// 计算桶索引,越过尾部返回头部
|
|
|
|
|
const j = (index + i) % this.#capacity;
|
|
|
|
|
// 若遇到空桶,说明无此 key ,则返回 null
|
|
|
|
|
if (this.#buckets[j] === null) return null;
|
|
|
|
|
// 若遇到指定 key ,则返回对应 val
|
|
|
|
|
if (
|
|
|
|
|
this.#buckets[j].key === key &&
|
|
|
|
|
this.#buckets[j][key] !== this.#removed.key
|
|
|
|
|
)
|
|
|
|
|
return this.#buckets[j].val;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 添加操作 */
|
|
|
|
|
put(key: number, val: string): void {
|
|
|
|
|
// 当负载因子超过阈值时,执行扩容
|
|
|
|
|
if (this.#loadFactor() > this.#loadThres) {
|
|
|
|
|
this.#extend();
|
|
|
|
|
}
|
|
|
|
|
const index = this.#hashFunc(key);
|
|
|
|
|
// 线性探测,从 index 开始向后遍历
|
|
|
|
|
for (let i = 0; i < this.#capacity; i++) {
|
|
|
|
|
// 计算桶索引,越过尾部返回头部
|
|
|
|
|
let j = (index + i) % this.#capacity;
|
|
|
|
|
// 若遇到空桶、或带有删除标记的桶,则将键值对放入该桶
|
|
|
|
|
if (
|
|
|
|
|
this.#buckets[j] === null ||
|
|
|
|
|
this.#buckets[j][key] === this.#removed.key
|
|
|
|
|
) {
|
|
|
|
|
this.#buckets[j] = new Pair(key, val);
|
|
|
|
|
this.#size += 1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 若遇到指定 key ,则更新对应 val
|
|
|
|
|
if (this.#buckets[j].key === key) {
|
|
|
|
|
this.#buckets[j].val = val;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除操作 */
|
|
|
|
|
remove(key: number): void {
|
|
|
|
|
const index = this.#hashFunc(key);
|
|
|
|
|
// 线性探测,从 index 开始向后遍历
|
|
|
|
|
for (let i = 0; i < this.#capacity; i++) {
|
|
|
|
|
// 计算桶索引,越过尾部返回头部
|
|
|
|
|
const j = (index + i) % this.#capacity;
|
|
|
|
|
// 若遇到空桶,说明无此 key ,则直接返回
|
|
|
|
|
if (this.#buckets[j] === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 若遇到指定 key ,则标记删除并返回
|
|
|
|
|
if (this.#buckets[j].key === key) {
|
|
|
|
|
this.#buckets[j] = this.#removed;
|
|
|
|
|
this.#size -= 1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 扩容哈希表 */
|
|
|
|
|
#extend(): void {
|
|
|
|
|
// 暂存原哈希表
|
|
|
|
|
const bucketsTmp = this.#buckets;
|
|
|
|
|
// 初始化扩容后的新哈希表
|
|
|
|
|
this.#capacity *= this.#extendRatio;
|
|
|
|
|
this.#buckets = new Array(this.#capacity).fill(null);
|
|
|
|
|
this.#size = 0;
|
|
|
|
|
// 将键值对从原哈希表搬运至新哈希表
|
|
|
|
|
for (const pair of bucketsTmp) {
|
|
|
|
|
if (pair !== null && pair.key !== this.#removed.key) {
|
|
|
|
|
this.put(pair.key, pair.val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 打印哈希表 */
|
|
|
|
|
print(): void {
|
|
|
|
|
for (const pair of this.#buckets) {
|
|
|
|
|
if (pair !== null) {
|
|
|
|
|
console.log(pair.key + ' -> ' + pair.val);
|
|
|
|
|
} else {
|
|
|
|
|
console.log('null');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|