diff --git a/codes/go/chapter_divide_and_conquer/binary_search_recur.go b/codes/go/chapter_divide_and_conquer/binary_search_recur.go index 7953a3a6c..df1946dc0 100644 --- a/codes/go/chapter_divide_and_conquer/binary_search_recur.go +++ b/codes/go/chapter_divide_and_conquer/binary_search_recur.go @@ -18,7 +18,7 @@ func dfs(nums []int, target, i, j int) int { // 递归子问题 f(m+1, j) return dfs(nums, target, m+1, j) } else if nums[m] > target { - // 小于则递归左半数组 + // 大于则递归左半数组 // 递归子问题 f(i, m-1) return dfs(nums, target, i, m-1) } else { diff --git a/codes/go/chapter_stack_and_queue/array_deque.go b/codes/go/chapter_stack_and_queue/array_deque.go index 85d98aca5..37b436f2b 100644 --- a/codes/go/chapter_stack_and_queue/array_deque.go +++ b/codes/go/chapter_stack_and_queue/array_deque.go @@ -72,6 +72,9 @@ func (q *arrayDeque) pushLast(num int) { /* 队首出队 */ func (q *arrayDeque) popFirst() any { num := q.peekFirst() + if num == nil { + return nil + } // 队首指针向后移动一位 q.front = q.index(q.front + 1) q.queSize-- @@ -81,6 +84,9 @@ func (q *arrayDeque) popFirst() any { /* 队尾出队 */ func (q *arrayDeque) popLast() any { num := q.peekLast() + if num == nil { + return nil + } q.queSize-- return num } diff --git a/codes/go/chapter_stack_and_queue/array_queue.go b/codes/go/chapter_stack_and_queue/array_queue.go index 9068996a1..d8723e8e3 100644 --- a/codes/go/chapter_stack_and_queue/array_queue.go +++ b/codes/go/chapter_stack_and_queue/array_queue.go @@ -49,6 +49,10 @@ func (q *arrayQueue) push(num int) { /* 出队 */ func (q *arrayQueue) pop() any { num := q.peek() + if num == nil { + return nil + } + // 队首指针向后移动一位,若越过尾部,则返回到数组头部 q.front = (q.front + 1) % q.queCapacity q.queSize-- diff --git a/codes/go/chapter_stack_and_queue/queue_test.go b/codes/go/chapter_stack_and_queue/queue_test.go index cf46cd17b..c25f0f12d 100644 --- a/codes/go/chapter_stack_and_queue/queue_test.go +++ b/codes/go/chapter_stack_and_queue/queue_test.go @@ -46,9 +46,13 @@ func TestQueue(t *testing.T) { } func TestArrayQueue(t *testing.T) { + // 初始化队列,使用队列的通用接口 capacity := 10 queue := newArrayQueue(capacity) + if queue.pop() != nil { + t.Errorf("want:%v,got:%v", nil, queue.pop()) + } // 元素入队 queue.push(1) diff --git a/codes/rust/chapter_backtracking/permutations_i.rs b/codes/rust/chapter_backtracking/permutations_i.rs index dae411014..3e2eec41f 100644 --- a/codes/rust/chapter_backtracking/permutations_i.rs +++ b/codes/rust/chapter_backtracking/permutations_i.rs @@ -23,7 +23,7 @@ fn backtrack(mut state: Vec, choices: &[i32], selected: &mut [bool], res: & backtrack(state.clone(), choices, selected, res); // 回退:撤销选择,恢复到之前的状态 selected[i] = false; - state.remove(state.len() - 1); + state.pop(); } } } diff --git a/codes/rust/chapter_backtracking/permutations_ii.rs b/codes/rust/chapter_backtracking/permutations_ii.rs index 9c422b6e0..d2db56928 100644 --- a/codes/rust/chapter_backtracking/permutations_ii.rs +++ b/codes/rust/chapter_backtracking/permutations_ii.rs @@ -27,7 +27,7 @@ fn backtrack(mut state: Vec, choices: &[i32], selected: &mut [bool], res: & backtrack(state.clone(), choices, selected, res); // 回退:撤销选择,恢复到之前的状态 selected[i] = false; - state.remove(state.len() - 1); + state.pop(); } } } diff --git a/codes/rust/chapter_computational_complexity/time_complexity.rs b/codes/rust/chapter_computational_complexity/time_complexity.rs index 70d451068..8fb4121a2 100644 --- a/codes/rust/chapter_computational_complexity/time_complexity.rs +++ b/codes/rust/chapter_computational_complexity/time_complexity.rs @@ -113,7 +113,7 @@ fn linear_log_recur(n: i32) -> i32 { return 1; } let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2); - for _ in 0..n as i32 { + for _ in 0..n { count += 1; } return count; diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 325571494..88c019bc4 100755 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -30,7 +30,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for (int i = 0; i < n; i++) { // 1 ns cout << 0 << endl; // 5 ns } } @@ -45,7 +45,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for (int i = 0; i < n; i++) { // 1 ns System.out.println(0); // 5 ns } } @@ -60,7 +60,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for (int i = 0; i < n; i++) { // 1 ns Console.WriteLine(0); // 5 ns } } @@ -105,7 +105,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for(let i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for(let i = 0; i < n; i++) { // 1 ns console.log(0); // 5 ns } } @@ -120,7 +120,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for(let i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for(let i = 0; i < n; i++) { // 1 ns console.log(0); // 5 ns } } @@ -135,7 +135,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for (int i = 0; i < n; i++) { // 1 ns print(0); // 5 ns } } @@ -150,7 +150,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for _ in 0..n { // 1 ns ,每轮都要执行 i++ + for _ in 0..n { // 1 ns println!("{}", 0); // 5 ns } } @@ -165,7 +165,7 @@ a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + for (int i = 0; i < n; i++) { // 1 ns printf("%d", 0); // 5 ns } } @@ -180,7 +180,7 @@ a = a + 1 // 1 ns a = a * 2 // 10 ns // 循环 n 次 - for (i in 0..undirected graphs and directed graphs, as shown in the figure below. +Graphs can be divided into undirected graphs and directed graphs depending on whether edges have direction, as shown in the figure below. -- In undirected graphs, edges represent a "bidirectional" connection between two vertices, for example, the "friendship" in WeChat or QQ. -- In directed graphs, edges have directionality, that is, the edges $A \rightarrow B$ and $A \leftarrow B$ are independent of each other, for example, the "follow" and "be followed" relationship on Weibo or TikTok. +- In undirected graphs, edges represent a "bidirectional" connection between two vertices, for example, the "friends" in Facebook. +- In directed graphs, edges have directionality, that is, the edges $A \rightarrow B$ and $A \leftarrow B$ are independent of each other. For example, the "follow" and "followed" relationship on Instagram or TikTok. ![Directed and undirected graphs](graph.assets/directed_graph.png) -Based on whether all vertices are connected, graphs can be divided into connected graphs and disconnected graphs, as shown in the figure below. +Depending on whether all vertices are connected, graphs can be divided into connected graphs and disconnected graphs, as shown in the figure below. -- For connected graphs, it is possible to reach any other vertex starting from a certain vertex. -- For disconnected graphs, there is at least one vertex that cannot be reached from a certain starting vertex. +- For connected graphs, it is possible to reach any other vertex starting from an arbitrary vertex. +- For disconnected graphs, there is at least one vertex that cannot be reached from an arbitrary starting vertex. ![Connected and disconnected graphs](graph.assets/connected_graph.png) -We can also add a weight variable to edges, resulting in weighted graphs as shown in the figure below. For example, in mobile games like "Honor of Kings", the system calculates the "closeness" between players based on shared gaming time, and this closeness network can be represented with a weighted graph. +We can also add a weight variable to edges, resulting in weighted graphs as shown in the figure below. For example, in Instagram, the system sorts your follower and following list by the level of interaction between you and other users (likes, views, comments, etc.). Such an interaction network can be represented by a weighted graph. ![Weighted and unweighted graphs](graph.assets/weighted_graph.png) @@ -42,7 +42,7 @@ Graph data structures include the following commonly used terms. ## Representation of graphs -Common representations of graphs include "adjacency matrices" and "adjacency lists". The following examples use undirected graphs. +Common representations of graphs include "adjacency matrix" and "adjacency list". The following examples use undirected graphs. ### Adjacency matrix @@ -55,10 +55,10 @@ As shown in the figure below, let the adjacency matrix be $M$, and the list of v Adjacency matrices have the following characteristics. - A vertex cannot be connected to itself, so the elements on the main diagonal of the adjacency matrix are meaningless. -- For undirected graphs, edges in both directions are equivalent, thus the adjacency matrix is symmetric about the main diagonal. -- By replacing the elements of the adjacency matrix from $1$ and $0$ to weights, it can represent weighted graphs. +- For undirected graphs, edges in both directions are equivalent, thus the adjacency matrix is symmetric with regard to the main diagonal. +- By replacing the elements of the adjacency matrix from $1$ and $0$ to weights, we can represent weighted graphs. -When representing graphs with adjacency matrices, it is possible to directly access matrix elements to obtain edges, thus operations of addition, deletion, lookup, and modification are very efficient, all with a time complexity of $O(1)$. However, the space complexity of the matrix is $O(n^2)$, which consumes more memory. +When representing graphs with adjacency matrices, it is possible to directly access matrix elements to obtain edges, resulting in efficient operations of addition, deletion, lookup, and modification, all with a time complexity of $O(1)$. However, the space complexity of the matrix is $O(n^2)$, which consumes more memory. ### Adjacency list @@ -78,6 +78,6 @@ As shown in the table below, many real-world systems can be modeled with graphs, | | Vertices | Edges | Graph Computing Problem | | --------------- | ---------------- | --------------------------------------------- | -------------------------------- | -| Social Networks | Users | Friendships | Potential Friend Recommendations | +| Social Networks | Users | Follow / Followed | Potential Following Recommendations | | Subway Lines | Stations | Connectivity Between Stations | Shortest Route Recommendations | | Solar System | Celestial Bodies | Gravitational Forces Between Celestial Bodies | Planetary Orbit Calculations | diff --git a/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step1_initialization.png b/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step1_initialization.png index 4650b97e2..6dd2d12eb 100644 Binary files a/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step1_initialization.png and b/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step1_initialization.png differ diff --git a/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step2_add_edge.png b/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step2_add_edge.png index 3e71604fe..21a6fd735 100644 Binary files a/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step2_add_edge.png and b/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step2_add_edge.png differ diff --git a/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step3_remove_edge.png b/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step3_remove_edge.png index 3cee7587e..a739d2e4b 100644 Binary files a/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step3_remove_edge.png and b/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step3_remove_edge.png differ diff --git a/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step4_add_vertex.png b/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step4_add_vertex.png index 1eb24d22f..8e6f63a2f 100644 Binary files a/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step4_add_vertex.png and b/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step4_add_vertex.png differ diff --git a/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step5_remove_vertex.png b/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step5_remove_vertex.png index aa33437e9..c2e5250b7 100644 Binary files a/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step5_remove_vertex.png and b/en/docs/chapter_graph/graph_operations.assets/adjacency_list_step5_remove_vertex.png differ diff --git a/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step1_initialization.png b/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step1_initialization.png index d2efe15b3..6496a4531 100644 Binary files a/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step1_initialization.png and b/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step1_initialization.png differ diff --git a/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step2_add_edge.png b/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step2_add_edge.png index 42cce3205..b52f84e89 100644 Binary files a/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step2_add_edge.png and b/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step2_add_edge.png differ diff --git a/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step3_remove_edge.png b/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step3_remove_edge.png index e4558601f..f00284f8a 100644 Binary files a/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step3_remove_edge.png and b/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step3_remove_edge.png differ diff --git a/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step4_add_vertex.png b/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step4_add_vertex.png index 1abc9ee7e..226a3024f 100644 Binary files a/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step4_add_vertex.png and b/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step4_add_vertex.png differ diff --git a/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step5_remove_vertex.png b/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step5_remove_vertex.png index 8b64d1d22..64e030b92 100644 Binary files a/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step5_remove_vertex.png and b/en/docs/chapter_graph/graph_operations.assets/adjacency_matrix_step5_remove_vertex.png differ diff --git a/en/docs/chapter_graph/index.md b/en/docs/chapter_graph/index.md index 9ff892648..46d9c9687 100644 --- a/en/docs/chapter_graph/index.md +++ b/en/docs/chapter_graph/index.md @@ -4,6 +4,6 @@ !!! abstract - In the journey of life, we are like individual nodes, connected by countless invisible edges. + In the journey of life, each of us is a node, connected by countless invisible edges. - Each encounter and parting leaves a distinctive imprint on this vast network graph. + Each encounter and parting leaves a unique imprint on this vast graph of life. diff --git a/en/docs/chapter_greedy/fractional_knapsack_problem.assets/fractional_knapsack_example.png b/en/docs/chapter_greedy/fractional_knapsack_problem.assets/fractional_knapsack_example.png index 64b3a870b..9e10560cb 100644 Binary files a/en/docs/chapter_greedy/fractional_knapsack_problem.assets/fractional_knapsack_example.png and b/en/docs/chapter_greedy/fractional_knapsack_problem.assets/fractional_knapsack_example.png differ diff --git a/en/docs/chapter_greedy/greedy_algorithm.assets/coin_change_greedy_strategy.png b/en/docs/chapter_greedy/greedy_algorithm.assets/coin_change_greedy_strategy.png index 78a5ef555..0412e0ce3 100644 Binary files a/en/docs/chapter_greedy/greedy_algorithm.assets/coin_change_greedy_strategy.png and b/en/docs/chapter_greedy/greedy_algorithm.assets/coin_change_greedy_strategy.png differ diff --git a/en/docs/chapter_greedy/greedy_algorithm.assets/coin_change_greedy_vs_dp.png b/en/docs/chapter_greedy/greedy_algorithm.assets/coin_change_greedy_vs_dp.png index bc03e9fff..b53735aa6 100644 Binary files a/en/docs/chapter_greedy/greedy_algorithm.assets/coin_change_greedy_vs_dp.png and b/en/docs/chapter_greedy/greedy_algorithm.assets/coin_change_greedy_vs_dp.png differ diff --git a/en/docs/chapter_greedy/max_capacity_problem.assets/max_capacity_moving_long_board.png b/en/docs/chapter_greedy/max_capacity_problem.assets/max_capacity_moving_long_board.png index 8b26aad2d..6b4936205 100644 Binary files a/en/docs/chapter_greedy/max_capacity_problem.assets/max_capacity_moving_long_board.png and b/en/docs/chapter_greedy/max_capacity_problem.assets/max_capacity_moving_long_board.png differ diff --git a/en/docs/chapter_greedy/max_capacity_problem.assets/max_capacity_moving_short_board.png b/en/docs/chapter_greedy/max_capacity_problem.assets/max_capacity_moving_short_board.png index 56d4f2038..ed2688703 100644 Binary files a/en/docs/chapter_greedy/max_capacity_problem.assets/max_capacity_moving_short_board.png and b/en/docs/chapter_greedy/max_capacity_problem.assets/max_capacity_moving_short_board.png differ diff --git a/en/docs/chapter_greedy/max_product_cutting_problem.assets/max_product_cutting_greedy_infer1.png b/en/docs/chapter_greedy/max_product_cutting_problem.assets/max_product_cutting_greedy_infer1.png index cb77b3190..0251cb147 100644 Binary files a/en/docs/chapter_greedy/max_product_cutting_problem.assets/max_product_cutting_greedy_infer1.png and b/en/docs/chapter_greedy/max_product_cutting_problem.assets/max_product_cutting_greedy_infer1.png differ diff --git a/en/docs/chapter_hashing/hash_map.assets/hash_table_reshash.png b/en/docs/chapter_hashing/hash_map.assets/hash_table_reshash.png index 6da388c1c..2586a5410 100644 Binary files a/en/docs/chapter_hashing/hash_map.assets/hash_table_reshash.png and b/en/docs/chapter_hashing/hash_map.assets/hash_table_reshash.png differ diff --git a/en/docs/chapter_hashing/hash_map.md b/en/docs/chapter_hashing/hash_map.md index 3530a7f7b..20d1ffe11 100755 --- a/en/docs/chapter_hashing/hash_map.md +++ b/en/docs/chapter_hashing/hash_map.md @@ -1,30 +1,30 @@ # Hash table -A hash table achieves efficient element querying by establishing a mapping between keys and values. Specifically, when we input a `key` into the hash table, we can retrieve the corresponding `value` in $O(1)$ time. +A hash table, also known as a hash map, is a data structure that establishes a mapping between keys and values, enabling efficient element retrieval. Specifically, when we input a `key` into the hash table, we can retrive the corresponding `value` in $O(1)$ time complexity. -As shown in the figure below, given $n$ students, each with two pieces of data: "name" and "student number". If we want to implement a query feature that returns the corresponding name when given a student number, we can use the hash table shown in the figure below. +As shown in the figure below, given $n$ students, each student has two data fields: "Name" and "Student ID". If we want to implement a query function that takes a student ID as input and returns the corresponding name, we can use the hash table shown in the figure below. ![Abstract representation of a hash table](hash_map.assets/hash_table_lookup.png) -Apart from hash tables, arrays and linked lists can also be used to implement querying functions. Their efficiency is compared in the table below. +In addition to hash tables, arrays and linked lists can also be used to implement query functionality, but the time complexity is different. Their efficiency is compared in the table below: -- **Adding elements**: Simply add the element to the end of the array (or linked list), using $O(1)$ time. -- **Querying elements**: Since the array (or linked list) is unordered, it requires traversing all the elements, using $O(n)$ time. -- **Deleting elements**: First, locate the element, then delete it from the array (or linked list), using $O(n)$ time. +- **Inserting elements**: Simply append the element to the tail of the array (or linked list). The time complexity of this operation is $O(1)$. +- **Searching for elements**: As the array (or linked list) is unsorted, searching for an element requires traversing through all of the elements. The time complexity of this operation is $O(n)$. +- **Deleting elements**: To remove an element, we first need to locate it. Then, we delete it from the array (or linked list). The time complexity of this operation is $O(n)$. -

Table   Comparison of element query efficiency

+

Table   Comparison of time efficiency for common operations

| | Array | Linked List | Hash Table | | -------------- | ------ | ----------- | ---------- | -| Find Element | $O(n)$ | $O(n)$ | $O(1)$ | -| Add Element | $O(1)$ | $O(1)$ | $O(1)$ | -| Delete Element | $O(n)$ | $O(n)$ | $O(1)$ | +| Search Elements | $O(n)$ | $O(n)$ | $O(1)$ | +| Insert Elements | $O(1)$ | $O(1)$ | $O(1)$ | +| Delete Elements | $O(n)$ | $O(n)$ | $O(1)$ | -Observations reveal that **the time complexity for adding, deleting, and querying in a hash table is $O(1)$**, which is highly efficient. +It can be seen that **the time complexity for operations (insertion, deletion, searching, and modification) in a hash table is $O(1)$**, which is highly efficient. ## Common operations of hash table -Common operations of a hash table include initialization, querying, adding key-value pairs, and deleting key-value pairs, etc. Example code is as follows: +Common operations of a hash table include: initialization, querying, adding key-value pairs, and deleting key-value pairs. Here is an example code: === "Python" @@ -283,7 +283,7 @@ Common operations of a hash table include initialization, querying, adding key-v https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%93%88%E5%B8%8C%E8%A1%A8%0A%20%20%20%20hmap%20%3D%20%7B%7D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%B7%BB%E5%8A%A0%E6%93%8D%E4%BD%9C%0A%20%20%20%20%23%20%E5%9C%A8%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%AD%E6%B7%BB%E5%8A%A0%E9%94%AE%E5%80%BC%E5%AF%B9%20%28key,%20value%29%0A%20%20%20%20hmap%5B12836%5D%20%3D%20%22%E5%B0%8F%E5%93%88%22%0A%20%20%20%20hmap%5B15937%5D%20%3D%20%22%E5%B0%8F%E5%95%B0%22%0A%20%20%20%20hmap%5B16750%5D%20%3D%20%22%E5%B0%8F%E7%AE%97%22%0A%20%20%20%20hmap%5B13276%5D%20%3D%20%22%E5%B0%8F%E6%B3%95%22%0A%20%20%20%20hmap%5B10583%5D%20%3D%20%22%E5%B0%8F%E9%B8%AD%22%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C%0A%20%20%20%20%23%20%E5%90%91%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%AD%E8%BE%93%E5%85%A5%E9%94%AE%20key%20%EF%BC%8C%E5%BE%97%E5%88%B0%E5%80%BC%20value%0A%20%20%20%20name%20%3D%20hmap%5B15937%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C%0A%20%20%20%20%23%20%E5%9C%A8%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%AD%E5%88%A0%E9%99%A4%E9%94%AE%E5%80%BC%E5%AF%B9%20%28key,%20value%29%0A%20%20%20%20hmap.pop%2810583%29&cumulative=false&curInstr=2&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false -There are three common ways to traverse a hash table: traversing key-value pairs, keys, and values. Example code is as follows: +There are three common ways to traverse a hash table: traversing key-value pairs, traversing keys, and traversing values. Here is an example code: === "Python" @@ -484,24 +484,24 @@ There are three common ways to traverse a hash table: traversing key-value pairs https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%93%88%E5%B8%8C%E8%A1%A8%0A%20%20%20%20hmap%20%3D%20%7B%7D%0A%20%20%20%20%0A%20%20%20%20%23%20%E6%B7%BB%E5%8A%A0%E6%93%8D%E4%BD%9C%0A%20%20%20%20%23%20%E5%9C%A8%E5%93%88%E5%B8%8C%E8%A1%A8%E4%B8%AD%E6%B7%BB%E5%8A%A0%E9%94%AE%E5%80%BC%E5%AF%B9%20%28key,%20value%29%0A%20%20%20%20hmap%5B12836%5D%20%3D%20%22%E5%B0%8F%E5%93%88%22%0A%20%20%20%20hmap%5B15937%5D%20%3D%20%22%E5%B0%8F%E5%95%B0%22%0A%20%20%20%20hmap%5B16750%5D%20%3D%20%22%E5%B0%8F%E7%AE%97%22%0A%20%20%20%20hmap%5B13276%5D%20%3D%20%22%E5%B0%8F%E6%B3%95%22%0A%20%20%20%20hmap%5B10583%5D%20%3D%20%22%E5%B0%8F%E9%B8%AD%22%0A%20%20%20%20%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E5%93%88%E5%B8%8C%E8%A1%A8%0A%20%20%20%20%23%20%E9%81%8D%E5%8E%86%E9%94%AE%E5%80%BC%E5%AF%B9%20key-%3Evalue%0A%20%20%20%20for%20key,%20value%20in%20hmap.items%28%29%3A%0A%20%20%20%20%20%20%20%20print%28key,%20%22-%3E%22,%20value%29%0A%20%20%20%20%23%20%E5%8D%95%E7%8B%AC%E9%81%8D%E5%8E%86%E9%94%AE%20key%0A%20%20%20%20for%20key%20in%20hmap.keys%28%29%3A%0A%20%20%20%20%20%20%20%20print%28key%29%0A%20%20%20%20%23%20%E5%8D%95%E7%8B%AC%E9%81%8D%E5%8E%86%E5%80%BC%20value%0A%20%20%20%20for%20value%20in%20hmap.values%28%29%3A%0A%20%20%20%20%20%20%20%20print%28value%29&cumulative=false&curInstr=8&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false -## Simple implementation of hash table +## Simple implementation of a hash table -First, let's consider the simplest case: **implementing a hash table using just an array**. In the hash table, each empty slot in the array is called a bucket, and each bucket can store one key-value pair. Therefore, the query operation involves finding the bucket corresponding to the `key` and retrieving the `value` from it. +First, let's consider the simplest case: **implementing a hash table using only one array**. In the hash table, each empty slot in the array is called a bucket, and each bucket can store a key-value pair. Therefore, the query operation involves finding the bucket corresponding to the `key` and retrieving the `value` from it. -So, how do we locate the appropriate bucket based on the `key`? This is achieved through a hash function. The role of the hash function is to map a larger input space to a smaller output space. In a hash table, the input space is all possible keys, and the output space is all buckets (array indices). In other words, input a `key`, **and we can use the hash function to determine the storage location of the corresponding key-value pair in the array**. +So, how do we locate the corresponding bucket based on the `key`? This is achieved through a hash function. The role of the hash function is to map a larger input space to a smaller output space. In a hash table, the input space consists of all the keys, and the output space consists of all the buckets (array indices). In other words, given a `key`, **we can use the hash function to determine the storage location of the corresponding key-value pair in the array**. -The calculation process of the hash function for a given `key` is divided into the following two steps: +When given a `key`, the calculation process of the hash function consists of the following two steps: -1. Calculate the hash value using a certain hash algorithm `hash()`. -2. Take the modulus of the hash value with the number of buckets (array length) `capacity` to obtain the array index `index`. +1. Calculate the hash value by using a certain hash algorithm `hash()`. +2. Take the modulus of the hash value with the bucket count (array length) `capacity` to obtain the array `index` corresponding to that key. ```shell index = hash(key) % capacity ``` -Afterward, we can use `index` to access the corresponding bucket in the hash table and thereby retrieve the `value`. +Afterward, we can use the `index` to access the corresponding bucket in the hash table and thereby retrieve the `value`. -Assuming array length `capacity = 100` and hash algorithm `hash(key) = key`, the hash function is `key % 100`. The figure below uses `key` as the student number and `value` as the name to demonstrate the working principle of the hash function. +Let's assume that the array length is `capacity = 100`, and the hash algorithm is defined as `hash(key) = key`. Therefore, the hash function can be expressed as `key % 100`. The following figure illustrates the working principle of the hash function using `key` as student ID and `value` as name. ![Working principle of hash function](hash_map.assets/hash_function.png) @@ -513,25 +513,25 @@ The following code implements a simple hash table. Here, we encapsulate `key` an ## Hash collision and resizing -Fundamentally, the role of the hash function is to map the entire input space of all keys to the output space of all array indices. However, the input space is often much larger than the output space. Therefore, **theoretically, there must be situations where "multiple inputs correspond to the same output"**. +Essentially, the role of the hash function is to map the entire input space of all keys to the output space of all array indices. However, the input space is often much larger than the output space. Therefore, **theoretically, there will always be cases where "multiple inputs correspond to the same output"**. -For the hash function in the above example, if the last two digits of the input `key` are the same, the output of the hash function will also be the same. For example, when querying for students with student numbers 12836 and 20336, we find: +In the example above, with the given hash function, when the last two digits of the input `key` are the same, the hash function produces the same output. For instance, when querying two students with student IDs 12836 and 20336, we find: ```shell 12836 % 100 = 36 20336 % 100 = 36 ``` -As shown in the figure below, both student numbers point to the same name, which is obviously incorrect. This situation where multiple inputs correspond to the same output is known as hash collision. +As shown in the figure below, both student IDs point to the same name, which is obviously incorrect. This situation where multiple inputs correspond to the same output is called hash collision. ![Example of hash collision](hash_map.assets/hash_collision.png) -It is easy to understand that the larger the capacity $n$ of the hash table, the lower the probability of multiple keys being allocated to the same bucket, and the fewer the collisions. Therefore, **expanding the capacity of the hash table can reduce hash collisions**. +It is easy to understand that as the capacity $n$ of the hash table increases, the probability of multiple keys being assigned to the same bucket decreases, resulting in fewer collisions. Therefore, **we can reduce hash collisions by resizing the hash table**. -As shown in the figure below, before expansion, key-value pairs `(136, A)` and `(236, D)` collided; after expansion, the collision is resolved. +As shown in the figure below, before resizing, the key-value pairs `(136, A)` and `(236, D)` collide. However, after resizing, the collision is resolved. -![Hash table expansion](hash_map.assets/hash_table_reshash.png) +![Hash table resizing](hash_map.assets/hash_table_reshash.png) -Similar to array expansion, resizing a hash table requires migrating all key-value pairs from the original hash table to the new one, which is time-consuming. Furthermore, since the capacity `capacity` of the hash table changes, we need to recalculate the storage positions of all key-value pairs using the hash function, which adds to the computational overhead of the resizing process. Therefore, programming languages often reserve a sufficiently large capacity for the hash table to prevent frequent resizing. +Similar to array expansion, resizing a hash table requires migrating all key-value pairs from the original hash table to the new one, which is time-consuming. Furthermore, since the `capacity` of the hash table changes, we need to recalculate the storage positions of all key-value pairs using the hash function, further increasing the computational overhead of the resizing process. Therefore, programming languages often allocate a sufficiently large capacity for the hash table to prevent frequent resizing. -The load factor is an important concept for hash tables. It is defined as the ratio of the number of elements in the hash table to the number of buckets. It is used to measure the severity of hash collisions and **is often used as a trigger for resizing the hash table**. For example, in Java, when the load factor exceeds $0.75$, the system will resize the hash table to twice its original size. +The load factor is an important concept in hash tables. It is defined as the ratio of the number of elements in the hash table to the number of buckets. It is used to measure the severity of hash collisions and **often serves as a trigger for hash table resizing**. For example, in Java, when the load factor exceeds $0.75$, the system will resize the hash table to twice its original size. diff --git a/en/docs/chapter_heap/build_heap.assets/heapify_operations_count.png b/en/docs/chapter_heap/build_heap.assets/heapify_operations_count.png index 012bd7cfa..c5f93069f 100644 Binary files a/en/docs/chapter_heap/build_heap.assets/heapify_operations_count.png and b/en/docs/chapter_heap/build_heap.assets/heapify_operations_count.png differ diff --git a/en/docs/chapter_heap/summary.md b/en/docs/chapter_heap/summary.md index 03ae24af1..83b126f5f 100644 --- a/en/docs/chapter_heap/summary.md +++ b/en/docs/chapter_heap/summary.md @@ -2,16 +2,16 @@ ### Key review -- A heap is a complete binary tree, which can be divided into a max heap and a min heap based on its property. The top element of a max (min) heap is the largest (smallest). +- A heap is a complete binary tree that can be categorized as either a max heap or a min heap based on its building property, where the top element of a max heap is the largest and the top element of a min heap is the smallest. - A priority queue is defined as a queue with dequeue priority, usually implemented using a heap. - Common operations of a heap and their corresponding time complexities include: element insertion into the heap $O(\log n)$, removing the top element from the heap $O(\log n)$, and accessing the top element of the heap $O(1)$. - A complete binary tree is well-suited to be represented by an array, thus heaps are commonly stored using arrays. - Heapify operations are used to maintain the properties of the heap and are used in both heap insertion and removal operations. -- The time complexity of inserting $n$ elements into a heap and building the heap can be optimized to $O(n)$, which is highly efficient. +- The time complexity of building a heap given an input of $n$ elements can be optimized to $O(n)$, which is highly efficient. - Top-k is a classic algorithm problem that can be efficiently solved using the heap data structure, with a time complexity of $O(n \log k)$. ### Q & A **Q**: Is the "heap" in data structures the same concept as the "heap" in memory management? -The two are not the same concept, even though they are both referred to as "heap". The heap in computer system memory is part of dynamic memory allocation, where the program can use it to store data during execution. The program can request a certain amount of heap memory to store complex structures like objects and arrays. When these data are no longer needed, the program needs to release this memory to prevent memory leaks. Compared to stack memory, the management and usage of heap memory need to be more cautious, as improper use may lead to memory leaks and dangling pointers. +The two are not the same concept, even though they are both referred to as "heap". The heap in computer system memory is part of dynamic memory allocation, where the program can use it to store data during execution. The program can request a certain amount of heap memory to store complex structures like objects and arrays. When the allocated data is no longer needed, the program needs to release this memory to prevent memory leaks. Compared to stack memory, the management and usage of heap memory demands more caution, as improper use may lead to memory leaks and dangling pointers. diff --git a/en/docs/chapter_heap/top_k.assets/top_k_heap_step1.png b/en/docs/chapter_heap/top_k.assets/top_k_heap_step1.png index 7447e4abc..e23da6857 100644 Binary files a/en/docs/chapter_heap/top_k.assets/top_k_heap_step1.png and b/en/docs/chapter_heap/top_k.assets/top_k_heap_step1.png differ diff --git a/en/docs/chapter_heap/top_k.assets/top_k_heap_step2.png b/en/docs/chapter_heap/top_k.assets/top_k_heap_step2.png index 5962b32ab..53ae93b87 100644 Binary files a/en/docs/chapter_heap/top_k.assets/top_k_heap_step2.png and b/en/docs/chapter_heap/top_k.assets/top_k_heap_step2.png differ diff --git a/en/docs/chapter_heap/top_k.assets/top_k_heap_step3.png b/en/docs/chapter_heap/top_k.assets/top_k_heap_step3.png index b73903f4c..f1a919eb0 100644 Binary files a/en/docs/chapter_heap/top_k.assets/top_k_heap_step3.png and b/en/docs/chapter_heap/top_k.assets/top_k_heap_step3.png differ diff --git a/en/docs/chapter_heap/top_k.assets/top_k_heap_step4.png b/en/docs/chapter_heap/top_k.assets/top_k_heap_step4.png index a8bfca590..e59e4775b 100644 Binary files a/en/docs/chapter_heap/top_k.assets/top_k_heap_step4.png and b/en/docs/chapter_heap/top_k.assets/top_k_heap_step4.png differ diff --git a/en/docs/chapter_heap/top_k.assets/top_k_heap_step5.png b/en/docs/chapter_heap/top_k.assets/top_k_heap_step5.png index a741434f1..d83d929c3 100644 Binary files a/en/docs/chapter_heap/top_k.assets/top_k_heap_step5.png and b/en/docs/chapter_heap/top_k.assets/top_k_heap_step5.png differ diff --git a/en/docs/chapter_heap/top_k.assets/top_k_heap_step6.png b/en/docs/chapter_heap/top_k.assets/top_k_heap_step6.png index 8bdd7c1cc..53c669e17 100644 Binary files a/en/docs/chapter_heap/top_k.assets/top_k_heap_step6.png and b/en/docs/chapter_heap/top_k.assets/top_k_heap_step6.png differ diff --git a/en/docs/chapter_heap/top_k.assets/top_k_heap_step7.png b/en/docs/chapter_heap/top_k.assets/top_k_heap_step7.png index dbf6e71e2..b77c839d3 100644 Binary files a/en/docs/chapter_heap/top_k.assets/top_k_heap_step7.png and b/en/docs/chapter_heap/top_k.assets/top_k_heap_step7.png differ diff --git a/en/docs/chapter_heap/top_k.assets/top_k_heap_step8.png b/en/docs/chapter_heap/top_k.assets/top_k_heap_step8.png index c9e5198a2..73283b928 100644 Binary files a/en/docs/chapter_heap/top_k.assets/top_k_heap_step8.png and b/en/docs/chapter_heap/top_k.assets/top_k_heap_step8.png differ diff --git a/en/docs/chapter_heap/top_k.assets/top_k_traversal.png b/en/docs/chapter_heap/top_k.assets/top_k_traversal.png index 15cc529e2..62d67b1e0 100644 Binary files a/en/docs/chapter_heap/top_k.assets/top_k_traversal.png and b/en/docs/chapter_heap/top_k.assets/top_k_traversal.png differ diff --git a/en/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_naive.png b/en/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_naive.png index 3b97fd57f..bb76c5ab4 100644 Binary files a/en/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_naive.png and b/en/docs/chapter_searching/binary_search_insertion.assets/binary_search_insertion_naive.png differ diff --git a/en/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png b/en/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png index a7f7e64b5..6e942957a 100644 Binary files a/en/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png and b/en/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png differ diff --git a/en/docs/chapter_sorting/bubble_sort.assets/bubble_sort_overview.png b/en/docs/chapter_sorting/bubble_sort.assets/bubble_sort_overview.png index 5ed1a155b..e9699f7d4 100644 Binary files a/en/docs/chapter_sorting/bubble_sort.assets/bubble_sort_overview.png and b/en/docs/chapter_sorting/bubble_sort.assets/bubble_sort_overview.png differ diff --git a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step1.png b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step1.png index 8966b7390..670b26a6b 100644 Binary files a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step1.png and b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step1.png differ diff --git a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step10.png b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step10.png index 27a61d45c..1f1b5180d 100644 Binary files a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step10.png and b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step10.png differ diff --git a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step11.png b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step11.png index 20b751215..4476f5210 100644 Binary files a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step11.png and b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step11.png differ diff --git a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step12.png b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step12.png index b094f53db..ed065e5db 100644 Binary files a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step12.png and b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step12.png differ diff --git a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step2.png b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step2.png index ba2fe8481..587eaec82 100644 Binary files a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step2.png and b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step2.png differ diff --git a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step3.png b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step3.png index acb09744e..4178dc6f7 100644 Binary files a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step3.png and b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step3.png differ diff --git a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step4.png b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step4.png index 0d145f850..c28742f84 100644 Binary files a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step4.png and b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step4.png differ diff --git a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step5.png b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step5.png index f68b1da53..568984c3f 100644 Binary files a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step5.png and b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step5.png differ diff --git a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step6.png b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step6.png index d16956b21..ca48e5fbc 100644 Binary files a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step6.png and b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step6.png differ diff --git a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step7.png b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step7.png index 6b9e84ec1..aba72afd4 100644 Binary files a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step7.png and b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step7.png differ diff --git a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step8.png b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step8.png index bc28bb736..62540bb5d 100644 Binary files a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step8.png and b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step8.png differ diff --git a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step9.png b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step9.png index 09dcde6e0..89aea66ab 100644 Binary files a/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step9.png and b/en/docs/chapter_sorting/heap_sort.assets/heap_sort_step9.png differ diff --git a/en/docs/chapter_sorting/insertion_sort.assets/insertion_operation.png b/en/docs/chapter_sorting/insertion_sort.assets/insertion_operation.png index db5145cdc..8a5b35cd7 100644 Binary files a/en/docs/chapter_sorting/insertion_sort.assets/insertion_operation.png and b/en/docs/chapter_sorting/insertion_sort.assets/insertion_operation.png differ diff --git a/en/docs/chapter_sorting/quick_sort.assets/pivot_division_step4.png b/en/docs/chapter_sorting/quick_sort.assets/pivot_division_step4.png index ca642b059..ec00e8f0d 100644 Binary files a/en/docs/chapter_sorting/quick_sort.assets/pivot_division_step4.png and b/en/docs/chapter_sorting/quick_sort.assets/pivot_division_step4.png differ diff --git a/en/docs/chapter_sorting/quick_sort.assets/pivot_division_step8.png b/en/docs/chapter_sorting/quick_sort.assets/pivot_division_step8.png index bbdeaeb09..3ada3943e 100644 Binary files a/en/docs/chapter_sorting/quick_sort.assets/pivot_division_step8.png and b/en/docs/chapter_sorting/quick_sort.assets/pivot_division_step8.png differ diff --git a/en/docs/chapter_sorting/selection_sort.assets/selection_sort_step11.png b/en/docs/chapter_sorting/selection_sort.assets/selection_sort_step11.png index 1b6d9df0a..d47f92440 100644 Binary files a/en/docs/chapter_sorting/selection_sort.assets/selection_sort_step11.png and b/en/docs/chapter_sorting/selection_sort.assets/selection_sort_step11.png differ diff --git a/en/docs/chapter_sorting/selection_sort.assets/selection_sort_step8.png b/en/docs/chapter_sorting/selection_sort.assets/selection_sort_step8.png index 2d4647d2f..be6b1285a 100644 Binary files a/en/docs/chapter_sorting/selection_sort.assets/selection_sort_step8.png and b/en/docs/chapter_sorting/selection_sort.assets/selection_sort_step8.png differ diff --git a/en/docs/chapter_tree/avl_tree.assets/avltree_degradation_from_inserting_node.png b/en/docs/chapter_tree/avl_tree.assets/avltree_degradation_from_inserting_node.png index c7870e8b9..ffc99032b 100644 Binary files a/en/docs/chapter_tree/avl_tree.assets/avltree_degradation_from_inserting_node.png and b/en/docs/chapter_tree/avl_tree.assets/avltree_degradation_from_inserting_node.png differ diff --git a/en/docs/chapter_tree/avl_tree.assets/avltree_degradation_from_removing_node.png b/en/docs/chapter_tree/avl_tree.assets/avltree_degradation_from_removing_node.png index f18234bc5..bbf8284a8 100644 Binary files a/en/docs/chapter_tree/avl_tree.assets/avltree_degradation_from_removing_node.png and b/en/docs/chapter_tree/avl_tree.assets/avltree_degradation_from_removing_node.png differ diff --git a/en/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step3.png b/en/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step3.png index 4028d3963..b563da0da 100644 Binary files a/en/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step3.png and b/en/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step3.png differ diff --git a/en/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step4.png b/en/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step4.png index 51feeb995..c3afd8c1b 100644 Binary files a/en/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step4.png and b/en/docs/chapter_tree/avl_tree.assets/avltree_right_rotate_step4.png differ diff --git a/en/docs/chapter_tree/avl_tree.assets/avltree_rotation_cases.png b/en/docs/chapter_tree/avl_tree.assets/avltree_rotation_cases.png index a40532dfe..da4dfce6d 100644 Binary files a/en/docs/chapter_tree/avl_tree.assets/avltree_rotation_cases.png and b/en/docs/chapter_tree/avl_tree.assets/avltree_rotation_cases.png differ diff --git a/en/docs/chapter_tree/avl_tree.md b/en/docs/chapter_tree/avl_tree.md index 70f159812..68bd5e088 100644 --- a/en/docs/chapter_tree/avl_tree.md +++ b/en/docs/chapter_tree/avl_tree.md @@ -289,17 +289,17 @@ It can be observed that **the right and left rotation operations are logically s [file]{avl_tree}-[class]{avl_tree}-[func]{left_rotate} ``` -### Right-left rotation +### Left-right rotation For the unbalanced node 3 shown in the figure below, using either left or right rotation alone cannot restore balance to the subtree. In this case, a "left rotation" needs to be performed on `child` first, followed by a "right rotation" on `node`. -![Right-left rotation](avl_tree.assets/avltree_left_right_rotate.png) +![Left-right rotation](avl_tree.assets/avltree_left_right_rotate.png) -### Left-right rotation +### Right-left rotation As shown in the figure below, for the mirror case of the above unbalanced binary tree, a "right rotation" needs to be performed on `child` first, followed by a "left rotation" on `node`. -![Left-right rotation](avl_tree.assets/avltree_right_left_rotate.png) +![Right-left rotation](avl_tree.assets/avltree_right_left_rotate.png) ### Choice of rotation diff --git a/en/docs/chapter_tree/binary_search_tree.assets/bst_degradation.png b/en/docs/chapter_tree/binary_search_tree.assets/bst_degradation.png index b8a732752..e56933ca5 100644 Binary files a/en/docs/chapter_tree/binary_search_tree.assets/bst_degradation.png and b/en/docs/chapter_tree/binary_search_tree.assets/bst_degradation.png differ diff --git a/en/docs/chapter_tree/binary_search_tree.assets/bst_inorder_traversal.png b/en/docs/chapter_tree/binary_search_tree.assets/bst_inorder_traversal.png index c2f8cd3fe..c26c1082b 100644 Binary files a/en/docs/chapter_tree/binary_search_tree.assets/bst_inorder_traversal.png and b/en/docs/chapter_tree/binary_search_tree.assets/bst_inorder_traversal.png differ diff --git a/en/docs/chapter_tree/binary_search_tree.assets/bst_insert.png b/en/docs/chapter_tree/binary_search_tree.assets/bst_insert.png index 9a295ffbc..a913e0063 100644 Binary files a/en/docs/chapter_tree/binary_search_tree.assets/bst_insert.png and b/en/docs/chapter_tree/binary_search_tree.assets/bst_insert.png differ diff --git a/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case1.png b/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case1.png index dd8b68a1c..494236725 100644 Binary files a/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case1.png and b/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case1.png differ diff --git a/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case2.png b/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case2.png index 7eca60a2c..979223610 100644 Binary files a/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case2.png and b/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case2.png differ diff --git a/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step1.png b/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step1.png index 705e0ef07..46e03649d 100644 Binary files a/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step1.png and b/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step1.png differ diff --git a/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step2.png b/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step2.png index 2bf435fd8..0efa18000 100644 Binary files a/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step2.png and b/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step2.png differ diff --git a/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step3.png b/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step3.png index 8fce7e613..7f7bd2416 100644 Binary files a/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step3.png and b/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step3.png differ diff --git a/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step4.png b/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step4.png index a66543a50..5acf8ffb8 100644 Binary files a/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step4.png and b/en/docs/chapter_tree/binary_search_tree.assets/bst_remove_case3_step4.png differ diff --git a/en/docs/chapter_tree/binary_tree.md b/en/docs/chapter_tree/binary_tree.md index ecc8d2b85..b855e1a82 100644 --- a/en/docs/chapter_tree/binary_tree.md +++ b/en/docs/chapter_tree/binary_tree.md @@ -57,7 +57,7 @@ A binary tree is a non-linear data structure that represents the hierarch Left *TreeNode Right *TreeNode } - /* 构造方法 */ + /* Constructor */ func NewTreeNode(v int) *TreeNode { return &TreeNode{ Left: nil, // Pointer to left child node @@ -141,7 +141,7 @@ A binary tree is a non-linear data structure that represents the hierarch } impl TreeNode { - /* 构造方法 */ + /* Constructor */ fn new(val: i32) -> Rc> { Rc::new(RefCell::new(Self { val, @@ -158,12 +158,12 @@ A binary tree is a non-linear data structure that represents the hierarch /* Binary tree node */ typedef struct TreeNode { int val; // Node value - int height; // 节点高度 + int height; // Node height struct TreeNode *left; // Pointer to left child node struct TreeNode *right; // Pointer to right child node } TreeNode; - /* 构造函数 */ + /* Constructor */ TreeNode *newTreeNode(int val) { TreeNode *node; diff --git a/zh-hant/codes/go/chapter_divide_and_conquer/binary_search_recur.go b/zh-hant/codes/go/chapter_divide_and_conquer/binary_search_recur.go index f24aaf89d..25ac0e467 100644 --- a/zh-hant/codes/go/chapter_divide_and_conquer/binary_search_recur.go +++ b/zh-hant/codes/go/chapter_divide_and_conquer/binary_search_recur.go @@ -18,7 +18,7 @@ func dfs(nums []int, target, i, j int) int { // 遞迴子問題 f(m+1, j) return dfs(nums, target, m+1, j) } else if nums[m] > target { - // 小於則遞迴左半陣列 + // 大於則遞迴左半陣列 // 遞迴子問題 f(i, m-1) return dfs(nums, target, i, m-1) } else {