Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -4,7 +4,7 @@
* Author: Justin (xiefahit@gmail.com)
*/
/* Key-value pair Number -> String */
/* Key-value pair: Number -> String */
class Pair {
constructor(key, val) {
this.key = key;
@@ -12,11 +12,11 @@ class Pair {
}
}
/* Hash table based on array implementation */
/* Hash table implemented with an array */
class ArrayHashMap {
#buckets;
constructor() {
// Initialize array with 100 buckets
// Initialize an array containing 100 buckets
this.#buckets = new Array(100).fill(null);
}
@@ -25,7 +25,7 @@ class ArrayHashMap {
return key % 100;
}
/* Query operation */
/* Lookup operation */
get(key) {
let index = this.#hashFunc(key);
let pair = this.#buckets[index];
@@ -33,7 +33,7 @@ class ArrayHashMap {
return pair.val;
}
/* Add operation */
/* Insert operation */
set(key, val) {
let index = this.#hashFunc(key);
this.#buckets[index] = new Pair(key, val);
@@ -42,7 +42,7 @@ class ArrayHashMap {
/* Remove operation */
delete(key) {
let index = this.#hashFunc(key);
// Set to null to represent deletion
// Set the bucket to null to mark it as deleted
this.#buckets[index] = null;
}
@@ -79,7 +79,7 @@ class ArrayHashMap {
return arr;
}
/* Print hash table */
/* Print the hash table */
print() {
let pairSet = this.entries();
for (const pair of pairSet) {
@@ -88,41 +88,41 @@ class ArrayHashMap {
}
}
/* Driver Code */
/* Initialize hash table */
/* Driver code */
/* Initialize the hash table */
const map = new ArrayHashMap();
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.set(12836, 'Xiao Ha');
map.set(15937, 'Xiao Luo');
map.set(16750, 'Xiao Suan');
map.set(13276, 'Xiao Fa');
map.set(10583, 'Xiao Ya');
console.info('\nAfter adding is complete, hash table is\nKey -> Value');
/* Insert operation */
// Insert the key-value pair (key, value) into the hash table
map.set(12836, '小哈');
map.set(15937, '小啰');
map.set(16750, '小算');
map.set(13276, '小法');
map.set(10583, '小鸭');
console.info('\nHash table after insertion:\nKey -> Value');
map.print();
/* Query operation */
// Input key into hash table to get value
/* Lookup operation */
// Look up key in the hash table to obtain value
let name = map.get(15937);
console.info('\nInput student ID 15937, query name ' + name);
console.info('\nLook up student ID 15937 to obtain the name ' + name);
/* Remove operation */
// Remove key-value pair (key, value) from hash table
// Remove the key-value pair (key, value) from the hash table
map.delete(10583);
console.info('\nAfter removing 10583, hash table is\nKey -> Value');
console.info('\nHash table after removing 10583:\nKey -> Value');
map.print();
/* Traverse hash table */
console.info('\nTraverse key-value pairs Key->Value');
/* Traverse the hash table */
console.info('\nTraverse key-value pairs: Key->Value');
for (const pair of map.entries()) {
if (!pair) continue;
console.info(pair.key + ' -> ' + pair.val);
}
console.info('\nTraverse keys only Key');
console.info('\nTraverse keys only: Key');
for (const key of map.keys()) {
console.info(key);
}
console.info('\nTraverse values only Value');
console.info('\nTraverse values only: Value');
for (const val of map.values()) {
console.info(val);
}

View File

@@ -4,41 +4,41 @@
* Author: Justin (xiefahit@gmail.com)
*/
/* Driver Code */
/* Initialize hash table */
/* Driver code */
/* Initialize the hash table */
const map = new Map();
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.set(12836, 'Xiao Ha');
map.set(15937, 'Xiao Luo');
map.set(16750, 'Xiao Suan');
map.set(13276, 'Xiao Fa');
map.set(10583, 'Xiao Ya');
console.info('\nAfter adding is complete, hash table is\nKey -> Value');
/* Insert operation */
// Insert the key-value pair (key, value) into the hash table
map.set(12836, '小哈');
map.set(15937, '小啰');
map.set(16750, '小算');
map.set(13276, '小法');
map.set(10583, '小鸭');
console.info('\nHash table after insertion:\nKey -> Value');
console.info(map);
/* Query operation */
// Input key into hash table to get value
/* Lookup operation */
// Look up key in the hash table to obtain value
let name = map.get(15937);
console.info('\nInput student ID 15937, query name ' + name);
console.info('\nLook up student ID 15937 to obtain the name ' + name);
/* Remove operation */
// Remove key-value pair (key, value) from hash table
// Remove the key-value pair (key, value) from the hash table
map.delete(10583);
console.info('\nAfter removing 10583, hash table is\nKey -> Value');
console.info('\nHash table after removing 10583:\nKey -> Value');
console.info(map);
/* Traverse hash table */
console.info('\nTraverse key-value pairs Key->Value');
/* Traverse the hash table */
console.info('\nTraverse key-value pairs: Key->Value');
for (const [k, v] of map.entries()) {
console.info(k + ' -> ' + v);
}
console.info('\nTraverse keys only Key');
console.info('\nTraverse keys only: Key');
for (const k of map.keys()) {
console.info(k);
}
console.info('\nTraverse values only Value');
console.info('\nTraverse values only: Value');
for (const v of map.values()) {
console.info(v);
}

View File

@@ -4,7 +4,7 @@
* Author: yuan0221 (yl1452491917@gmail.com)
*/
/* Key-value pair Number -> String */
/* Key-value pair: Number -> String */
class Pair {
constructor(key, val) {
this.key = key;
@@ -16,8 +16,8 @@ class Pair {
class HashMapChaining {
#size; // Number of key-value pairs
#capacity; // Hash table capacity
#loadThres; // Load factor threshold for triggering expansion
#extendRatio; // Expansion multiplier
#loadThres; // Load factor threshold that triggers capacity growth
#extendRatio; // Capacity growth factor
#buckets; // Bucket array
/* Constructor */
@@ -39,36 +39,36 @@ class HashMapChaining {
return this.#size / this.#capacity;
}
/* Query operation */
/* Lookup operation */
get(key) {
const index = this.#hashFunc(key);
const bucket = this.#buckets[index];
// Traverse bucket, if key is found, return corresponding val
// Traverse the bucket; if key is found, return its corresponding val
for (const pair of bucket) {
if (pair.key === key) {
return pair.val;
}
}
// If key is not found, return null
// Return null if key was not found
return null;
}
/* Add operation */
/* Insert operation */
put(key, val) {
// When load factor exceeds threshold, perform expansion
// Grow the capacity when the load factor exceeds the threshold
if (this.#loadFactor() > this.#loadThres) {
this.#extend();
}
const index = this.#hashFunc(key);
const bucket = this.#buckets[index];
// Traverse bucket, if specified key is encountered, update corresponding val and return
// Traverse the bucket; if the specified key is found, update its val and return
for (const pair of bucket) {
if (pair.key === key) {
pair.val = val;
return;
}
}
// If key does not exist, append key-value pair to the end
// If key is absent, append the key-value pair at the end
const pair = new Pair(key, val);
bucket.push(pair);
this.#size++;
@@ -78,7 +78,7 @@ class HashMapChaining {
remove(key) {
const index = this.#hashFunc(key);
let bucket = this.#buckets[index];
// Traverse bucket and remove key-value pair from it
// Traverse the bucket and remove the key-value pair
for (let i = 0; i < bucket.length; i++) {
if (bucket[i].key === key) {
bucket.splice(i, 1);
@@ -88,15 +88,15 @@ class HashMapChaining {
}
}
/* Expand hash table */
/* Grow the hash table capacity */
#extend() {
// Temporarily store the original hash table
// Keep a reference to the original hash table
const bucketsTmp = this.#buckets;
// Initialize expanded new hash table
// Initialize a new hash table with the expanded capacity
this.#capacity *= this.#extendRatio;
this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);
this.#size = 0;
// Move key-value pairs from original hash table to new hash table
// Move the key-value pairs from the original hash table to the new one
for (const bucket of bucketsTmp) {
for (const pair of bucket) {
this.put(pair.key, pair.val);
@@ -104,7 +104,7 @@ class HashMapChaining {
}
}
/* Print hash table */
/* Print the hash table */
print() {
for (const bucket of this.#buckets) {
let res = [];
@@ -116,27 +116,27 @@ class HashMapChaining {
}
}
/* Driver Code */
/* Initialize hash table */
/* Driver code */
/* Initialize the hash table */
const map = new HashMapChaining();
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.put(12836, 'Xiao Ha');
map.put(15937, 'Xiao Luo');
map.put(16750, 'Xiao Suan');
map.put(13276, 'Xiao Fa');
map.put(10583, 'Xiao Ya');
console.log('\nAfter adding is complete, hash table is\nKey -> Value');
/* Insert operation */
// Insert the key-value pair (key, value) into the hash table
map.put(12836, '小哈');
map.put(15937, '小啰');
map.put(16750, '小算');
map.put(13276, '小法');
map.put(10583, '小鸭');
console.log('\nHash table after insertion:\nKey -> Value');
map.print();
/* Query operation */
// Input key into hash table to get value
/* Lookup operation */
// Look up key in the hash table to obtain value
const name = map.get(13276);
console.log('\nInput student ID 13276, query name ' + name);
console.log('\nLook up student ID 13276 to obtain the name ' + name);
/* Remove operation */
// Remove key-value pair (key, value) from hash table
// Remove the key-value pair (key, value) from the hash table
map.remove(12836);
console.log('\nAfter removing 12836, hash table is\nKey -> Value');
console.log('\nHash table after removing 12836:\nKey -> Value');
map.print();

View File

@@ -4,7 +4,7 @@
* Author: yuan0221 (yl1452491917@gmail.com), krahets (krahets@163.com)
*/
/* Key-value pair Number -> String */
/* Key-value pair: Number -> String */
class Pair {
constructor(key, val) {
this.key = key;
@@ -16,19 +16,19 @@ class Pair {
class HashMapOpenAddressing {
#size; // Number of key-value pairs
#capacity; // Hash table capacity
#loadThres; // Load factor threshold for triggering expansion
#extendRatio; // Expansion multiplier
#loadThres; // Load factor threshold that triggers capacity growth
#extendRatio; // Capacity growth factor
#buckets; // Bucket array
#TOMBSTONE; // Removal marker
#TOMBSTONE; // Tombstone marker
/* Constructor */
constructor() {
this.#size = 0; // Number of key-value pairs
this.#capacity = 4; // Hash table capacity
this.#loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion
this.#extendRatio = 2; // Expansion multiplier
this.#loadThres = 2.0 / 3.0; // Load factor threshold that triggers capacity growth
this.#extendRatio = 2; // Capacity growth factor
this.#buckets = Array(this.#capacity).fill(null); // Bucket array
this.#TOMBSTONE = new Pair(-1, '-1'); // Removal marker
this.#TOMBSTONE = new Pair(-1, '-1'); // Tombstone marker
}
/* Hash function */
@@ -41,60 +41,60 @@ class HashMapOpenAddressing {
return this.#size / this.#capacity;
}
/* Search for bucket index corresponding to key */
/* Find the bucket index for key */
#findBucket(key) {
let index = this.#hashFunc(key);
let firstTombstone = -1;
// Linear probing, break when encountering an empty bucket
// Probe linearly until an empty bucket is reached
while (this.#buckets[index] !== null) {
// If key is encountered, return the corresponding bucket index
// If key is found, return its bucket index
if (this.#buckets[index].key === key) {
// If a removal marker was encountered before, move the key-value pair to that index
// If a tombstone was encountered earlier, move the key-value pair to that index
if (firstTombstone !== -1) {
this.#buckets[firstTombstone] = this.#buckets[index];
this.#buckets[index] = this.#TOMBSTONE;
return firstTombstone; // Return the moved bucket index
return firstTombstone; // Return the bucket index after the move
}
return index; // Return bucket index
return index; // Return the bucket index
}
// Record the first removal marker encountered
// Record the first tombstone encountered
if (
firstTombstone === -1 &&
this.#buckets[index] === this.#TOMBSTONE
) {
firstTombstone = index;
}
// Calculate bucket index, wrap around to the head if past the tail
// Calculate the bucket index, wrapping to the beginning after the end
index = (index + 1) % this.#capacity;
}
// If key does not exist, return the index for insertion
// If key is absent, return the index for insertion
return firstTombstone === -1 ? index : firstTombstone;
}
/* Query operation */
/* Lookup operation */
get(key) {
// Search for bucket index corresponding to key
// Find the bucket index for key
const index = this.#findBucket(key);
// If key-value pair is found, return corresponding val
// If the key-value pair is found, return its val
if (
this.#buckets[index] !== null &&
this.#buckets[index] !== this.#TOMBSTONE
) {
return this.#buckets[index].val;
}
// If key-value pair does not exist, return null
// Return null if the key-value pair does not exist
return null;
}
/* Add operation */
/* Insert operation */
put(key, val) {
// When load factor exceeds threshold, perform expansion
// Grow the capacity when the load factor exceeds the threshold
if (this.#loadFactor() > this.#loadThres) {
this.#extend();
}
// Search for bucket index corresponding to key
// Find the bucket index for key
const index = this.#findBucket(key);
// If key-value pair is found, overwrite val and return
// If the key-value pair is found, overwrite val and return
if (
this.#buckets[index] !== null &&
this.#buckets[index] !== this.#TOMBSTONE
@@ -102,16 +102,16 @@ class HashMapOpenAddressing {
this.#buckets[index].val = val;
return;
}
// If key-value pair does not exist, add the key-value pair
// If the key-value pair is absent, insert it
this.#buckets[index] = new Pair(key, val);
this.#size++;
}
/* Remove operation */
remove(key) {
// Search for bucket index corresponding to key
// Find the bucket index for key
const index = this.#findBucket(key);
// If key-value pair is found, overwrite it with removal marker
// If the key-value pair is found, replace it with a tombstone
if (
this.#buckets[index] !== null &&
this.#buckets[index] !== this.#TOMBSTONE
@@ -121,15 +121,15 @@ class HashMapOpenAddressing {
}
}
/* Expand hash table */
/* Grow the hash table capacity */
#extend() {
// Temporarily store the original hash table
// Keep a reference to the original hash table
const bucketsTmp = this.#buckets;
// Initialize expanded new hash table
// Initialize a new hash table with the expanded capacity
this.#capacity *= this.#extendRatio;
this.#buckets = Array(this.#capacity).fill(null);
this.#size = 0;
// Move key-value pairs from original hash table to new hash table
// Move the key-value pairs from the original hash table to the new one
for (const pair of bucketsTmp) {
if (pair !== null && pair !== this.#TOMBSTONE) {
this.put(pair.key, pair.val);
@@ -137,7 +137,7 @@ class HashMapOpenAddressing {
}
}
/* Print hash table */
/* Print the hash table */
print() {
for (const pair of this.#buckets) {
if (pair === null) {
@@ -151,27 +151,27 @@ class HashMapOpenAddressing {
}
}
/* Driver Code */
// Initialize hash table
/* Driver code */
// Initialize the hash table
const hashmap = new HashMapOpenAddressing();
// Add operation
// Add key-value pair (key, val) to the hash table
hashmap.put(12836, 'Xiao Ha');
hashmap.put(15937, 'Xiao Luo');
hashmap.put(16750, 'Xiao Suan');
hashmap.put(13276, 'Xiao Fa');
hashmap.put(10583, 'Xiao Ya');
console.log('\nAfter adding is complete, hash table is\nKey -> Value');
// Insert operation
// Insert the key-value pair (key, val) into the hash table
hashmap.put(12836, '小哈');
hashmap.put(15937, '小啰');
hashmap.put(16750, '小算');
hashmap.put(13276, '小法');
hashmap.put(10583, '小鸭');
console.log('\nHash table after insertion:\nKey -> Value');
hashmap.print();
// Query operation
// Input key into hash table to get value val
// Lookup operation
// Look up key in the hash table to obtain val
const name = hashmap.get(13276);
console.log('\nInput student ID 13276, query name ' + name);
console.log('\nLook up student ID 13276 to obtain the name ' + name);
// Remove operation
// Remove key-value pair (key, val) from hash table
// Remove the key-value pair (key, val) from the hash table
hashmap.remove(16750);
console.log('\nAfter removing 16750, hash table is\nKey -> Value');
console.log('\nHash table after removing 16750:\nKey -> Value');
hashmap.print();

View File

@@ -34,7 +34,7 @@ function xorHash(key) {
return hash % MODULUS;
}
/* Rotational hash */
/* Rotating hash */
function rotHash(key) {
let hash = 0;
const MODULUS = 1000000007;
@@ -44,17 +44,17 @@ function rotHash(key) {
return hash;
}
/* Driver Code */
const key = 'Hello Algo';
/* Driver code */
const key = 'Hello 算法';
let hash = addHash(key);
console.log('Additive hash value is ' + hash);
console.log('Additive hash value: ' + hash);
hash = mulHash(key);
console.log('Multiplicative hash value is ' + hash);
console.log('Multiplicative hash value: ' + hash);
hash = xorHash(key);
console.log('XOR hash value is ' + hash);
console.log('XOR hash value: ' + hash);
hash = rotHash(key);
console.log('Rotational hash value is ' + hash);
console.log('Rotating hash value: ' + hash);