diff --git a/codes/c/chapter_sorting/bucket_sort.c b/codes/c/chapter_sorting/bucket_sort.c index 5af623d286..0d9ef6b752 100644 --- a/codes/c/chapter_sorting/bucket_sort.c +++ b/codes/c/chapter_sorting/bucket_sort.c @@ -6,7 +6,7 @@ #include "../utils/common.h" -#define ARRAY_SIZE 10 +#define SIZE 10 /* 比较两个浮点数的大小 */ int compare_float(const void *a, const void *b) { @@ -28,8 +28,8 @@ void bucketSort(float nums[], int size) { int k = size / 2; float **buckets = calloc(k, sizeof(float *)); for (int i = 0; i < k; i++) { - // 每个桶最多可以分配 k 个元素 - buckets[i] = calloc(ARRAY_SIZE, sizeof(float)); + // 每个桶最多可以分配 size 个元素 + buckets[i] = calloc(size, sizeof(float)); } // 1. 将数组元素分配到各个桶中 @@ -42,7 +42,7 @@ void bucketSort(float nums[], int size) { j++; } float temp = nums[i]; - while (j < ARRAY_SIZE && buckets[bucket_idx][j] > 0) { + while (j < size && buckets[bucket_idx][j] > 0) { swap(&temp, &buckets[bucket_idx][j]); j++; } @@ -51,12 +51,12 @@ void bucketSort(float nums[], int size) { // 2. 对各个桶执行排序 for (int i = 0; i < k; i++) { - qsort(buckets[i], ARRAY_SIZE, sizeof(float), compare_float); + qsort(buckets[i], size, sizeof(float), compare_float); } // 3. 遍历桶合并结果 for (int i = 0, j = 0; j < k; j++) { - for (int l = 0; l < ARRAY_SIZE; l++) { + for (int l = 0; l < size; l++) { if (buckets[j][l] > 0) { nums[i++] = buckets[j][l]; } @@ -73,10 +73,10 @@ void bucketSort(float nums[], int size) { /* Driver Code */ int main() { // 设输入数据为浮点数,范围为 [0, 1) - float nums[ARRAY_SIZE] = {0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f}; - bucketSort(nums, ARRAY_SIZE); + float nums[SIZE] = {0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f}; + bucketSort(nums, SIZE); printf("桶排序完成后 nums = "); - printArrayFloat(nums, ARRAY_SIZE); + printArrayFloat(nums, SIZE); return 0; } \ No newline at end of file diff --git a/codes/c/utils/common_test.c b/codes/c/utils/common_test.c index 1f70e42ffa..a889b423b8 100644 --- a/codes/c/utils/common_test.c +++ b/codes/c/utils/common_test.c @@ -11,9 +11,6 @@ void testListNode() { int size = sizeof(nums) / sizeof(int); ListNode *head = arrToLinkedList(nums, size); printLinkedList(head); - - ListNode *node = getListNode(head, 5); - printf("find node: %d\n", node->val); } void testTreeNode() { diff --git a/codes/c/utils/list_node.h b/codes/c/utils/list_node.h index a98c6839ba..78db687843 100644 --- a/codes/c/utils/list_node.h +++ b/codes/c/utils/list_node.h @@ -26,7 +26,7 @@ ListNode *newListNode(int val) { return node; } -/* Generate a linked list with an array */ +/* 将数组反序列化为链表 */ ListNode *arrToLinkedList(const int *arr, size_t size) { if (size <= 0) { return NULL; @@ -41,15 +41,7 @@ ListNode *arrToLinkedList(const int *arr, size_t size) { return dummy->next; } -/* Get a list node with specific value from a linked list */ -ListNode *getListNode(ListNode *head, int val) { - while (head != NULL && head->val != val) { - head = head->next; - } - return head; -} - -/* Free the memory allocated to a linked list */ +/* 释放分配给链表的内存空间 */ void freeMemoryLinkedList(ListNode *cur) { // 释放内存 ListNode *pre; diff --git a/codes/c/utils/print_util.h b/codes/c/utils/print_util.h index 57699b0bb1..7a71a3ce84 100644 --- a/codes/c/utils/print_util.h +++ b/codes/c/utils/print_util.h @@ -18,7 +18,7 @@ extern "C" { #endif -/* Print an Array */ +/* 打印数组 */ void printArray(int arr[], int size) { if (arr == NULL || size == 0) { printf("[]"); @@ -31,7 +31,7 @@ void printArray(int arr[], int size) { printf("%d]\n", arr[size - 1]); } -/* Print an Array */ +/* 打印数组 */ void printArrayFloat(float arr[], int size) { if (arr == NULL || size == 0) { printf("[]"); @@ -44,7 +44,7 @@ void printArrayFloat(float arr[], int size) { printf("%.2f]\n", arr[size - 1]); } -/* Print a linked list */ +/* 打印链表 */ void printLinkedList(ListNode *node) { if (node == NULL) { return; @@ -69,7 +69,6 @@ Trunk *newTrunk(Trunk *prev, char *str) { return trunk; } -/* Helper function to print branches of the binary tree */ void showTrunks(Trunk *trunk) { if (trunk == NULL) { return; @@ -78,7 +77,11 @@ void showTrunks(Trunk *trunk) { printf("%s", trunk->str); } -/* Help to print a binary tree, hide more details */ +/** + * 打印二叉树 + * This tree printer is borrowed from TECHIE DELIGHT + * https://www.techiedelight.com/c-program-print-binary-tree/ + */ void printTreeHelper(TreeNode *node, Trunk *prev, bool isRight) { if (node == NULL) { return; @@ -106,12 +109,12 @@ void printTreeHelper(TreeNode *node, Trunk *prev, bool isRight) { printTreeHelper(node->left, trunk, false); } -/* Print a binary tree */ +/* 打印二叉树 */ void printTree(TreeNode *root) { printTreeHelper(root, NULL, false); } -/* Print a Heap */ +/* 打印堆 */ void printHeap(int arr[], int size) { TreeNode *root; printf("堆的数组表示:"); diff --git a/codes/cpp/utils/list_node.hpp b/codes/cpp/utils/list_node.hpp index 4dd39b7f51..7526fe608d 100644 --- a/codes/cpp/utils/list_node.hpp +++ b/codes/cpp/utils/list_node.hpp @@ -11,7 +11,7 @@ using namespace std; -/* Definition for a singly-linked list node */ +/* 链表节点 */ struct ListNode { int val; ListNode *next; @@ -19,7 +19,7 @@ struct ListNode { } }; -/* Generate a linked list with a vector */ +/* 将列表反序列化为链表 */ ListNode *vecToLinkedList(vector list) { ListNode *dum = new ListNode(0); ListNode *head = dum; @@ -30,15 +30,7 @@ ListNode *vecToLinkedList(vector list) { return dum->next; } -/* Get a list node with specific value from a linked list */ -ListNode *getListNode(ListNode *head, int val) { - while (head != nullptr && head->val != val) { - head = head->next; - } - return head; -} - -/* Free the memory allocated to a linked list */ +/* 释放分配给链表的内存空间 */ void freeMemoryLinkedList(ListNode *cur) { // 释放内存 ListNode *pre; diff --git a/codes/cpp/utils/print_utils.hpp b/codes/cpp/utils/print_utils.hpp index 4f65681c83..ff66fb2044 100644 --- a/codes/cpp/utils/print_utils.hpp +++ b/codes/cpp/utils/print_utils.hpp @@ -44,7 +44,7 @@ string strRepeat(string str, int n) { return os.str(); } -/* Print an Array */ +/* 打印数组 */ template void printArray(T *arr, int n) { cout << "["; for (int i = 0; i < n - 1; i++) { @@ -61,12 +61,12 @@ template string getVectorString(vector &list) { return "[" + strJoin(", ", list) + "]"; } -/* Print a vector */ +/* 打印列表 */ template void printVector(vector list) { cout << getVectorString(list) << '\n'; } -/* Print a vector matrix */ +/* 打印矩阵 */ template void printVectorMatrix(vector> &matrix) { cout << "[" << '\n'; for (vector &list : matrix) @@ -74,7 +74,7 @@ template void printVectorMatrix(vector> &matrix) { cout << "]" << '\n'; } -/* Print a linked list */ +/* 打印链表 */ void printLinkedList(ListNode *head) { vector list; while (head != nullptr) { @@ -85,10 +85,6 @@ void printLinkedList(ListNode *head) { cout << strJoin(" -> ", list) << '\n'; } -/** - * This tree printer is borrowed from TECHIE DELIGHT - * https://www.techiedelight.com/c-program-print-binary-tree/ - */ struct Trunk { Trunk *prev; string str; @@ -98,7 +94,6 @@ struct Trunk { } }; -/* Helper function to print branches of the binary tree */ void showTrunks(Trunk *p) { if (p == nullptr) { return; @@ -108,7 +103,11 @@ void showTrunks(Trunk *p) { cout << p->str; } -/* Print a binary tree */ +/** + * 打印二叉树 + * This tree printer is borrowed from TECHIE DELIGHT + * https://www.techiedelight.com/c-program-print-binary-tree/ + */ void printTree(TreeNode *root, Trunk *prev, bool isRight) { if (root == nullptr) { return; @@ -140,12 +139,12 @@ void printTree(TreeNode *root, Trunk *prev, bool isRight) { printTree(root->left, &trunk, false); } -/* The interface of the tree printer */ +/* 打印二叉树 */ void printTree(TreeNode *root) { printTree(root, nullptr, false); } -/* Print a stack */ +/* 打印栈 */ template void printStack(stack stk) { // Reverse the input stack stack tmp; @@ -167,7 +166,7 @@ template void printStack(stack stk) { cout << "[" + s.str() + "]" << '\n'; } -/* Print a queue */ +/* 打印队列 */ template void printQueue(queue queue) { // Generate the string to print ostringstream s; @@ -183,7 +182,7 @@ template void printQueue(queue queue) { cout << "[" + s.str() + "]" << '\n'; } -/* Print a deque */ +/* 打印双向队列 */ template void printDeque(deque deque) { // Generate the string to print ostringstream s; @@ -199,7 +198,7 @@ template void printDeque(deque deque) { cout << "[" + s.str() + "]" << '\n'; } -/* Print a HashMap */ +/* 打印哈希表 */ // 定义模板参数 TKey 和 TValue ,用于指定键值对的类型 template void printHashMap(unordered_map map) { for (auto kv : map) { @@ -217,7 +216,7 @@ template S &Container(priority_queue void printHeap(priority_queue &heap) { vector vec = Container(heap); cout << "堆的数组表示:"; diff --git a/codes/csharp/utils/ListNode.cs b/codes/csharp/utils/ListNode.cs index 430549815a..3205b49ed0 100644 --- a/codes/csharp/utils/ListNode.cs +++ b/codes/csharp/utils/ListNode.cs @@ -4,12 +4,12 @@ namespace hello_algo.utils; -/* Definition for a singly-linked list node */ +/* 链表节点 */ public class ListNode(int x) { public int val = x; public ListNode? next; - /* Generate a linked list with an array */ + /* 将数组反序列化为链表 */ public static ListNode? ArrToLinkedList(int[] arr) { ListNode dum = new(0); ListNode head = dum; @@ -20,14 +20,6 @@ public class ListNode(int x) { return dum.next; } - /* Get a list node with specific value from a linked list */ - public static ListNode? GetListNode(ListNode? head, int val) { - while (head != null && head.val != val) { - head = head.next; - } - return head; - } - public override string? ToString() { List list = []; var head = this; diff --git a/codes/csharp/utils/PrintUtil.cs b/codes/csharp/utils/PrintUtil.cs index 97870b1af7..b91f013251 100644 --- a/codes/csharp/utils/PrintUtil.cs +++ b/codes/csharp/utils/PrintUtil.cs @@ -12,7 +12,7 @@ public class Trunk(Trunk? prev, string str) { }; public static class PrintUtil { - /* Print a list */ + /* 打印列表 */ public static void PrintList(IList list) { Console.WriteLine("[" + string.Join(", ", list) + "]"); } @@ -21,7 +21,7 @@ public static class PrintUtil { return $"[ {string.Join(", ", list.Select(x => x?.ToString() ?? "null"))} ]"; } - /* Print a matrix (Array) */ + /* 打印矩阵 (Array) */ public static void PrintMatrix(T[][] matrix) { Console.WriteLine("["); foreach (T[] row in matrix) { @@ -30,7 +30,7 @@ public static class PrintUtil { Console.WriteLine("]"); } - /* Print a matrix (List) */ + /* 打印矩阵 (List) */ public static void PrintMatrix(List> matrix) { Console.WriteLine("["); foreach (List row in matrix) { @@ -39,7 +39,7 @@ public static class PrintUtil { Console.WriteLine("]"); } - /* Print a linked list */ + /* 打印链表 */ public static void PrintLinkedList(ListNode? head) { List list = []; while (head != null) { @@ -50,7 +50,7 @@ public static class PrintUtil { } /** - * The interface of the tree printer + * 打印二叉树 * This tree printer is borrowed from TECHIE DELIGHT * https://www.techiedelight.com/c-program-print-binary-tree/ */ @@ -58,7 +58,7 @@ public static class PrintUtil { PrintTree(root, null, false); } - /* Print a binary tree */ + /* 打印二叉树 */ public static void PrintTree(TreeNode? root, Trunk? prev, bool isRight) { if (root == null) { return; @@ -90,7 +90,6 @@ public static class PrintUtil { PrintTree(root.left, trunk, false); } - /* Helper function to print branches of the binary tree */ public static void ShowTrunks(Trunk? p) { if (p == null) { return; @@ -100,14 +99,14 @@ public static class PrintUtil { Console.Write(p.str); } - /* Print a hash map */ + /* 打印哈希表 */ public static void PrintHashMap(Dictionary map) where K : notnull { foreach (var kv in map.Keys) { Console.WriteLine(kv.ToString() + " -> " + map[kv]?.ToString()); } } - /* Print a heap */ + /* 打印堆 */ public static void PrintHeap(Queue queue) { Console.Write("堆的数组表示:"); List list = [.. queue]; @@ -117,7 +116,7 @@ public static class PrintUtil { PrintTree(tree); } - /* Print a PriorityQueue */ + /* 打印优先队列 */ public static void PrintHeap(PriorityQueue queue) { var newQueue = new PriorityQueue(queue.UnorderedItems, queue.Comparer); Console.Write("堆的数组表示:"); diff --git a/codes/dart/utils/list_node.dart b/codes/dart/utils/list_node.dart index c895d4e10f..b77f61d1c3 100644 --- a/codes/dart/utils/list_node.dart +++ b/codes/dart/utils/list_node.dart @@ -4,7 +4,7 @@ * Author: Jefferson (JeffersonHuang77@gmail.com) */ -/* Definition for a singly-linked list node */ +/* 链表节点 */ class ListNode { int val; ListNode? next; @@ -12,7 +12,7 @@ class ListNode { ListNode(this.val, [this.next]); } -/* Generate a linked list with a list */ +/* 将列表反序列化为链表 */ ListNode? listToLinkedList(List list) { ListNode dum = ListNode(0); ListNode? head = dum; diff --git a/codes/dart/utils/print_util.dart b/codes/dart/utils/print_util.dart index df3fbfd3c1..49cccc2244 100644 --- a/codes/dart/utils/print_util.dart +++ b/codes/dart/utils/print_util.dart @@ -16,7 +16,7 @@ class Trunk { Trunk(this.prev, this.str); } -/* Print a matrix (Array) */ +/* 打印矩阵 (Array) */ void printMatrix(List> matrix) { print("["); for (List row in matrix) { @@ -25,7 +25,7 @@ void printMatrix(List> matrix) { print("]"); } -/* Print a linked list */ +/* 打印链表 */ void printLinkedList(ListNode? head) { List list = []; @@ -38,7 +38,7 @@ void printLinkedList(ListNode? head) { } /** - * The interface of the tree printer + * 打印二叉树 * This tree printer is borrowed from TECHIE DELIGHT * https://www.techiedelight.com/c-program-print-binary-tree/ */ @@ -72,7 +72,6 @@ void printTree(TreeNode? root, [Trunk? prev = null, bool isRight = false]) { printTree(root.left, trunk, false); } -/* Helper function to print branches of the binary tree */ void showTrunks(Trunk? p) { if (p == null) { return; @@ -82,7 +81,7 @@ void showTrunks(Trunk? p) { stdout.write(p.str); } -/* Print a heap (PriorityQueue) */ +/* 打印堆 */ void printHeap(List heap) { print("堆的数组表示:$heap"); print("堆的树状表示:"); diff --git a/codes/go/chapter_stack_and_queue/array_deque.go b/codes/go/chapter_stack_and_queue/array_deque.go index acaa4095c7..85d98aca5c 100644 --- a/codes/go/chapter_stack_and_queue/array_deque.go +++ b/codes/go/chapter_stack_and_queue/array_deque.go @@ -64,7 +64,7 @@ func (q *arrayDeque) pushLast(num int) { } // 计算队尾指针,指向队尾索引 + 1 rear := q.index(q.front + q.queSize) - // 将 num 添加至队首 + // 将 num 添加至队尾 q.nums[rear] = num q.queSize++ } diff --git a/codes/go/pkg/list_node.go b/codes/go/pkg/list_node.go index f1c152de1a..877f8563d3 100644 --- a/codes/go/pkg/list_node.go +++ b/codes/go/pkg/list_node.go @@ -4,13 +4,13 @@ package pkg -// ListNode Definition for a singly-linked list node +// ListNode 链表节点 type ListNode struct { Next *ListNode Val int } -// NewListNode Generate a list node with an val +// NewListNode 链表节点构造函数 func NewListNode(v int) *ListNode { return &ListNode{ Next: nil, @@ -18,7 +18,7 @@ func NewListNode(v int) *ListNode { } } -// ArrayToLinkedList Generate a linked list with an array +// ArrayToLinkedList 将数组反序列化为链表 func ArrayToLinkedList(arr []int) *ListNode { // dummy header of linked list dummy := NewListNode(0) @@ -29,11 +29,3 @@ func ArrayToLinkedList(arr []int) *ListNode { } return dummy.Next } - -// GetListNode Get a list node with specific value from a linked list -func GetListNode(node *ListNode, val int) *ListNode { - for node != nil && node.Val != val { - node = node.Next - } - return node -} diff --git a/codes/go/pkg/list_node_test.go b/codes/go/pkg/list_node_test.go index 31b99618f8..e61d8d5bff 100644 --- a/codes/go/pkg/list_node_test.go +++ b/codes/go/pkg/list_node_test.go @@ -5,7 +5,6 @@ package pkg import ( - "fmt" "testing" ) @@ -14,6 +13,4 @@ func TestListNode(t *testing.T) { head := ArrayToLinkedList(arr) PrintLinkedList(head) - node := GetListNode(head, 5) - fmt.Println("Find node: ", node.Val) } diff --git a/codes/go/pkg/print_utils.go b/codes/go/pkg/print_utils.go index fb16013a67..ab952150ef 100644 --- a/codes/go/pkg/print_utils.go +++ b/codes/go/pkg/print_utils.go @@ -11,13 +11,13 @@ import ( "strings" ) -// PrintSlice Print a slice +// PrintSlice 打印切片 func PrintSlice[T any](nums []T) { fmt.Printf("%v", nums) fmt.Println() } -// PrintList Print a list +// PrintList 打印列表 func PrintList(list *list.List) { if list.Len() == 0 { fmt.Print("[]\n") @@ -33,14 +33,14 @@ func PrintList(list *list.List) { fmt.Print(e.Value, "]\n") } -// PrintMap Print a hash map +// PrintMap 打印哈希表 func PrintMap[K comparable, V any](m map[K]V) { for key, value := range m { fmt.Println(key, "->", value) } } -// PrintHeap Print a heap +// PrintHeap 打印堆 func PrintHeap(h []any) { fmt.Printf("堆的数组表示:") fmt.Printf("%v", h) @@ -49,7 +49,7 @@ func PrintHeap(h []any) { PrintTree(root) } -// PrintLinkedList Print a linked list +// PrintLinkedList 打印链表 func PrintLinkedList(node *ListNode) { if node == nil { return @@ -63,12 +63,12 @@ func PrintLinkedList(node *ListNode) { fmt.Println(builder.String()) } -// PrintTree Print a binary tree +// PrintTree 打印二叉树 func PrintTree(root *TreeNode) { printTreeHelper(root, nil, false) } -// printTreeHelper Help to print a binary tree, hide more details +// printTreeHelper 打印二叉树 // This tree printer is borrowed from TECHIE DELIGHT // https://www.techiedelight.com/c-program-print-binary-tree/ func printTreeHelper(root *TreeNode, prev *trunk, isRight bool) { @@ -96,7 +96,6 @@ func printTreeHelper(root *TreeNode, prev *trunk, isRight bool) { printTreeHelper(root.Left, trunk, false) } -// trunk Help to print tree structure type trunk struct { prev *trunk str string diff --git a/codes/go/pkg/tree_node.go b/codes/go/pkg/tree_node.go index c5c7434c15..80c1eab358 100644 --- a/codes/go/pkg/tree_node.go +++ b/codes/go/pkg/tree_node.go @@ -4,6 +4,7 @@ package pkg +// TreeNode 二叉树节点 type TreeNode struct { Val any // 节点值 Height int // 节点高度 @@ -11,6 +12,7 @@ type TreeNode struct { Right *TreeNode // 右子节点引用 } +// NewTreeNode 二叉树节点构造函数 func NewTreeNode(v any) *TreeNode { return &TreeNode{ Val: v, diff --git a/codes/go/pkg/vertex.go b/codes/go/pkg/vertex.go index 5bab0724e5..19190d7a31 100644 --- a/codes/go/pkg/vertex.go +++ b/codes/go/pkg/vertex.go @@ -9,14 +9,14 @@ type Vertex struct { Val int } -// NewVertex 构造函数 +// NewVertex 顶点构造函数 func NewVertex(val int) Vertex { return Vertex{ Val: val, } } -// ValsToVets Generate a vertex list tree given an array +// ValsToVets 将值列表反序列化为顶点列表 func ValsToVets(vals []int) []Vertex { vets := make([]Vertex, len(vals)) for i := 0; i < len(vals); i++ { @@ -25,7 +25,7 @@ func ValsToVets(vals []int) []Vertex { return vets } -// VetsToVals Serialize given vertex list to a value list +// VetsToVals 将顶点列表序列化为值列表 func VetsToVals(vets []Vertex) []int { vals := make([]int, len(vets)) for i := range vets { diff --git a/codes/java/utils/ListNode.java b/codes/java/utils/ListNode.java index 485aa8fd63..8e1d4d1279 100755 --- a/codes/java/utils/ListNode.java +++ b/codes/java/utils/ListNode.java @@ -6,7 +6,7 @@ package utils; -/* Definition for a singly-linked list node */ +/* 链表节点 */ public class ListNode { public int val; public ListNode next; @@ -15,7 +15,7 @@ public ListNode(int x) { val = x; } - /* Generate a linked list with an array */ + /* 将列表反序列化为链表 */ public static ListNode arrToLinkedList(int[] arr) { ListNode dum = new ListNode(0); ListNode head = dum; @@ -25,12 +25,4 @@ public static ListNode arrToLinkedList(int[] arr) { } return dum.next; } - - /* Get a list node with specific value from a linked list */ - public static ListNode getListNode(ListNode head, int val) { - while (head != null && head.val != val) { - head = head.next; - } - return head; - } } diff --git a/codes/java/utils/PrintUtil.java b/codes/java/utils/PrintUtil.java index 2a046644e0..e8ff8ff524 100755 --- a/codes/java/utils/PrintUtil.java +++ b/codes/java/utils/PrintUtil.java @@ -19,8 +19,7 @@ class Trunk { }; public class PrintUtil { - - /* Print a matrix (Array) */ + /* 打印矩阵(Array) */ public static void printMatrix(T[][] matrix) { System.out.println("["); for (T[] row : matrix) { @@ -29,7 +28,7 @@ public static void printMatrix(T[][] matrix) { System.out.println("]"); } - /* Print a matrix (List) */ + /* 打印矩阵(List) */ public static void printMatrix(List> matrix) { System.out.println("["); for (List row : matrix) { @@ -38,7 +37,7 @@ public static void printMatrix(List> matrix) { System.out.println("]"); } - /* Print a linked list */ + /* 打印链表 */ public static void printLinkedList(ListNode head) { List list = new ArrayList<>(); while (head != null) { @@ -48,16 +47,16 @@ public static void printLinkedList(ListNode head) { System.out.println(String.join(" -> ", list)); } - /** - * The interface of the tree printer - * This tree printer is borrowed from TECHIE DELIGHT - * https://www.techiedelight.com/c-program-print-binary-tree/ - */ + /* 打印二叉树 */ public static void printTree(TreeNode root) { printTree(root, null, false); } - /* Print a binary tree */ + /** + * 打印二叉树 + * This tree printer is borrowed from TECHIE DELIGHT + * https://www.techiedelight.com/c-program-print-binary-tree/ + */ public static void printTree(TreeNode root, Trunk prev, boolean isRight) { if (root == null) { return; @@ -89,7 +88,6 @@ public static void printTree(TreeNode root, Trunk prev, boolean isRight) { printTree(root.left, trunk, false); } - /* Helper function to print branches of the binary tree */ public static void showTrunks(Trunk p) { if (p == null) { return; @@ -99,14 +97,14 @@ public static void showTrunks(Trunk p) { System.out.print(p.str); } - /* Print a hash map */ + /* 打印哈希表 */ public static void printHashMap(Map map) { for (Map.Entry kv : map.entrySet()) { System.out.println(kv.getKey() + " -> " + kv.getValue()); } } - /* Print a heap (PriorityQueue) */ + /* 打印堆(优先队列) */ public static void printHeap(Queue queue) { List list = new ArrayList<>(queue); System.out.print("堆的数组表示:"); diff --git a/codes/javascript/modules/ListNode.js b/codes/javascript/modules/ListNode.js index 7157781963..5a7cd0c222 100755 --- a/codes/javascript/modules/ListNode.js +++ b/codes/javascript/modules/ListNode.js @@ -4,9 +4,7 @@ * Author: IsChristina (christinaxia77@foxmail.com) */ -/** - * Definition for a singly-linked list node - */ +/* 链表节点 */ class ListNode { val; // 节点值 next; // 指向下一节点的引用(指针) @@ -16,11 +14,7 @@ class ListNode { } } -/** - * Generate a linked list with an array - * @param arr - * @return - */ +/* 将列表反序列化为链表 */ function arrToLinkedList(arr) { const dum = new ListNode(0); let head = dum; @@ -31,21 +25,7 @@ function arrToLinkedList(arr) { return dum.next; } -/** - * Get a list node with specific value from a linked list - * @param head - * @param val - * @return - */ -function getListNode(head, val) { - while (head !== null && head.val !== val) { - head = head.next; - } - return head; -} - module.exports = { ListNode, arrToLinkedList, - getListNode, }; diff --git a/codes/javascript/modules/PrintUtil.js b/codes/javascript/modules/PrintUtil.js index 4d5de36787..77787d3f0b 100644 --- a/codes/javascript/modules/PrintUtil.js +++ b/codes/javascript/modules/PrintUtil.js @@ -6,10 +6,7 @@ const { arrToTree } = require('./TreeNode'); -/** - * Print a linked list - * @param head - */ +/* 打印链表 */ function printLinkedList(head) { let list = []; while (head !== null) { @@ -25,21 +22,15 @@ function Trunk(prev, str) { } /** - * The interface of the tree printer + * 打印二叉树 * This tree printer is borrowed from TECHIE DELIGHT * https://www.techiedelight.com/c-program-print-binary-tree/ - * @param root */ function printTree(root) { printTree(root, null, false); } -/** - * Print a binary tree - * @param root - * @param prev - * @param isRight - */ +/* 打印二叉树 */ function printTree(root, prev, isRight) { if (root === null) { return; @@ -71,10 +62,6 @@ function printTree(root, prev, isRight) { printTree(root.left, trunk, false); } -/** - * Helper function to print branches of the binary tree - * @param p - */ function showTrunks(p) { if (!p) { return; @@ -84,10 +71,7 @@ function showTrunks(p) { process.stdout.write(p.str); } -/** - * Print a heap - * @param arr - */ +/* 打印堆 */ function printHeap(arr) { console.log('堆的数组表示:'); console.log(arr); diff --git a/codes/javascript/modules/TreeNode.js b/codes/javascript/modules/TreeNode.js index bdde3519d0..d3263c35ea 100644 --- a/codes/javascript/modules/TreeNode.js +++ b/codes/javascript/modules/TreeNode.js @@ -4,9 +4,7 @@ * Author: IsChristina (christinaxia77@foxmail.com) */ -/** - * Definition for a binary tree node. - */ +/* 二叉树节点 */ class TreeNode { val; // 节点值 left; // 左子节点指针 @@ -20,11 +18,7 @@ class TreeNode { } } -/** - * Generate a binary tree given an array - * @param arr - * @return - */ +/* 将数组反序列化为二叉树 */ function arrToTree(arr, i = 0) { if (i < 0 || i >= arr.length || arr[i] === null) { return null; diff --git a/codes/kotlin/utils/ListNode.kt b/codes/kotlin/utils/ListNode.kt index 82c1bf296e..69b87356d0 100644 --- a/codes/kotlin/utils/ListNode.kt +++ b/codes/kotlin/utils/ListNode.kt @@ -11,7 +11,7 @@ class ListNode(var value: Int) { var next: ListNode? = null companion object { - /* 将列表序列化为链表 */ + /* 将列表反序列化为链表 */ fun arrToLinkedList(arr: IntArray): ListNode? { val dum = ListNode(0) var head = dum @@ -21,14 +21,5 @@ class ListNode(var value: Int) { } return dum.next } - - /* 获取链表中值为 value 的节点 */ - fun getListNode(h: ListNode, value: Int): ListNode { - var head = h - while (head.value != value) { - head = head.next!! - } - return head - } } } diff --git a/codes/python/modules/list_node.py b/codes/python/modules/list_node.py index 521e1e548e..ee51f234b0 100644 --- a/codes/python/modules/list_node.py +++ b/codes/python/modules/list_node.py @@ -14,7 +14,7 @@ def __init__(self, val: int): def list_to_linked_list(arr: list[int]) -> ListNode | None: - """将列表序列化为链表""" + """将列表反序列化为链表""" dum = head = ListNode(0) for a in arr: node = ListNode(a) @@ -24,7 +24,7 @@ def list_to_linked_list(arr: list[int]) -> ListNode | None: def linked_list_to_list(head: ListNode | None) -> list[int]: - """将链表反序列化为列表""" + """将链表序列化为列表""" arr: list[int] = [] while head: arr.append(head.val) diff --git a/codes/ruby/utils/list_node.rb b/codes/ruby/utils/list_node.rb index b8fe7e58bd..12bf2676a2 100644 --- a/codes/ruby/utils/list_node.rb +++ b/codes/ruby/utils/list_node.rb @@ -15,7 +15,7 @@ def initialize(val=nil, next_node=nil) end end -### 将列表序列化为链表 ### +### 将列表反序列化为链表 ### def arr_to_linked_list(arr) head = current = ListNode.new arr[0] @@ -27,7 +27,7 @@ def arr_to_linked_list(arr) head end -### 将链表反序列化为列表 ### +### 将链表序列化为列表 ### def linked_list_to_arr(head) arr = [] diff --git a/codes/rust/include/list_node.rs b/codes/rust/include/list_node.rs index a29b853237..68cf804140 100644 --- a/codes/rust/include/list_node.rs +++ b/codes/rust/include/list_node.rs @@ -19,7 +19,7 @@ impl ListNode { Rc::new(RefCell::new(ListNode { val, next: None })) } - /* Generate a linked list with an array */ + /* 将数组反序列化为链表 */ pub fn arr_to_linked_list(array: &[T]) -> Option>>> where T: Copy + Clone, @@ -36,7 +36,7 @@ impl ListNode { head } - /* Generate a hashmap with a linked_list */ + /* 将链表转化为哈希表 */ pub fn linked_list_to_hashmap( linked_list: Option>>>, ) -> HashMap>>> diff --git a/codes/rust/include/print_util.rs b/codes/rust/include/print_util.rs index 47cb858028..68b4870a2f 100644 --- a/codes/rust/include/print_util.rs +++ b/codes/rust/include/print_util.rs @@ -17,7 +17,7 @@ struct Trunk<'a, 'b> { str: Cell<&'b str>, } -/* Print an array */ +/* 打印数组 */ pub fn print_array(nums: &[T]) { print!("["); if nums.len() > 0 { @@ -29,14 +29,14 @@ pub fn print_array(nums: &[T]) { } } -/* Print a hash map */ +/* 打印哈希表 */ pub fn print_hash_map(map: &HashMap) { for (key, value) in map { println!("{key} -> {value}"); } } -/* Print a queue or deque */ +/* 打印队列(双向队列) */ pub fn print_queue(queue: &VecDeque) { print!("["); let iter = queue.iter(); @@ -45,7 +45,7 @@ pub fn print_queue(queue: &VecDeque) { } } -/* Print a linked list */ +/* 打印链表 */ pub fn print_linked_list(head: &Rc>>) { print!("{}{}", head.borrow().val, if head.borrow().next.is_none() {"\n"} else {" -> "}); if let Some(node) = &head.borrow().next { @@ -53,10 +53,12 @@ pub fn print_linked_list(head: &Rc>>) { } } +/* 打印二叉树 */ pub fn print_tree(root: &Rc>) { _print_tree(Some(root), None, false); } +/* 打印二叉树 */ fn _print_tree(root: Option<&Rc>>, prev: Option<&Trunk>, is_right: bool) { if let Some(node) = root { let mut prev_str = " "; @@ -91,7 +93,7 @@ fn show_trunks(trunk: Option<&Trunk>) { } } -/* Print a heap both in array and tree representations */ +/* 打印堆 */ pub fn print_heap(heap: Vec) { println!("堆的数组表示:{:?}", heap); println!("堆的树状表示:"); diff --git a/codes/typescript/modules/ListNode.ts b/codes/typescript/modules/ListNode.ts index 95d8d72437..b42905f585 100644 --- a/codes/typescript/modules/ListNode.ts +++ b/codes/typescript/modules/ListNode.ts @@ -4,9 +4,7 @@ * Author: Justin (xiefahit@gmail.com) */ -/** - * Definition for a singly-linked list node - */ +/* 链表节点 */ class ListNode { val: number; next: ListNode | null; @@ -16,11 +14,7 @@ class ListNode { } } -/** - * Generate a linked list with an array - * @param arr - * @return - */ +/* 将数组反序列化为链表 */ function arrToLinkedList(arr: number[]): ListNode | null { const dum: ListNode = new ListNode(0); let head = dum; @@ -31,17 +25,4 @@ function arrToLinkedList(arr: number[]): ListNode | null { return dum.next; } -/** - * Get a list node with specific value from a linked list - * @param head - * @param val - * @return - */ -function getListNode(head: ListNode | null, val: number): ListNode | null { - while (head !== null && head.val !== val) { - head = head.next; - } - return head; -} - -export { ListNode, arrToLinkedList, getListNode }; +export { ListNode, arrToLinkedList }; diff --git a/codes/typescript/modules/PrintUtil.ts b/codes/typescript/modules/PrintUtil.ts index d4b2ed36f6..93dad2d1c4 100644 --- a/codes/typescript/modules/PrintUtil.ts +++ b/codes/typescript/modules/PrintUtil.ts @@ -7,10 +7,7 @@ import { ListNode } from './ListNode'; import { TreeNode, arrToTree } from './TreeNode'; -/** - * Print a linked list - * @param head - */ +/* 打印链表 */ function printLinkedList(head: ListNode | null): void { const list: string[] = []; while (head !== null) { @@ -31,21 +28,15 @@ class Trunk { } /** - * The interface of the tree printer + * 打印二叉树 * This tree printer is borrowed from TECHIE DELIGHT * https://www.techiedelight.com/c-program-print-binary-tree/ - * @param root */ function printTree(root: TreeNode | null) { printTreeHelper(root, null, false); } -/** - * Print a binary tree - * @param root - * @param prev - * @param isRight - */ +/* 打印二叉树 */ function printTreeHelper( root: TreeNode | null, prev: Trunk | null, @@ -81,10 +72,6 @@ function printTreeHelper( printTreeHelper(root.left, trunk, false); } -/** - * Helper function to print branches of the binary tree - * @param p - */ function showTrunks(p: Trunk | null) { if (p === null) { return; @@ -97,10 +84,7 @@ function showTrunks(p: Trunk | null) { // restart the vscode } -/** - * Print a heap - * @param arr - */ +/* 打印堆 */ function printHeap(arr: number[]): void { console.log('堆的数组表示:'); console.log(arr); diff --git a/codes/typescript/modules/TreeNode.ts b/codes/typescript/modules/TreeNode.ts index e91232db0c..26e39f3721 100644 --- a/codes/typescript/modules/TreeNode.ts +++ b/codes/typescript/modules/TreeNode.ts @@ -4,9 +4,7 @@ * Author: Justin (xiefahit@gmail.com) */ -/** - * Definition for a binary tree node. - */ +/* 二叉树节点 */ class TreeNode { val: number; // 节点值 height: number; // 节点高度 @@ -25,11 +23,7 @@ class TreeNode { } } -/** - * Generate a binary tree given an array - * @param arr - * @return - */ +/* 将数组反序列化为二叉树 */ function arrToTree(arr: (number | null)[], i: number = 0): TreeNode | null { if (i < 0 || i >= arr.length || arr[i] === null) { return null; diff --git a/codes/zig/include/ListNode.zig b/codes/zig/include/ListNode.zig index 4f30b81d5e..f88f222fa4 100644 --- a/codes/zig/include/ListNode.zig +++ b/codes/zig/include/ListNode.zig @@ -4,7 +4,7 @@ const std = @import("std"); -// Definition for a singly-linked list node +// 链表节点 pub fn ListNode(comptime T: type) type { return struct { const Self = @This(); @@ -20,7 +20,7 @@ pub fn ListNode(comptime T: type) type { }; } -// Generate a linked list with a list +// 将列表反序列化为链表 pub fn listToLinkedList(comptime T: type, mem_allocator: std.mem.Allocator, list: std.ArrayList(T)) !?*ListNode(T) { var dum = try mem_allocator.create(ListNode(T)); dum.init(0); @@ -34,7 +34,7 @@ pub fn listToLinkedList(comptime T: type, mem_allocator: std.mem.Allocator, list return dum.next; } -// Generate a linked list with an array +// 将数组反序列化为链表 pub fn arrToLinkedList(comptime T: type, mem_allocator: std.mem.Allocator, arr: []T) !?*ListNode(T) { var dum = try mem_allocator.create(ListNode(T)); dum.init(0); diff --git a/codes/zig/include/PrintUtil.zig b/codes/zig/include/PrintUtil.zig index 22d9aac2d9..638082943c 100644 --- a/codes/zig/include/PrintUtil.zig +++ b/codes/zig/include/PrintUtil.zig @@ -8,7 +8,7 @@ pub const ListNode = ListUtil.ListNode; pub const TreeUtil = @import("TreeNode.zig"); pub const TreeNode = TreeUtil.TreeNode; -// Print an array +// 打印数组 pub fn printArray(comptime T: type, nums: []T) void { std.debug.print("[", .{}); if (nums.len > 0) { @@ -20,7 +20,7 @@ pub fn printArray(comptime T: type, nums: []T) void { } } -// Print a list +// 打印列表 pub fn printList(comptime T: type, list: std.ArrayList(T)) void { std.debug.print("[", .{}); if (list.items.len > 0) { @@ -32,7 +32,7 @@ pub fn printList(comptime T: type, list: std.ArrayList(T)) void { } } -// Print a linked list +// 打印链表 pub fn printLinkedList(comptime T: type, node: ?*ListNode(T)) !void { if (node == null) return; var list = std.ArrayList(T).init(std.heap.page_allocator); @@ -47,7 +47,7 @@ pub fn printLinkedList(comptime T: type, node: ?*ListNode(T)) !void { } } -// Print a queue or deque +// 打印队列 pub fn printQueue(comptime T: type, queue: std.TailQueue(T)) void { var node = queue.first; std.debug.print("[", .{}); @@ -59,7 +59,7 @@ pub fn printQueue(comptime T: type, queue: std.TailQueue(T)) void { } } -// Print a hash map +// 打印哈希表 pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHashMap(TKey, TValue)) void { var it = map.iterator(); while (it.next()) |kv| { @@ -69,7 +69,7 @@ pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHas } } -// print a heap (PriorityQueue) +// 打印堆 pub fn printHeap(comptime T: type, mem_allocator: std.mem.Allocator, queue: anytype) !void { var arr = queue.items; var len = queue.len; @@ -80,6 +80,7 @@ pub fn printHeap(comptime T: type, mem_allocator: std.mem.Allocator, queue: anyt try printTree(root, null, false); } +// 打印二叉树 // This tree printer is borrowed from TECHIE DELIGHT // https://www.techiedelight.com/c-program-print-binary-tree/ const Trunk = struct { @@ -92,15 +93,13 @@ const Trunk = struct { } }; -// Helper function to print branches of the binary tree pub fn showTrunks(p: ?*Trunk) void { if (p == null) return; showTrunks(p.?.prev); std.debug.print("{s}", .{p.?.str}); } -// The interface of the tree printer -// Print a binary tree +// 打印二叉树 pub fn printTree(root: ?*TreeNode(i32), prev: ?*Trunk, isRight: bool) !void { if (root == null) { return; diff --git a/codes/zig/include/TreeNode.zig b/codes/zig/include/TreeNode.zig index ee2116b841..ebcdbd3f78 100644 --- a/codes/zig/include/TreeNode.zig +++ b/codes/zig/include/TreeNode.zig @@ -4,7 +4,7 @@ const std = @import("std"); -// Definition for a binary tree node +// 二叉树节点 pub fn TreeNode(comptime T: type) type { return struct { const Self = @This(); @@ -24,7 +24,7 @@ pub fn TreeNode(comptime T: type) type { }; } -// Generate a binary tree with an array +// 将数组反序列化为二叉树 pub fn arrToTree(comptime T: type, mem_allocator: std.mem.Allocator, arr: []T) !?*TreeNode(T) { if (arr.len == 0) return null; var root = try mem_allocator.create(TreeNode(T)); diff --git a/docs/chapter_appendix/terminology.md b/docs/chapter_appendix/terminology.md index 091365b7b4..56a1bcfd52 100644 --- a/docs/chapter_appendix/terminology.md +++ b/docs/chapter_appendix/terminology.md @@ -106,7 +106,7 @@ | adjacency list | 邻接表 | 鄰接表 | | breadth-first search | 广度优先搜索 | 廣度優先搜尋 | | depth-first search | 深度优先搜索 | 深度優先搜尋 | -| binary search | 二分查找 | 二分查找 | +| binary search | 二分查找 | 二分搜尋 | | searching algorithm | 搜索算法 | 搜尋演算法 | | sorting algorithm | 排序算法 | 排序演算法 | | selection sort | 选择排序 | 選擇排序 | @@ -130,7 +130,7 @@ | $n$-queens problem | $n$ 皇后问题 | $n$ 皇后問題 | | dynamic programming | 动态规划 | 動態規劃 | | initial state | 初始状态 | 初始狀態 | -| state-transition equation | 状态转移方程 | 狀態轉移方程 | +| state-transition equation | 状态转移方程 | 狀態轉移方程 | | knapsack problem | 背包问题 | 背包問題 | | edit distance problem | 编辑距离问题 | 編輯距離問題 | | greedy algorithm | 贪心算法 | 貪婪演算法 | diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index ab03ebcb5b..dffa376bb0 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -700,7 +700,7 @@ } } /* 递归 O(n) */ - void recur(n: i32) { + fn recur(n: i32) { if n == 1 { return; } diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index f9d35a799e..d65c51b4b7 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -283,7 +283,7 @@ int size = deque.length; /* 判断双向队列是否为空 */ - bool isEmpty = deque.isEmpty;W + bool isEmpty = deque.isEmpty; ``` === "Rust"