diff --git a/chapter_heap/heap/index.html b/chapter_heap/heap/index.html index f4279943d..e31e6c3f1 100644 --- a/chapter_heap/heap/index.html +++ b/chapter_heap/heap/index.html @@ -3934,8 +3934,8 @@
heap.cs
/* 初始化堆 */
 // 初始化小顶堆
 PriorityQueue<int, int> minHeap = new();
-// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
-PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));
+// 初始化大顶堆(使用 lambda 表达式修改 Comparer 即可)
+PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y.CompareTo(x)));
 
 /* 元素入堆 */
 maxHeap.Enqueue(1, 1);
diff --git a/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png b/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png
index 33fca976a..a9db1579d 100644
Binary files a/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png and b/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png differ
diff --git a/chapter_searching/replace_linear_by_hashing/index.html b/chapter_searching/replace_linear_by_hashing/index.html
index 8431c8f1b..975b796c5 100644
--- a/chapter_searching/replace_linear_by_hashing/index.html
+++ b/chapter_searching/replace_linear_by_hashing/index.html
@@ -4094,12 +4094,12 @@
 }
 
 /* 哈希表元素插入 */
-void insert(HashTable *h, int key, int val) {
-    HashTable *t = find(h, key);
+void insert(HashTable **h, int key, int val) {
+    HashTable *t = find(*h, key);
     if (t == NULL) {
         HashTable *tmp = malloc(sizeof(HashTable));
         tmp->key = key, tmp->val = val;
-        HASH_ADD_INT(h, key, tmp);
+        HASH_ADD_INT(*h, key, tmp);
     } else {
         t->val = val;
     }
@@ -4116,7 +4116,7 @@
             *returnSize = 2;
             return res;
         }
-        insert(hashtable, nums[i], i);
+        insert(&hashtable, nums[i], i);
     }
     *returnSize = 0;
     return NULL;
diff --git a/chapter_sorting/counting_sort/index.html b/chapter_sorting/counting_sort/index.html
index 38912c94e..b62c5d170 100644
--- a/chapter_sorting/counting_sort/index.html
+++ b/chapter_sorting/counting_sort/index.html
@@ -4463,8 +4463,9 @@
     // 使用结果数组 res 覆盖原数组 nums
     memcpy(nums, res, size * sizeof(int));
     // 5. 释放内存
-    free(counter);
-}
+    free(res);
+    free(counter);
+}
 
