From 9eeefff447339b0184dfcb908aaceec8bc1886ea Mon Sep 17 00:00:00 2001 From: hpstory <33348162+hpstory@users.noreply.github.com> Date: Fri, 21 Apr 2023 14:59:22 +0800 Subject: [PATCH] refactor: add/refactor method in include, simplified print code (#471) --- .../backtrack_find_constrained_paths.cs | 9 +-- .../preorder_find_constrained_paths.cs | 9 +-- .../preorder_find_nodes.cs | 10 +--- .../preorder_find_paths.cs | 9 +-- .../space_complexity.cs | 2 +- .../chapter_graph/graph_adjacency_list.cs | 6 +- .../chapter_graph/graph_adjacency_matrix.cs | 2 +- codes/csharp/chapter_graph/graph_bfs.cs | 4 +- codes/csharp/chapter_graph/graph_dfs.cs | 4 +- .../csharp/chapter_hashing/array_hash_map.cs | 18 +++--- codes/csharp/chapter_hashing/hash_map.cs | 10 ++-- codes/csharp/chapter_heap/heap.cs | 6 +- codes/csharp/chapter_heap/my_heap.cs | 2 +- .../chapter_stack_and_queue/array_stack.cs | 4 +- codes/csharp/chapter_stack_and_queue/deque.cs | 6 +- .../linkedlist_queue.cs | 4 +- .../linkedlist_stack.cs | 4 +- codes/csharp/chapter_stack_and_queue/queue.cs | 4 +- codes/csharp/chapter_stack_and_queue/stack.cs | 4 +- codes/csharp/chapter_tree/binary_tree_bfs.cs | 4 +- codes/csharp/chapter_tree/binary_tree_dfs.cs | 8 +-- codes/csharp/include/PrintUtil.cs | 60 +++++++++++-------- codes/csharp/include/TreeNode.cs | 12 ++-- codes/csharp/include/Vertex.cs | 10 ++-- 24 files changed, 102 insertions(+), 109 deletions(-) diff --git a/codes/csharp/chapter_backtracking/backtrack_find_constrained_paths.cs b/codes/csharp/chapter_backtracking/backtrack_find_constrained_paths.cs index 02389eba..38f64ef0 100644 --- a/codes/csharp/chapter_backtracking/backtrack_find_constrained_paths.cs +++ b/codes/csharp/chapter_backtracking/backtrack_find_constrained_paths.cs @@ -70,7 +70,7 @@ public class backtrack_find_constrained_paths [Test] public void Test() { - TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 }); + TreeNode root = TreeNode.ListToTree(new List { 1, 7, 3, 4, 5, 6, 7 }); Console.WriteLine("\n初始化二叉树"); PrintUtil.PrintTree(root); @@ -82,12 +82,7 @@ public class backtrack_find_constrained_paths Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点"); foreach (List path in res) { - List vals = new List(); - foreach (TreeNode node in path) - { - vals.Add(node.val); - } - Console.WriteLine(string.Join(" ", vals)); + PrintUtil.PrintList(path.Select(p => p.val).ToList()); } } } \ No newline at end of file diff --git a/codes/csharp/chapter_backtracking/preorder_find_constrained_paths.cs b/codes/csharp/chapter_backtracking/preorder_find_constrained_paths.cs index 0a545ef9..0ed13a04 100644 --- a/codes/csharp/chapter_backtracking/preorder_find_constrained_paths.cs +++ b/codes/csharp/chapter_backtracking/preorder_find_constrained_paths.cs @@ -38,7 +38,7 @@ public class preorder_find_constrained_paths [Test] public void Test() { - TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 }); + TreeNode root = TreeNode.ListToTree(new List { 1, 7, 3, 4, 5, 6, 7 }); Console.WriteLine("\n初始化二叉树"); PrintUtil.PrintTree(root); @@ -50,12 +50,7 @@ public class preorder_find_constrained_paths Console.WriteLine("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点"); foreach (List path in res) { - List vals = new List(); - foreach (TreeNode node in path) - { - vals.Add(node.val); - } - Console.WriteLine(string.Join(" ", vals)); + PrintUtil.PrintList(path.Select(p => p.val).ToList()); } } } \ No newline at end of file diff --git a/codes/csharp/chapter_backtracking/preorder_find_nodes.cs b/codes/csharp/chapter_backtracking/preorder_find_nodes.cs index 76c383cc..ab971171 100644 --- a/codes/csharp/chapter_backtracking/preorder_find_nodes.cs +++ b/codes/csharp/chapter_backtracking/preorder_find_nodes.cs @@ -6,6 +6,7 @@ using hello_algo.include; using NUnit.Framework; +using System.IO; namespace hello_algo.chapter_backtracking; @@ -32,7 +33,7 @@ public class preorder_find_nodes [Test] public void Test() { - TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 }); + TreeNode root = TreeNode.ListToTree(new List { 1, 7, 3, 4, 5, 6, 7 }); Console.WriteLine("\n初始化二叉树"); PrintUtil.PrintTree(root); @@ -41,11 +42,6 @@ public class preorder_find_nodes preOrder(root); Console.WriteLine("\n输出所有值为 7 的节点"); - List vals = new List(); - foreach (TreeNode node in res) - { - vals.Add(node.val); - } - Console.WriteLine(string.Join(" ", vals)); + PrintUtil.PrintList(res.Select(p => p.val).ToList()); } } \ No newline at end of file diff --git a/codes/csharp/chapter_backtracking/preorder_find_paths.cs b/codes/csharp/chapter_backtracking/preorder_find_paths.cs index ae6503a9..bc72c684 100644 --- a/codes/csharp/chapter_backtracking/preorder_find_paths.cs +++ b/codes/csharp/chapter_backtracking/preorder_find_paths.cs @@ -37,7 +37,7 @@ public class preorder_find_paths [Test] public void Test() { - TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 }); + TreeNode root = TreeNode.ListToTree(new List { 1, 7, 3, 4, 5, 6, 7 }); Console.WriteLine("\n初始化二叉树"); PrintUtil.PrintTree(root); @@ -49,12 +49,7 @@ public class preorder_find_paths Console.WriteLine("\n输出所有根节点到节点 7 的路径"); foreach (List path in res) { - List vals = new List(); - foreach (TreeNode node in path) - { - vals.Add(node.val); - } - Console.WriteLine(string.Join(" ", vals)); + PrintUtil.PrintList(path.Select(p => p.val).ToList()); } } } \ No newline at end of file diff --git a/codes/csharp/chapter_computational_complexity/space_complexity.cs b/codes/csharp/chapter_computational_complexity/space_complexity.cs index 9fbd2aef..f24df022 100644 --- a/codes/csharp/chapter_computational_complexity/space_complexity.cs +++ b/codes/csharp/chapter_computational_complexity/space_complexity.cs @@ -50,7 +50,7 @@ public class space_complexity nodes.Add(new ListNode(i)); } // 长度为 n 的哈希表占用 O(n) 空间 - Dictionary map = new(); + Dictionary map = new(); for (int i = 0; i < n; i++) { map.Add(i, i.ToString()); diff --git a/codes/csharp/chapter_graph/graph_adjacency_list.cs b/codes/csharp/chapter_graph/graph_adjacency_list.cs index 5de7b3a3..bd36b37e 100644 --- a/codes/csharp/chapter_graph/graph_adjacency_list.cs +++ b/codes/csharp/chapter_graph/graph_adjacency_list.cs @@ -85,8 +85,8 @@ public class GraphAdjList { List tmp = new List(); foreach (Vertex vertex in entry.Value) - tmp.Add(vertex.Val); - Console.WriteLine(entry.Key.Val + ": [" + string.Join(", ", tmp) + "],"); + tmp.Add(vertex.val); + Console.WriteLine(entry.Key.val + ": [" + string.Join(", ", tmp) + "],"); } } } @@ -97,7 +97,7 @@ public class graph_adjacency_list public void Test() { /* 初始化无向图 */ - Vertex[] v = Vertex.valsToVets(new int[] { 1, 3, 2, 5, 4 }); + Vertex[] v = Vertex.ValsToVets(new int[] { 1, 3, 2, 5, 4 }); Vertex[][] edges = new Vertex[][] { new Vertex[] { v[0], v[1] }, new Vertex[] { v[0], v[3] }, new Vertex[] { v[1], v[2] }, new Vertex[] { v[2], v[3] }, new Vertex[] { v[2], v[4] }, new Vertex[] { v[3], v[4] } }; diff --git a/codes/csharp/chapter_graph/graph_adjacency_matrix.cs b/codes/csharp/chapter_graph/graph_adjacency_matrix.cs index cfbfa8bc..ee688765 100644 --- a/codes/csharp/chapter_graph/graph_adjacency_matrix.cs +++ b/codes/csharp/chapter_graph/graph_adjacency_matrix.cs @@ -104,7 +104,7 @@ class GraphAdjMat Console.Write("顶点列表 = "); PrintUtil.PrintList(vertices); Console.WriteLine("邻接矩阵 ="); - PrintUtil.printMatrix(adjMat); + PrintUtil.PrintMatrix(adjMat); } } diff --git a/codes/csharp/chapter_graph/graph_bfs.cs b/codes/csharp/chapter_graph/graph_bfs.cs index f2894ea9..daf2cb9e 100644 --- a/codes/csharp/chapter_graph/graph_bfs.cs +++ b/codes/csharp/chapter_graph/graph_bfs.cs @@ -46,7 +46,7 @@ public class graph_bfs public void Test() { /* 初始化无向图 */ - Vertex[] v = Vertex.valsToVets(new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); + Vertex[] v = Vertex.ValsToVets(new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); Vertex[][] edges = new Vertex[12][] { new Vertex[2] { v[0], v[1] }, new Vertex[2] { v[0], v[3] }, new Vertex[2] { v[1], v[2] }, @@ -62,6 +62,6 @@ public class graph_bfs /* 广度优先遍历 BFS */ List res = graphBFS(graph, v[0]); Console.WriteLine("\n广度优先遍历(BFS)顶点序列为"); - Console.WriteLine(string.Join(" ", Vertex.vetsToVals(res))); + Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res))); } } \ No newline at end of file diff --git a/codes/csharp/chapter_graph/graph_dfs.cs b/codes/csharp/chapter_graph/graph_dfs.cs index deb4e4f5..cf13d9a0 100644 --- a/codes/csharp/chapter_graph/graph_dfs.cs +++ b/codes/csharp/chapter_graph/graph_dfs.cs @@ -44,7 +44,7 @@ public class graph_dfs public void Test() { /* 初始化无向图 */ - Vertex[] v = Vertex.valsToVets(new int[7] { 0, 1, 2, 3, 4, 5, 6 }); + Vertex[] v = Vertex.ValsToVets(new int[7] { 0, 1, 2, 3, 4, 5, 6 }); Vertex[][] edges = new Vertex[6][] { new Vertex[2] { v[0], v[1] }, new Vertex[2] { v[0], v[3] }, new Vertex[2] { v[1], v[2] }, @@ -58,6 +58,6 @@ public class graph_dfs /* 深度优先遍历 DFS */ List res = graphDFS(graph, v[0]); Console.WriteLine("\n深度优先遍历(DFS)顶点序列为"); - Console.WriteLine(string.Join(" ", Vertex.vetsToVals(res))); + Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res))); } } \ No newline at end of file diff --git a/codes/csharp/chapter_hashing/array_hash_map.cs b/codes/csharp/chapter_hashing/array_hash_map.cs index d9ba14e9..b84be972 100644 --- a/codes/csharp/chapter_hashing/array_hash_map.cs +++ b/codes/csharp/chapter_hashing/array_hash_map.cs @@ -8,12 +8,12 @@ using NUnit.Framework; namespace hello_algo.chapter_hashing; -/* 键值对 int->String */ +/* 键值对 int->string */ class Entry { public int key; - public String val; - public Entry(int key, String val) + public string val; + public Entry(int key, string val) { this.key = key; this.val = val; @@ -42,7 +42,7 @@ class ArrayHashMap } /* 查询操作 */ - public String? get(int key) + public string? get(int key) { int index = hashFunc(key); Entry? pair = buckets[index]; @@ -51,7 +51,7 @@ class ArrayHashMap } /* 添加操作 */ - public void put(int key, String val) + public void put(int key, string val) { Entry pair = new Entry(key, val); int index = hashFunc(key); @@ -91,9 +91,9 @@ class ArrayHashMap } /* 获取所有值 */ - public List valueSet() + public List valueSet() { - List valueSet = new(); + List valueSet = new(); foreach (Entry? pair in buckets) { if (pair != null) @@ -133,7 +133,7 @@ public class array_hash_map /* 查询操作 */ // 向哈希表输入键 key ,得到值 value - String? name = map.get(15937); + string? name = map.get(15937); Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name); /* 删除操作 */ @@ -154,7 +154,7 @@ public class array_hash_map Console.WriteLine(key); } Console.WriteLine("\n单独遍历值 Value"); - foreach (String val in map.valueSet()) + foreach (string val in map.valueSet()) { Console.WriteLine(val); } diff --git a/codes/csharp/chapter_hashing/hash_map.cs b/codes/csharp/chapter_hashing/hash_map.cs index f733795d..d51f14ff 100644 --- a/codes/csharp/chapter_hashing/hash_map.cs +++ b/codes/csharp/chapter_hashing/hash_map.cs @@ -16,7 +16,7 @@ public class hash_map public void Test() { /* 初始化哈希表 */ - Dictionary map = new(); + Dictionary map = new(); /* 添加操作 */ // 在哈希表中添加键值对 (key, value) @@ -26,18 +26,18 @@ public class hash_map map.Add(13276, "小法"); map.Add(10583, "小鸭"); Console.WriteLine("\n添加完成后,哈希表为\nKey -> Value"); - PrintUtil.printHashMap(map); + PrintUtil.PrintHashMap(map); /* 查询操作 */ // 向哈希表输入键 key ,得到值 value - String name = map[15937]; + string name = map[15937]; Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name); /* 删除操作 */ // 在哈希表中删除键值对 (key, value) map.Remove(10583); Console.WriteLine("\n删除 10583 后,哈希表为\nKey -> Value"); - PrintUtil.printHashMap(map); + PrintUtil.PrintHashMap(map); /* 遍历哈希表 */ Console.WriteLine("\n遍历键值对 Key->Value"); @@ -51,7 +51,7 @@ public class hash_map Console.WriteLine(key); } Console.WriteLine("\n单独遍历值 Value"); - foreach (String val in map.Values) + foreach (string val in map.Values) { Console.WriteLine(val); } diff --git a/codes/csharp/chapter_heap/heap.cs b/codes/csharp/chapter_heap/heap.cs index f13889be..2b714711 100644 --- a/codes/csharp/chapter_heap/heap.cs +++ b/codes/csharp/chapter_heap/heap.cs @@ -15,14 +15,14 @@ public class heap { heap.Enqueue(val, val); // 元素入堆 Console.WriteLine($"\n元素 {val} 入堆后\n"); - PrintUtil.printHeap(heap); + PrintUtil.PrintHeap(heap); } public void testPop(PriorityQueue heap) { int val = heap.Dequeue(); // 堆顶元素出堆 Console.WriteLine($"\n堆顶元素 {val} 出堆后\n"); - PrintUtil.printHeap(heap); + PrintUtil.PrintHeap(heap); } [Test] public void Test() @@ -65,6 +65,6 @@ public class heap var list = new int[] { 1, 3, 2, 5, 4 }; minHeap = new PriorityQueue(list.Select(x => (x, x))); Console.WriteLine("输入列表并建立小顶堆后"); - PrintUtil.printHeap(minHeap); + PrintUtil.PrintHeap(minHeap); } } diff --git a/codes/csharp/chapter_heap/my_heap.cs b/codes/csharp/chapter_heap/my_heap.cs index 3fb7c4fe..363ee601 100644 --- a/codes/csharp/chapter_heap/my_heap.cs +++ b/codes/csharp/chapter_heap/my_heap.cs @@ -143,7 +143,7 @@ class MaxHeap public void print() { var queue = new Queue(maxHeap); - PrintUtil.printHeap(queue); + PrintUtil.PrintHeap(queue); } } diff --git a/codes/csharp/chapter_stack_and_queue/array_stack.cs b/codes/csharp/chapter_stack_and_queue/array_stack.cs index 07711be4..3b556d5b 100644 --- a/codes/csharp/chapter_stack_and_queue/array_stack.cs +++ b/codes/csharp/chapter_stack_and_queue/array_stack.cs @@ -75,7 +75,7 @@ public class array_stack stack.push(2); stack.push(5); stack.push(4); - Console.WriteLine("栈 stack = " + String.Join(",", stack.toArray())); + Console.WriteLine("栈 stack = " + string.Join(",", stack.toArray())); /* 访问栈顶元素 */ int peek = stack.peek(); @@ -83,7 +83,7 @@ public class array_stack /* 元素出栈 */ int pop = stack.pop(); - Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + String.Join(",", stack.toArray())); + Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.toArray())); /* 获取栈的长度 */ int size = stack.size(); diff --git a/codes/csharp/chapter_stack_and_queue/deque.cs b/codes/csharp/chapter_stack_and_queue/deque.cs index 47700f7c..41933d69 100644 --- a/codes/csharp/chapter_stack_and_queue/deque.cs +++ b/codes/csharp/chapter_stack_and_queue/deque.cs @@ -23,7 +23,7 @@ public class deque deque.AddLast(4); deque.AddFirst(3); // 添加至队首 deque.AddFirst(1); - Console.WriteLine("双向队列 deque = " + String.Join(",", deque.ToArray())); + Console.WriteLine("双向队列 deque = " + string.Join(",", deque)); /* 访问元素 */ int peekFirst = deque.First.Value; // 队首元素 @@ -33,9 +33,9 @@ public class deque /* 元素出队 */ deque.RemoveFirst(); // 队首元素出队 - Console.WriteLine("队首元素出队后 deque = " + String.Join(",", deque.ToArray())); + Console.WriteLine("队首元素出队后 deque = " + string.Join(",", deque)); deque.RemoveLast(); // 队尾元素出队 - Console.WriteLine("队尾元素出队后 deque = " + String.Join(",", deque.ToArray())); + Console.WriteLine("队尾元素出队后 deque = " + string.Join(",", deque)); /* 获取双向队列的长度 */ int size = deque.Count; diff --git a/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs b/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs index 088dd2ba..d5fd8af2 100644 --- a/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs +++ b/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs @@ -102,7 +102,7 @@ public class linkedlist_queue queue.push(2); queue.push(5); queue.push(4); - Console.WriteLine("队列 queue = " + String.Join(",", queue.toArray())); + Console.WriteLine("队列 queue = " + string.Join(",", queue.toArray())); /* 访问队首元素 */ int peek = queue.peek(); @@ -110,7 +110,7 @@ public class linkedlist_queue /* 元素出队 */ int pop = queue.pop(); - Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + String.Join(",", queue.toArray())); + Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue.toArray())); /* 获取队列的长度 */ int size = queue.size(); diff --git a/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs b/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs index dc697ed1..edd8f46f 100644 --- a/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs +++ b/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs @@ -92,7 +92,7 @@ public class linkedlist_stack stack.push(2); stack.push(5); stack.push(4); - Console.WriteLine("栈 stack = " + String.Join(",", stack.toArray())); + Console.WriteLine("栈 stack = " + string.Join(",", stack.toArray())); /* 访问栈顶元素 */ int peek = stack.peek(); @@ -100,7 +100,7 @@ public class linkedlist_stack /* 元素出栈 */ int pop = stack.pop(); - Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + String.Join(",", stack.toArray())); + Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.toArray())); /* 获取栈的长度 */ int size = stack.size(); diff --git a/codes/csharp/chapter_stack_and_queue/queue.cs b/codes/csharp/chapter_stack_and_queue/queue.cs index c4fa36dc..a3a55944 100644 --- a/codes/csharp/chapter_stack_and_queue/queue.cs +++ b/codes/csharp/chapter_stack_and_queue/queue.cs @@ -22,7 +22,7 @@ public class queue queue.Enqueue(2); queue.Enqueue(5); queue.Enqueue(4); - Console.WriteLine("队列 queue = " + String.Join(",", queue.ToArray())); + Console.WriteLine("队列 queue = " + string.Join(",", queue)); /* 访问队首元素 */ int peek = queue.Peek(); @@ -30,7 +30,7 @@ public class queue /* 元素出队 */ int pop = queue.Dequeue(); - Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + String.Join(",", queue.ToArray())); + Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue)); /* 获取队列的长度 */ int size = queue.Count(); diff --git a/codes/csharp/chapter_stack_and_queue/stack.cs b/codes/csharp/chapter_stack_and_queue/stack.cs index 56fc47f4..e917dafa 100644 --- a/codes/csharp/chapter_stack_and_queue/stack.cs +++ b/codes/csharp/chapter_stack_and_queue/stack.cs @@ -23,7 +23,7 @@ public class stack stack.Push(5); stack.Push(4); // 请注意,stack.ToArray() 得到的是倒序序列,即索引 0 为栈顶 - Console.WriteLine("栈 stack = " + string.Join(",", stack.ToArray())); + Console.WriteLine("栈 stack = " + string.Join(",", stack)); /* 访问栈顶元素 */ int peek = stack.Peek(); @@ -31,7 +31,7 @@ public class stack /* 元素出栈 */ int pop = stack.Pop(); - Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.ToArray())); + Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack)); /* 获取栈的长度 */ int size = stack.Count(); diff --git a/codes/csharp/chapter_tree/binary_tree_bfs.cs b/codes/csharp/chapter_tree/binary_tree_bfs.cs index a98e6e77..6ee2fa33 100644 --- a/codes/csharp/chapter_tree/binary_tree_bfs.cs +++ b/codes/csharp/chapter_tree/binary_tree_bfs.cs @@ -37,11 +37,11 @@ public class binary_tree_bfs { /* 初始化二叉树 */ // 这里借助了一个从数组直接生成二叉树的函数 - TreeNode? root = TreeNode.ArrToTree(new int?[] { 1, 2, 3, 4, 5, 6, 7 }); + TreeNode? root = TreeNode.ListToTree(new List { 1, 2, 3, 4, 5, 6, 7 }); Console.WriteLine("\n初始化二叉树\n"); PrintUtil.PrintTree(root); List list = levelOrder(root); - Console.WriteLine("\n层序遍历的节点打印序列 = " + string.Join(",", list.ToArray())); + Console.WriteLine("\n层序遍历的节点打印序列 = " + string.Join(",", list)); } } diff --git a/codes/csharp/chapter_tree/binary_tree_dfs.cs b/codes/csharp/chapter_tree/binary_tree_dfs.cs index 27a847b7..b82bb86f 100644 --- a/codes/csharp/chapter_tree/binary_tree_dfs.cs +++ b/codes/csharp/chapter_tree/binary_tree_dfs.cs @@ -48,20 +48,20 @@ public class binary_tree_dfs { /* 初始化二叉树 */ // 这里借助了一个从数组直接生成二叉树的函数 - TreeNode? root = TreeNode.ArrToTree(new int?[] { 1, 2, 3, 4, 5, 6, 7 }); + TreeNode? root = TreeNode.ListToTree(new List { 1, 2, 3, 4, 5, 6, 7 }); Console.WriteLine("\n初始化二叉树\n"); PrintUtil.PrintTree(root); list.Clear(); preOrder(root); - Console.WriteLine("\n前序遍历的节点打印序列 = " + string.Join(",", list.ToArray())); + Console.WriteLine("\n前序遍历的节点打印序列 = " + string.Join(",", list)); list.Clear(); inOrder(root); - Console.WriteLine("\n中序遍历的节点打印序列 = " + string.Join(",", list.ToArray())); + Console.WriteLine("\n中序遍历的节点打印序列 = " + string.Join(",", list)); list.Clear(); postOrder(root); - Console.WriteLine("\n后序遍历的节点打印序列 = " + string.Join(",", list.ToArray())); + Console.WriteLine("\n后序遍历的节点打印序列 = " + string.Join(",", list)); } } diff --git a/codes/csharp/include/PrintUtil.cs b/codes/csharp/include/PrintUtil.cs index f940206f..b258c0a3 100644 --- a/codes/csharp/include/PrintUtil.cs +++ b/codes/csharp/include/PrintUtil.cs @@ -9,9 +9,9 @@ namespace hello_algo.include; public class Trunk { public Trunk? prev; - public String str; + public string str; - public Trunk(Trunk? prev, String str) + public Trunk(Trunk? prev, string str) { this.prev = prev; this.str = str; @@ -24,24 +24,46 @@ public class PrintUtil * Print a list * @param list */ - public static void PrintList(List list) + public static void PrintList(List list) { Console.WriteLine("[" + string.Join(", ", list) + "]"); } + /* Print a matrix (Array) */ + public static void PrintMatrix(T[][] matrix) + { + Console.WriteLine("["); + foreach (T[] row in matrix) + { + Console.WriteLine(" " + string.Join(", ", row) + ","); + } + Console.WriteLine("]"); + } + + /* Print a matrix (List) */ + public static void PrintMatrix(List> matrix) + { + Console.WriteLine("["); + foreach (List row in matrix) + { + Console.WriteLine(" " + string.Join(", ", row) + ","); + } + Console.WriteLine("]"); + } + /** * Print a linked list * @param head */ public static void PrintLinkedList(ListNode head) { - List list = new(); + List list = new(); while (head != null) { list.Add(head.val.ToString()); head = head.next; } - Console.Write(String.Join(" -> ", list)); + Console.Write(string.Join(" -> ", list)); } /** @@ -68,7 +90,7 @@ public class PrintUtil return; } - String prev_str = " "; + string prev_str = " "; Trunk trunk = new Trunk(prev, prev_str); PrintTree(root.right, trunk, true); @@ -88,7 +110,7 @@ public class PrintUtil prev.str = prev_str; } - showTrunks(trunk); + ShowTrunks(trunk); Console.WriteLine(" " + root.val); if (prev != null) @@ -104,14 +126,14 @@ public class PrintUtil * Helper function to print branches of the binary tree * @param p */ - public static void showTrunks(Trunk? p) + public static void ShowTrunks(Trunk? p) { if (p == null) { return; } - showTrunks(p.prev); + ShowTrunks(p.prev); Console.Write(p.str); } @@ -121,7 +143,7 @@ public class PrintUtil * @param * @param map */ - public static void printHashMap(Dictionary map) where K : notnull + public static void PrintHashMap(Dictionary map) where K : notnull { foreach (var kv in map.Keys) { @@ -129,17 +151,17 @@ public class PrintUtil } } - public static void printHeap(Queue queue) + public static void PrintHeap(Queue queue) { Console.Write("堆的数组表示:"); List list = queue.ToList(); Console.WriteLine(string.Join(',', list)); Console.WriteLine("堆的树状表示:"); - TreeNode tree = TreeNode.ArrToTree(list.Cast().ToArray()); + TreeNode tree = TreeNode.ListToTree(list.Cast().ToList()); PrintTree(tree); } - public static void printHeap(PriorityQueue queue) + public static void PrintHeap(PriorityQueue queue) { var newQueue = new PriorityQueue(queue.UnorderedItems, queue.Comparer); Console.Write("堆的数组表示:"); @@ -150,17 +172,7 @@ public class PrintUtil } Console.WriteLine("堆的树状表示:"); Console.WriteLine(string.Join(',', list.ToList())); - TreeNode tree = TreeNode.ArrToTree(list.Cast().ToArray()); + TreeNode tree = TreeNode.ListToTree(list.Cast().ToList()); PrintTree(tree); } - - public static void printMatrix(List> matrix) - { - Console.WriteLine("["); - foreach (List row in matrix) - { - Console.WriteLine(" [" + string.Join(", ", row.Select(r => $"{r}")) + "],"); - } - Console.WriteLine("]"); - } } diff --git a/codes/csharp/include/TreeNode.cs b/codes/csharp/include/TreeNode.cs index 330c7711..d76aef27 100644 --- a/codes/csharp/include/TreeNode.cs +++ b/codes/csharp/include/TreeNode.cs @@ -23,25 +23,25 @@ public class TreeNode * @param arr * @return */ - public static TreeNode? ArrToTree(int?[] arr) + public static TreeNode? ListToTree(List arr) { - if (arr.Length == 0 || arr[0] == null) + if (arr.Count == 0 || arr[0] == null) return null; - TreeNode root = new TreeNode((int)arr[0]); + TreeNode root = new TreeNode(arr[0]!.Value); Queue queue = new Queue(); queue.Enqueue(root); int i = 0; while (queue.Count != 0) { TreeNode node = queue.Dequeue(); - if (++i >= arr.Length) break; + if (++i >= arr.Count) break; if (arr[i] != null) { node.left = new TreeNode((int)arr[i]); queue.Enqueue(node.left); } - if (++i >= arr.Length) break; + if (++i >= arr.Count) break; if (arr[i] != null) { node.right = new TreeNode((int)arr[i]); @@ -92,6 +92,6 @@ public class TreeNode return root; TreeNode? left = GetTreeNode(root.left, val); TreeNode? right = GetTreeNode(root.right, val); - return left != null ? left : right; + return left ?? right; } } diff --git a/codes/csharp/include/Vertex.cs b/codes/csharp/include/Vertex.cs index ac64d1b6..4d74a930 100644 --- a/codes/csharp/include/Vertex.cs +++ b/codes/csharp/include/Vertex.cs @@ -9,14 +9,14 @@ namespace hello_algo.include; /* 顶点类 */ public class Vertex { - public int Val { get; init; } + public int val; public Vertex(int val) { - Val = val; + this.val = val; } /* 输入值列表 vals ,返回顶点列表 vets */ - public static Vertex[] valsToVets(int[] vals) + public static Vertex[] ValsToVets(int[] vals) { Vertex[] vets = new Vertex[vals.Length]; for (int i = 0; i < vals.Length; i++) @@ -27,12 +27,12 @@ public class Vertex } /* 输入顶点列表 vets ,返回值列表 vals */ - public static List vetsToVals(List vets) + public static List VetsToVals(List vets) { List vals = new List(); foreach (Vertex vet in vets) { - vals.Add(vet.Val); + vals.Add(vet.val); } return vals; }