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

@@ -19,12 +19,12 @@ class Pair {
}
}
/* Hash table based on array implementation */
/* Hash table implemented with an array */
class ArrayHashMap {
private List<Pair> buckets;
public ArrayHashMap() {
// Initialize array with 100 buckets
// Initialize an array containing 100 buckets
buckets = new ArrayList<>();
for (int i = 0; i < 100; i++) {
buckets.add(null);
@@ -37,7 +37,7 @@ class ArrayHashMap {
return index;
}
/* Query operation */
/* Lookup operation */
public String get(int key) {
int index = hashFunc(key);
Pair pair = buckets.get(index);
@@ -46,7 +46,7 @@ class ArrayHashMap {
return pair.val;
}
/* Add operation */
/* Insert operation */
public void put(int key, String val) {
Pair pair = new Pair(key, val);
int index = hashFunc(key);
@@ -56,7 +56,7 @@ class ArrayHashMap {
/* Remove operation */
public void remove(int key) {
int index = hashFunc(key);
// Set to null to represent deletion
// Set the bucket to null to mark it as removed
buckets.set(index, null);
}
@@ -90,7 +90,7 @@ class ArrayHashMap {
return valueSet;
}
/* Print hash table */
/* Print the hash table */
public void print() {
for (Pair kv : pairSet()) {
System.out.println(kv.key + " -> " + kv.val);
@@ -100,40 +100,40 @@ class ArrayHashMap {
public class array_hash_map {
public static void main(String[] args) {
/* Initialize hash table */
/* Initialize the hash table */
ArrayHashMap map = new ArrayHashMap();
/* 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");
System.out.println("\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, "小鸭");
System.out.println("\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
String name = map.get(15937);
System.out.println("\nInput student ID 15937, query name " + name);
System.out.println("\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.remove(10583);
System.out.println("\nAfter removing 10583, hash table is\nKey -> Value");
System.out.println("\nHash table after removing 10583:\nKey -> Value");
map.print();
/* Traverse hash table */
System.out.println("\nTraverse key-value pairs Key->Value");
/* Traverse the hash table */
System.out.println("\nTraverse key-value pairs: Key->Value");
for (Pair kv : map.pairSet()) {
System.out.println(kv.key + " -> " + kv.val);
}
System.out.println("\nTraverse keys only Key");
System.out.println("\nTraverse keys only: Key");
for (int key : map.keySet()) {
System.out.println(key);
}
System.out.println("\nTraverse values only Value");
System.out.println("\nTraverse values only: Value");
for (String val : map.valueSet()) {
System.out.println(val);
}

View File

@@ -13,26 +13,26 @@ public class built_in_hash {
public static void main(String[] args) {
int num = 3;
int hashNum = Integer.hashCode(num);
System.out.println("Hash value of integer " + num + " is " + hashNum);
System.out.println("Integer " + num + " has hash value " + hashNum);
boolean bol = true;
int hashBol = Boolean.hashCode(bol);
System.out.println("Hash value of boolean " + bol + " is " + hashBol);
System.out.println("Boolean " + bol + " has hash value " + hashBol);
double dec = 3.14159;
int hashDec = Double.hashCode(dec);
System.out.println("Hash value of decimal " + dec + " is " + hashDec);
System.out.println("Decimal " + dec + " has hash value " + hashDec);
String str = "Hello Algo";
String str = "Hello 算法";
int hashStr = str.hashCode();
System.out.println("Hash value of string " + str + " is " + hashStr);
System.out.println("String " + str + " has hash value " + hashStr);
Object[] arr = { 12836, "Xiao Ha" };
Object[] arr = { 12836, "小哈" };
int hashTup = Arrays.hashCode(arr);
System.out.println("Hash value of array " + Arrays.toString(arr) + " is " + hashTup);
System.out.println("Array " + Arrays.toString(arr) + " has hash value " + hashTup);
ListNode obj = new ListNode(0);
int hashObj = obj.hashCode();
System.out.println("Hash value of node object " + obj + " is " + hashObj);
System.out.println("Node object " + obj + " has hash value " + hashObj);
}
}

View File

@@ -11,40 +11,40 @@ import utils.*;
public class hash_map {
public static void main(String[] args) {
/* Initialize hash table */
/* Initialize the hash table */
Map<Integer, String> map = new HashMap<>();
/* 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");
System.out.println("\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, "小鸭");
System.out.println("\nHash table after insertion:\nKey -> Value");
PrintUtil.printHashMap(map);
/* Query operation */
// Input key into hash table to get value
/* Lookup operation */
// Look up key in the hash table to obtain value
String name = map.get(15937);
System.out.println("\nInput student ID 15937, query name " + name);
System.out.println("\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.remove(10583);
System.out.println("\nAfter removing 10583, hash table is\nKey -> Value");
System.out.println("\nHash table after removing 10583:\nKey -> Value");
PrintUtil.printHashMap(map);
/* Traverse hash table */
System.out.println("\nTraverse key-value pairs Key->Value");
/* Traverse the hash table */
System.out.println("\nTraverse key-value pairs: Key->Value");
for (Map.Entry<Integer, String> kv : map.entrySet()) {
System.out.println(kv.getKey() + " -> " + kv.getValue());
}
System.out.println("\nTraverse keys only Key");
System.out.println("\nTraverse keys only: Key");
for (int key : map.keySet()) {
System.out.println(key);
}
System.out.println("\nTraverse values only Value");
System.out.println("\nTraverse values only: Value");
for (String val : map.values()) {
System.out.println(val);
}

View File

@@ -13,8 +13,8 @@ import java.util.List;
class HashMapChaining {
int size; // Number of key-value pairs
int capacity; // Hash table capacity
double loadThres; // Load factor threshold for triggering expansion
int extendRatio; // Expansion multiplier
double loadThres; // Load factor threshold that triggers capacity growth
int extendRatio; // Capacity growth factor
List<List<Pair>> buckets; // Bucket array
/* Constructor */
@@ -39,36 +39,36 @@ class HashMapChaining {
return (double) size / capacity;
}
/* Query operation */
/* Lookup operation */
String get(int key) {
int index = hashFunc(key);
List<Pair> bucket = buckets.get(index);
// Traverse bucket, if key is found, return corresponding val
// Traverse the bucket; if key is found, return its corresponding val
for (Pair pair : bucket) {
if (pair.key == key) {
return pair.val;
}
}
// If key is not found, return null
// Return null if key is not found
return null;
}
/* Add operation */
/* Insert operation */
void put(int key, String val) {
// When load factor exceeds threshold, perform expansion
// Grow the capacity when the load factor exceeds the threshold
if (loadFactor() > loadThres) {
extend();
}
int index = hashFunc(key);
List<Pair> bucket = buckets.get(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 (Pair pair : 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
Pair pair = new Pair(key, val);
bucket.add(pair);
size++;
@@ -78,7 +78,7 @@ class HashMapChaining {
void remove(int key) {
int index = hashFunc(key);
List<Pair> bucket = buckets.get(index);
// Traverse bucket and remove key-value pair from it
// Traverse the bucket and remove the key-value pair
for (Pair pair : bucket) {
if (pair.key == key) {
bucket.remove(pair);
@@ -88,18 +88,18 @@ class HashMapChaining {
}
}
/* Expand hash table */
/* Grow the hash table capacity */
void extend() {
// Temporarily store the original hash table
// Keep a reference to the original hash table
List<List<Pair>> bucketsTmp = buckets;
// Initialize expanded new hash table
// Initialize a new hash table with the expanded capacity
capacity *= extendRatio;
buckets = new ArrayList<>(capacity);
for (int i = 0; i < capacity; i++) {
buckets.add(new ArrayList<>());
}
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 (List<Pair> bucket : bucketsTmp) {
for (Pair pair : bucket) {
put(pair.key, pair.val);
@@ -107,7 +107,7 @@ class HashMapChaining {
}
}
/* Print hash table */
/* Print the hash table */
void print() {
for (List<Pair> bucket : buckets) {
List<String> res = new ArrayList<>();
@@ -121,28 +121,28 @@ class HashMapChaining {
public class hash_map_chaining {
public static void main(String[] args) {
/* Initialize hash table */
/* Initialize the hash table */
HashMapChaining 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");
System.out.println("\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, "小鸭");
System.out.println("\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
String name = map.get(13276);
System.out.println("\nInput student ID 13276, query name " + name);
System.out.println("\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);
System.out.println("\nAfter removing 12836, hash table is\nKey -> Value");
System.out.println("\nHash table after removing 12836:\nKey -> Value");
map.print();
}
}

View File

@@ -10,10 +10,10 @@ package chapter_hashing;
class HashMapOpenAddressing {
private int size; // Number of key-value pairs
private int capacity = 4; // Hash table capacity
private final double loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion
private final int extendRatio = 2; // Expansion multiplier
private final double loadThres = 2.0 / 3.0; // Load factor threshold that triggers capacity growth
private final int extendRatio = 2; // Capacity growth factor
private Pair[] buckets; // Bucket array
private final Pair TOMBSTONE = new Pair(-1, "-1"); // Removal marker
private final Pair TOMBSTONE = new Pair(-1, "-1"); // Tombstone marker
/* Constructor */
public HashMapOpenAddressing() {
@@ -31,83 +31,83 @@ class HashMapOpenAddressing {
return (double) size / capacity;
}
/* Search for bucket index corresponding to key */
/* Find the bucket index for key */
private int findBucket(int key) {
int index = hashFunc(key);
int firstTombstone = -1;
// Linear probing, break when encountering an empty bucket
// Probe linearly until an empty bucket is reached
while (buckets[index] != null) {
// If key is encountered, return the corresponding bucket index
// If key is found, return its bucket index
if (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) {
buckets[firstTombstone] = buckets[index];
buckets[index] = 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 && buckets[index] == 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) % 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 */
public String get(int key) {
// Search for bucket index corresponding to key
// Find the bucket index for key
int index = findBucket(key);
// If key-value pair is found, return corresponding val
// If the key-value pair is found, return its val
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
return buckets[index].val;
}
// If key-value pair does not exist, return null
// Return null if the key-value pair is absent
return null;
}
/* Add operation */
/* Insert operation */
public void put(int key, String val) {
// When load factor exceeds threshold, perform expansion
// Grow the capacity when the load factor exceeds the threshold
if (loadFactor() > loadThres) {
extend();
}
// Search for bucket index corresponding to key
// Find the bucket index for key
int index = findBucket(key);
// If key-value pair is found, overwrite val and return
// If the key-value pair is found, overwrite val and return
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
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
buckets[index] = new Pair(key, val);
size++;
}
/* Remove operation */
public void remove(int key) {
// Search for bucket index corresponding to key
// Find the bucket index for key
int index = 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 (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index] = TOMBSTONE;
size--;
}
}
/* Expand hash table */
/* Grow the hash table capacity */
private void extend() {
// Temporarily store the original hash table
// Keep a reference to the original hash table
Pair[] bucketsTmp = buckets;
// Initialize expanded new hash table
// Initialize a new hash table with the expanded capacity
capacity *= extendRatio;
buckets = new Pair[capacity];
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 (Pair pair : bucketsTmp) {
if (pair != null && pair != TOMBSTONE) {
put(pair.key, pair.val);
@@ -115,7 +115,7 @@ class HashMapOpenAddressing {
}
}
/* Print hash table */
/* Print the hash table */
public void print() {
for (Pair pair : buckets) {
if (pair == null) {
@@ -131,28 +131,28 @@ class HashMapOpenAddressing {
public class hash_map_open_addressing {
public static void main(String[] args) {
// Initialize hash table
// Initialize the hash table
HashMapOpenAddressing 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");
System.out.println("\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, "小鸭");
System.out.println("\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
String name = hashmap.get(13276);
System.out.println("\nInput student ID 13276, query name " + name);
System.out.println("\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);
System.out.println("\nAfter removing 16750, hash table is\nKey -> Value");
System.out.println("\nHash table after removing 16750:\nKey -> Value");
hashmap.print();
}
}

View File

@@ -37,7 +37,7 @@ public class simple_hash {
return hash & MODULUS;
}
/* Rotational hash */
/* Rotating hash */
static int rotHash(String key) {
long hash = 0;
final int MODULUS = 1000000007;
@@ -48,18 +48,18 @@ public class simple_hash {
}
public static void main(String[] args) {
String key = "Hello Algo";
String key = "Hello 算法";
int hash = addHash(key);
System.out.println("Additive hash value is " + hash);
System.out.println("Additive hash value: " + hash);
hash = mulHash(key);
System.out.println("Multiplicative hash value is " + hash);
System.out.println("Multiplicative hash value: " + hash);
hash = xorHash(key);
System.out.println("XOR hash value is " + hash);
System.out.println("XOR hash value: " + hash);
hash = rotHash(key);
System.out.println("Rotational hash value is " + hash);
System.out.println("Rotating hash value: " + hash);
}
}