diff --git a/chapter_sorting/radix_sort/index.html b/chapter_sorting/radix_sort/index.html index dc44dde77..90c4d7f80 100644 --- a/chapter_sorting/radix_sort/index.html +++ b/chapter_sorting/radix_sort/index.html @@ -4198,48 +4198,52 @@ x_k = \lfloor\frac{x}{d^{k-1}}\rfloor \bmod d void countingSortDigit(int nums[], int size, int exp) { // 十进制的位范围为 0~9 ,因此需要长度为 10 的桶数组 int *counter = (int *)malloc((sizeof(int) * 10)); - // 统计 0~9 各数字的出现次数 - for (int i = 0; i < size; i++) { - // 获取 nums[i] 第 k 位,记为 d - int d = digit(nums[i], exp); - // 统计数字 d 的出现次数 - counter[d]++; - } - // 求前缀和,将“出现个数”转换为“数组索引” - for (int i = 1; i < 10; i++) { - counter[i] += counter[i - 1]; - } - // 倒序遍历,根据桶内统计结果,将各元素填入 res - int *res = (int *)malloc(sizeof(int) * size); - for (int i = size - 1; i >= 0; i--) { - int d = digit(nums[i], exp); - int j = counter[d] - 1; // 获取 d 在数组中的索引 j - res[j] = nums[i]; // 将当前元素填入索引 j - counter[d]--; // 将 d 的数量减 1 - } - // 使用结果覆盖原数组 nums - for (int i = 0; i < size; i++) { - nums[i] = res[i]; - } -} - -/* 基数排序 */ -void radixSort(int nums[], int size) { - // 获取数组的最大元素,用于判断最大位数 - int max = INT32_MIN; - for (size_t i = 0; i < size - 1; i++) { - if (nums[i] > max) { - max = nums[i]; - } - } - // 按照从低位到高位的顺序遍历 - for (int exp = 1; max >= exp; exp *= 10) - // 对数组元素的第 k 位执行计数排序 - // k = 1 -> exp = 1 - // k = 2 -> exp = 10 - // 即 exp = 10^(k-1) - countingSortDigit(nums, size, exp); -} + memset(counter, 0, sizeof(int) * 10); // 初始化为 0 以支持后续内存释放 + // 统计 0~9 各数字的出现次数 + for (int i = 0; i < size; i++) { + // 获取 nums[i] 第 k 位,记为 d + int d = digit(nums[i], exp); + // 统计数字 d 的出现次数 + counter[d]++; + } + // 求前缀和,将“出现个数”转换为“数组索引” + for (int i = 1; i < 10; i++) { + counter[i] += counter[i - 1]; + } + // 倒序遍历,根据桶内统计结果,将各元素填入 res + int *res = (int *)malloc(sizeof(int) * size); + for (int i = size - 1; i >= 0; i--) { + int d = digit(nums[i], exp); + int j = counter[d] - 1; // 获取 d 在数组中的索引 j + res[j] = nums[i]; // 将当前元素填入索引 j + counter[d]--; // 将 d 的数量减 1 + } + // 使用结果覆盖原数组 nums + for (int i = 0; i < size; i++) { + nums[i] = res[i]; + } + // 释放内存 + free(res); + free(counter); +} + +/* 基数排序 */ +void radixSort(int nums[], int size) { + // 获取数组的最大元素,用于判断最大位数 + int max = INT32_MIN; + for (int i = 0; i < size; i++) { + if (nums[i] > max) { + max = nums[i]; + } + } + // 按照从低位到高位的顺序遍历 + for (int exp = 1; max >= exp; exp *= 10) + // 对数组元素的第 k 位执行计数排序 + // k = 1 -> exp = 1 + // k = 2 -> exp = 10 + // 即 exp = 10^(k-1) + countingSortDigit(nums, size, exp); +}
diff --git a/chapter_tree/array_representation_of_tree/index.html b/chapter_tree/array_representation_of_tree/index.html index 6e1b69238..799269f21 100644 --- a/chapter_tree/array_representation_of_tree/index.html +++ b/chapter_tree/array_representation_of_tree/index.html @@ -4632,59 +4632,52 @@ /* 层序遍历 */ fn level_order(&self) -> Vec<i32> { - let mut res = vec![]; - // 直接遍历数组 - for i in 0..self.size() { - if let Some(val) = self.val(i) { - res.push(val) - } - } - res - } - - /* 深度优先遍历 */ - fn dfs(&self, i: i32, order: &str, res: &mut Vec<i32>) { - if self.val(i).is_none() { - return; - } - let val = self.val(i).unwrap(); - // 前序遍历 - if order == "pre" { - res.push(val); - } - self.dfs(self.left(i), order, res); - // 中序遍历 - if order == "in" { - res.push(val); - } - self.dfs(self.right(i), order, res); - // 后序遍历 - if order == "post" { - res.push(val); - } + self.tree.iter().filter_map(|&x| x).collect() + } + + /* 深度优先遍历 */ + fn dfs(&self, i: i32, order: &'static str, res: &mut Vec<i32>) { + if self.val(i).is_none() { + return; + } + let val = self.val(i).unwrap(); + // 前序遍历 + if order == "pre" { + res.push(val); + } + self.dfs(self.left(i), order, res); + // 中序遍历 + if order == "in" { + res.push(val); + } + self.dfs(self.right(i), order, res); + // 后序遍历 + if order == "post" { + res.push(val); + } + } + + /* 前序遍历 */ + fn pre_order(&self) -> Vec<i32> { + let mut res = vec![]; + self.dfs(0, "pre", &mut res); + res } - /* 前序遍历 */ - fn pre_order(&self) -> Vec<i32> { + /* 中序遍历 */ + fn in_order(&self) -> Vec<i32> { let mut res = vec![]; - self.dfs(0, "pre", &mut res); + self.dfs(0, "in", &mut res); res } - /* 中序遍历 */ - fn in_order(&self) -> Vec<i32> { + /* 后序遍历 */ + fn post_order(&self) -> Vec<i32> { let mut res = vec![]; - self.dfs(0, "in", &mut res); + self.dfs(0, "post", &mut res); res } - - /* 后序遍历 */ - fn post_order(&self) -> Vec<i32> { - let mut res = vec![]; - self.dfs(0, "post", &mut res); - res - } -} +}
diff --git a/chapter_tree/avl_tree/index.html b/chapter_tree/avl_tree/index.html index 492176215..d84e650b5 100644 --- a/chapter_tree/avl_tree/index.html +++ b/chapter_tree/avl_tree/index.html @@ -4062,7 +4062,7 @@
/* AVL 树节点结构体 */
-TreeNode struct TreeNode {
+typedef struct TreeNode {
     int val;
     int height;
     struct TreeNode *left;
diff --git a/chapter_tree/binary_search_tree/index.html b/chapter_tree/binary_search_tree/index.html
index cecabc001..097e0d963 100644
--- a/chapter_tree/binary_search_tree/index.html
+++ b/chapter_tree/binary_search_tree/index.html
@@ -5127,7 +5127,7 @@
             // 删除节点 cur
             if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {
                 let left = pre.borrow().left.clone();
-                if left.is_some() && Rc::ptr_eq(&left.as_ref().unwrap(), &cur) {
+                if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) {
                     pre.borrow_mut().left = child;
                 } else {
                     pre.borrow_mut().right = child;
@@ -5148,11 +5148,11 @@
                     break;
                 }
             }
-            let tmpval = tmp.unwrap().borrow().val;
+            let tmp_val = tmp.unwrap().borrow().val;
             // 递归删除节点 tmp
-            self.remove(tmpval);
+            self.remove(tmp_val);
             // 用 tmp 覆盖 cur
-            cur.borrow_mut().val = tmpval;
+            cur.borrow_mut().val = tmp_val;
         }
     }
 }
diff --git a/chapter_tree/binary_tree_traversal/index.html b/chapter_tree/binary_tree_traversal/index.html
index c46f015d4..78e66ad09 100644
--- a/chapter_tree/binary_tree_traversal/index.html
+++ b/chapter_tree/binary_tree_traversal/index.html
@@ -4392,40 +4392,56 @@
 fn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
     let mut result = vec![];
 
-    if let Some(node) = root {
-        // 访问优先级:根节点 -> 左子树 -> 右子树
-        result.push(node.borrow().val);
-        result.extend(pre_order(node.borrow().left.as_ref()));
-        result.extend(pre_order(node.borrow().right.as_ref()));
-    }
-    result
-}
-
-/* 中序遍历 */
-fn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
-    let mut result = vec![];
-
-    if let Some(node) = root {
-        // 访问优先级:左子树 -> 根节点 -> 右子树
-        result.extend(in_order(node.borrow().left.as_ref()));
-        result.push(node.borrow().val);
-        result.extend(in_order(node.borrow().right.as_ref()));
-    }
-    result
-}
-
-/* 后序遍历 */
-fn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
-    let mut result = vec![];
-
-    if let Some(node) = root {
-        // 访问优先级:左子树 -> 右子树 -> 根节点
-        result.extend(post_order(node.borrow().left.as_ref()));
-        result.extend(post_order(node.borrow().right.as_ref()));
-        result.push(node.borrow().val);
-    }
-    result
-}
+    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
+        if let Some(node) = root {
+            // 访问优先级:根节点 -> 左子树 -> 右子树
+            let node = node.borrow();
+            res.push(node.val);
+            dfs(node.left.as_ref(), res);
+            dfs(node.right.as_ref(), res);
+        }
+    }
+    dfs(root, &mut result);
+
+    result
+}
+
+/* 中序遍历 */
+fn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
+    let mut result = vec![];
+
+    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
+        if let Some(node) = root {
+            // 访问优先级:左子树 -> 根节点 -> 右子树
+            let node = node.borrow();
+            dfs(node.left.as_ref(), res);
+            res.push(node.val);
+            dfs(node.right.as_ref(), res);
+        }
+    }
+    dfs(root, &mut result);
+
+    result
+}
+
+/* 后序遍历 */
+fn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
+    let mut result = vec![];
+
+    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
+        if let Some(node) = root {
+            // 访问优先级:左子树 -> 右子树 -> 根节点
+            let node = node.borrow();
+            dfs(node.left.as_ref(), res);
+            dfs(node.right.as_ref(), res);
+            res.push(node.val);
+        }
+    }
+
+    dfs(root, &mut result);
+
+    result
+}
 
diff --git a/en/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png b/en/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png index 6e942957a..8481ea4ec 100644 Binary files a/en/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png and b/en/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png differ diff --git a/en/chapter_tree/index.html b/en/chapter_tree/index.html index 3234369fe..c41de2b45 100644 --- a/en/chapter_tree/index.html +++ b/en/chapter_tree/index.html @@ -3527,8 +3527,8 @@

Tree

Abstract

-

The towering tree, vibrant with it's deep roots and lush leaves, branches spreading wide.

-

It vividly illustrates the concept of divide-and-conquer in data.

+

The towering tree exudes a vibrant essence, boasting profound roots and abundant foliage, yet its branches are sparsely scattered, creating an ethereal aura.

+

It shows us the vivid form of divide-and-conquer in data.

Chapter contents