refactor: add/refactor method in include, simplified print code (#471)

This commit is contained in:
hpstory 2023-04-21 14:59:22 +08:00 committed by GitHub
parent 9c2e5e2831
commit 9eeefff447
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 102 additions and 109 deletions

View File

@ -70,7 +70,7 @@ public class backtrack_find_constrained_paths
[Test] [Test]
public void Test() public void Test()
{ {
TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 }); TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树"); Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
@ -82,12 +82,7 @@ public class backtrack_find_constrained_paths
Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点"); Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
foreach (List<TreeNode> path in res) foreach (List<TreeNode> path in res)
{ {
List<int> vals = new List<int>(); PrintUtil.PrintList(path.Select(p => p.val).ToList());
foreach (TreeNode node in path)
{
vals.Add(node.val);
}
Console.WriteLine(string.Join(" ", vals));
} }
} }
} }

View File

@ -38,7 +38,7 @@ public class preorder_find_constrained_paths
[Test] [Test]
public void Test() public void Test()
{ {
TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 }); TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树"); Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
@ -50,12 +50,7 @@ public class preorder_find_constrained_paths
Console.WriteLine("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点"); Console.WriteLine("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点");
foreach (List<TreeNode> path in res) foreach (List<TreeNode> path in res)
{ {
List<int> vals = new List<int>(); PrintUtil.PrintList(path.Select(p => p.val).ToList());
foreach (TreeNode node in path)
{
vals.Add(node.val);
}
Console.WriteLine(string.Join(" ", vals));
} }
} }
} }

View File

@ -6,6 +6,7 @@
using hello_algo.include; using hello_algo.include;
using NUnit.Framework; using NUnit.Framework;
using System.IO;
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
@ -32,7 +33,7 @@ public class preorder_find_nodes
[Test] [Test]
public void Test() public void Test()
{ {
TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 }); TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树"); Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
@ -41,11 +42,6 @@ public class preorder_find_nodes
preOrder(root); preOrder(root);
Console.WriteLine("\n输出所有值为 7 的节点"); Console.WriteLine("\n输出所有值为 7 的节点");
List<int> vals = new List<int>(); PrintUtil.PrintList(res.Select(p => p.val).ToList());
foreach (TreeNode node in res)
{
vals.Add(node.val);
}
Console.WriteLine(string.Join(" ", vals));
} }
} }

View File

@ -37,7 +37,7 @@ public class preorder_find_paths
[Test] [Test]
public void Test() public void Test()
{ {
TreeNode root = TreeNode.ArrToTree(new int?[] { 1, 7, 3, 4, 5, 6, 7 }); TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树"); Console.WriteLine("\n初始化二叉树");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
@ -49,12 +49,7 @@ public class preorder_find_paths
Console.WriteLine("\n输出所有根节点到节点 7 的路径"); Console.WriteLine("\n输出所有根节点到节点 7 的路径");
foreach (List<TreeNode> path in res) foreach (List<TreeNode> path in res)
{ {
List<int> vals = new List<int>(); PrintUtil.PrintList(path.Select(p => p.val).ToList());
foreach (TreeNode node in path)
{
vals.Add(node.val);
}
Console.WriteLine(string.Join(" ", vals));
} }
} }
} }

View File

@ -50,7 +50,7 @@ public class space_complexity
nodes.Add(new ListNode(i)); nodes.Add(new ListNode(i));
} }
// 长度为 n 的哈希表占用 O(n) 空间 // 长度为 n 的哈希表占用 O(n) 空间
Dictionary<int, String> map = new(); Dictionary<int, string> map = new();
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
{ {
map.Add(i, i.ToString()); map.Add(i, i.ToString());

View File

@ -85,8 +85,8 @@ public class GraphAdjList
{ {
List<int> tmp = new List<int>(); List<int> tmp = new List<int>();
foreach (Vertex vertex in entry.Value) foreach (Vertex vertex in entry.Value)
tmp.Add(vertex.Val); tmp.Add(vertex.val);
Console.WriteLine(entry.Key.Val + ": [" + string.Join(", ", tmp) + "],"); Console.WriteLine(entry.Key.val + ": [" + string.Join(", ", tmp) + "],");
} }
} }
} }
@ -97,7 +97,7 @@ public class graph_adjacency_list
public void Test() 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] }, 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[1], v[2] }, new Vertex[] { v[2], v[3] },
new Vertex[] { v[2], v[4] }, new Vertex[] { v[3], v[4] } }; new Vertex[] { v[2], v[4] }, new Vertex[] { v[3], v[4] } };

View File

@ -104,7 +104,7 @@ class GraphAdjMat
Console.Write("顶点列表 = "); Console.Write("顶点列表 = ");
PrintUtil.PrintList(vertices); PrintUtil.PrintList(vertices);
Console.WriteLine("邻接矩阵 ="); Console.WriteLine("邻接矩阵 =");
PrintUtil.printMatrix(adjMat); PrintUtil.PrintMatrix(adjMat);
} }
} }

View File

@ -46,7 +46,7 @@ public class graph_bfs
public void Test() 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][] 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] }, 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 */ /* 广度优先遍历 BFS */
List<Vertex> res = graphBFS(graph, v[0]); List<Vertex> res = graphBFS(graph, v[0]);
Console.WriteLine("\n广度优先遍历BFS顶点序列为"); Console.WriteLine("\n广度优先遍历BFS顶点序列为");
Console.WriteLine(string.Join(" ", Vertex.vetsToVals(res))); Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
} }
} }

View File

@ -44,7 +44,7 @@ public class graph_dfs
public void Test() 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][] 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] }, 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 */ /* 深度优先遍历 DFS */
List<Vertex> res = graphDFS(graph, v[0]); List<Vertex> res = graphDFS(graph, v[0]);
Console.WriteLine("\n深度优先遍历DFS顶点序列为"); Console.WriteLine("\n深度优先遍历DFS顶点序列为");
Console.WriteLine(string.Join(" ", Vertex.vetsToVals(res))); Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
} }
} }

View File

@ -8,12 +8,12 @@ using NUnit.Framework;
namespace hello_algo.chapter_hashing; namespace hello_algo.chapter_hashing;
/* 键值对 int->String */ /* 键值对 int->string */
class Entry class Entry
{ {
public int key; public int key;
public String val; public string val;
public Entry(int key, String val) public Entry(int key, string val)
{ {
this.key = key; this.key = key;
this.val = val; this.val = val;
@ -42,7 +42,7 @@ class ArrayHashMap
} }
/* 查询操作 */ /* 查询操作 */
public String? get(int key) public string? get(int key)
{ {
int index = hashFunc(key); int index = hashFunc(key);
Entry? pair = buckets[index]; 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); Entry pair = new Entry(key, val);
int index = hashFunc(key); int index = hashFunc(key);
@ -91,9 +91,9 @@ class ArrayHashMap
} }
/* 获取所有值 */ /* 获取所有值 */
public List<String> valueSet() public List<string> valueSet()
{ {
List<String> valueSet = new(); List<string> valueSet = new();
foreach (Entry? pair in buckets) foreach (Entry? pair in buckets)
{ {
if (pair != null) if (pair != null)
@ -133,7 +133,7 @@ public class array_hash_map
/* 查询操作 */ /* 查询操作 */
// 向哈希表输入键 key ,得到值 value // 向哈希表输入键 key ,得到值 value
String? name = map.get(15937); string? name = map.get(15937);
Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name); Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name);
/* 删除操作 */ /* 删除操作 */
@ -154,7 +154,7 @@ public class array_hash_map
Console.WriteLine(key); Console.WriteLine(key);
} }
Console.WriteLine("\n单独遍历值 Value"); Console.WriteLine("\n单独遍历值 Value");
foreach (String val in map.valueSet()) foreach (string val in map.valueSet())
{ {
Console.WriteLine(val); Console.WriteLine(val);
} }

View File

@ -16,7 +16,7 @@ public class hash_map
public void Test() public void Test()
{ {
/* 初始化哈希表 */ /* 初始化哈希表 */
Dictionary<int, String> map = new(); Dictionary<int, string> map = new();
/* 添加操作 */ /* 添加操作 */
// 在哈希表中添加键值对 (key, value) // 在哈希表中添加键值对 (key, value)
@ -26,18 +26,18 @@ public class hash_map
map.Add(13276, "小法"); map.Add(13276, "小法");
map.Add(10583, "小鸭"); map.Add(10583, "小鸭");
Console.WriteLine("\n添加完成后哈希表为\nKey -> Value"); Console.WriteLine("\n添加完成后哈希表为\nKey -> Value");
PrintUtil.printHashMap(map); PrintUtil.PrintHashMap(map);
/* 查询操作 */ /* 查询操作 */
// 向哈希表输入键 key ,得到值 value // 向哈希表输入键 key ,得到值 value
String name = map[15937]; string name = map[15937];
Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name); Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name);
/* 删除操作 */ /* 删除操作 */
// 在哈希表中删除键值对 (key, value) // 在哈希表中删除键值对 (key, value)
map.Remove(10583); map.Remove(10583);
Console.WriteLine("\n删除 10583 后,哈希表为\nKey -> Value"); Console.WriteLine("\n删除 10583 后,哈希表为\nKey -> Value");
PrintUtil.printHashMap(map); PrintUtil.PrintHashMap(map);
/* 遍历哈希表 */ /* 遍历哈希表 */
Console.WriteLine("\n遍历键值对 Key->Value"); Console.WriteLine("\n遍历键值对 Key->Value");
@ -51,7 +51,7 @@ public class hash_map
Console.WriteLine(key); Console.WriteLine(key);
} }
Console.WriteLine("\n单独遍历值 Value"); Console.WriteLine("\n单独遍历值 Value");
foreach (String val in map.Values) foreach (string val in map.Values)
{ {
Console.WriteLine(val); Console.WriteLine(val);
} }

View File

@ -15,14 +15,14 @@ public class heap
{ {
heap.Enqueue(val, val); // 元素入堆 heap.Enqueue(val, val); // 元素入堆
Console.WriteLine($"\n元素 {val} 入堆后\n"); Console.WriteLine($"\n元素 {val} 入堆后\n");
PrintUtil.printHeap(heap); PrintUtil.PrintHeap(heap);
} }
public void testPop(PriorityQueue<int, int> heap) public void testPop(PriorityQueue<int, int> heap)
{ {
int val = heap.Dequeue(); // 堆顶元素出堆 int val = heap.Dequeue(); // 堆顶元素出堆
Console.WriteLine($"\n堆顶元素 {val} 出堆后\n"); Console.WriteLine($"\n堆顶元素 {val} 出堆后\n");
PrintUtil.printHeap(heap); PrintUtil.PrintHeap(heap);
} }
[Test] [Test]
public void Test() public void Test()
@ -65,6 +65,6 @@ public class heap
var list = new int[] { 1, 3, 2, 5, 4 }; var list = new int[] { 1, 3, 2, 5, 4 };
minHeap = new PriorityQueue<int, int>(list.Select(x => (x, x))); minHeap = new PriorityQueue<int, int>(list.Select(x => (x, x)));
Console.WriteLine("输入列表并建立小顶堆后"); Console.WriteLine("输入列表并建立小顶堆后");
PrintUtil.printHeap(minHeap); PrintUtil.PrintHeap(minHeap);
} }
} }

View File

@ -143,7 +143,7 @@ class MaxHeap
public void print() public void print()
{ {
var queue = new Queue<int>(maxHeap); var queue = new Queue<int>(maxHeap);
PrintUtil.printHeap(queue); PrintUtil.PrintHeap(queue);
} }
} }

View File

@ -75,7 +75,7 @@ public class array_stack
stack.push(2); stack.push(2);
stack.push(5); stack.push(5);
stack.push(4); stack.push(4);
Console.WriteLine("栈 stack = " + String.Join(",", stack.toArray())); Console.WriteLine("栈 stack = " + string.Join(",", stack.toArray()));
/* 访问栈顶元素 */ /* 访问栈顶元素 */
int peek = stack.peek(); int peek = stack.peek();
@ -83,7 +83,7 @@ public class array_stack
/* 元素出栈 */ /* 元素出栈 */
int pop = stack.pop(); 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(); int size = stack.size();

View File

@ -23,7 +23,7 @@ public class deque
deque.AddLast(4); deque.AddLast(4);
deque.AddFirst(3); // 添加至队首 deque.AddFirst(3); // 添加至队首
deque.AddFirst(1); deque.AddFirst(1);
Console.WriteLine("双向队列 deque = " + String.Join(",", deque.ToArray())); Console.WriteLine("双向队列 deque = " + string.Join(",", deque));
/* 访问元素 */ /* 访问元素 */
int peekFirst = deque.First.Value; // 队首元素 int peekFirst = deque.First.Value; // 队首元素
@ -33,9 +33,9 @@ public class deque
/* 元素出队 */ /* 元素出队 */
deque.RemoveFirst(); // 队首元素出队 deque.RemoveFirst(); // 队首元素出队
Console.WriteLine("队首元素出队后 deque = " + String.Join(",", deque.ToArray())); Console.WriteLine("队首元素出队后 deque = " + string.Join(",", deque));
deque.RemoveLast(); // 队尾元素出队 deque.RemoveLast(); // 队尾元素出队
Console.WriteLine("队尾元素出队后 deque = " + String.Join(",", deque.ToArray())); Console.WriteLine("队尾元素出队后 deque = " + string.Join(",", deque));
/* 获取双向队列的长度 */ /* 获取双向队列的长度 */
int size = deque.Count; int size = deque.Count;

View File

@ -102,7 +102,7 @@ public class linkedlist_queue
queue.push(2); queue.push(2);
queue.push(5); queue.push(5);
queue.push(4); queue.push(4);
Console.WriteLine("队列 queue = " + String.Join(",", queue.toArray())); Console.WriteLine("队列 queue = " + string.Join(",", queue.toArray()));
/* 访问队首元素 */ /* 访问队首元素 */
int peek = queue.peek(); int peek = queue.peek();
@ -110,7 +110,7 @@ public class linkedlist_queue
/* 元素出队 */ /* 元素出队 */
int pop = queue.pop(); 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(); int size = queue.size();

View File

@ -92,7 +92,7 @@ public class linkedlist_stack
stack.push(2); stack.push(2);
stack.push(5); stack.push(5);
stack.push(4); stack.push(4);
Console.WriteLine("栈 stack = " + String.Join(",", stack.toArray())); Console.WriteLine("栈 stack = " + string.Join(",", stack.toArray()));
/* 访问栈顶元素 */ /* 访问栈顶元素 */
int peek = stack.peek(); int peek = stack.peek();
@ -100,7 +100,7 @@ public class linkedlist_stack
/* 元素出栈 */ /* 元素出栈 */
int pop = stack.pop(); 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(); int size = stack.size();

View File

@ -22,7 +22,7 @@ public class queue
queue.Enqueue(2); queue.Enqueue(2);
queue.Enqueue(5); queue.Enqueue(5);
queue.Enqueue(4); queue.Enqueue(4);
Console.WriteLine("队列 queue = " + String.Join(",", queue.ToArray())); Console.WriteLine("队列 queue = " + string.Join(",", queue));
/* 访问队首元素 */ /* 访问队首元素 */
int peek = queue.Peek(); int peek = queue.Peek();
@ -30,7 +30,7 @@ public class queue
/* 元素出队 */ /* 元素出队 */
int pop = queue.Dequeue(); 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(); int size = queue.Count();

View File

@ -23,7 +23,7 @@ public class stack
stack.Push(5); stack.Push(5);
stack.Push(4); stack.Push(4);
// 请注意stack.ToArray() 得到的是倒序序列,即索引 0 为栈顶 // 请注意stack.ToArray() 得到的是倒序序列,即索引 0 为栈顶
Console.WriteLine("栈 stack = " + string.Join(",", stack.ToArray())); Console.WriteLine("栈 stack = " + string.Join(",", stack));
/* 访问栈顶元素 */ /* 访问栈顶元素 */
int peek = stack.Peek(); int peek = stack.Peek();
@ -31,7 +31,7 @@ public class stack
/* 元素出栈 */ /* 元素出栈 */
int pop = stack.Pop(); 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(); int size = stack.Count();

View File

@ -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<int?> { 1, 2, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树\n"); Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
List<int> list = levelOrder(root); List<int> list = levelOrder(root);
Console.WriteLine("\n层序遍历的节点打印序列 = " + string.Join(",", list.ToArray())); Console.WriteLine("\n层序遍历的节点打印序列 = " + string.Join(",", list));
} }
} }

View File

@ -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<int?> { 1, 2, 3, 4, 5, 6, 7 });
Console.WriteLine("\n初始化二叉树\n"); Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(root); PrintUtil.PrintTree(root);
list.Clear(); list.Clear();
preOrder(root); preOrder(root);
Console.WriteLine("\n前序遍历的节点打印序列 = " + string.Join(",", list.ToArray())); Console.WriteLine("\n前序遍历的节点打印序列 = " + string.Join(",", list));
list.Clear(); list.Clear();
inOrder(root); inOrder(root);
Console.WriteLine("\n中序遍历的节点打印序列 = " + string.Join(",", list.ToArray())); Console.WriteLine("\n中序遍历的节点打印序列 = " + string.Join(",", list));
list.Clear(); list.Clear();
postOrder(root); postOrder(root);
Console.WriteLine("\n后序遍历的节点打印序列 = " + string.Join(",", list.ToArray())); Console.WriteLine("\n后序遍历的节点打印序列 = " + string.Join(",", list));
} }
} }

View File

@ -9,9 +9,9 @@ namespace hello_algo.include;
public class Trunk public class Trunk
{ {
public Trunk? prev; 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.prev = prev;
this.str = str; this.str = str;
@ -24,24 +24,46 @@ public class PrintUtil
* Print a list * Print a list
* @param list * @param list
*/ */
public static void PrintList(List<int> list) public static void PrintList<T>(List<T> list)
{ {
Console.WriteLine("[" + string.Join(", ", list) + "]"); Console.WriteLine("[" + string.Join(", ", list) + "]");
} }
/* Print a matrix (Array) */
public static void PrintMatrix<T>(T[][] matrix)
{
Console.WriteLine("[");
foreach (T[] row in matrix)
{
Console.WriteLine(" " + string.Join(", ", row) + ",");
}
Console.WriteLine("]");
}
/* Print a matrix (List) */
public static void PrintMatrix<T>(List<List<T>> matrix)
{
Console.WriteLine("[");
foreach (List<T> row in matrix)
{
Console.WriteLine(" " + string.Join(", ", row) + ",");
}
Console.WriteLine("]");
}
/** /**
* Print a linked list * Print a linked list
* @param head * @param head
*/ */
public static void PrintLinkedList(ListNode head) public static void PrintLinkedList(ListNode head)
{ {
List<String> list = new(); List<string> list = new();
while (head != null) while (head != null)
{ {
list.Add(head.val.ToString()); list.Add(head.val.ToString());
head = head.next; head = head.next;
} }
Console.Write(String.Join(" -> ", list)); Console.Write(string.Join(" -> ", list));
} }
/** /**
@ -68,7 +90,7 @@ public class PrintUtil
return; return;
} }
String prev_str = " "; string prev_str = " ";
Trunk trunk = new Trunk(prev, prev_str); Trunk trunk = new Trunk(prev, prev_str);
PrintTree(root.right, trunk, true); PrintTree(root.right, trunk, true);
@ -88,7 +110,7 @@ public class PrintUtil
prev.str = prev_str; prev.str = prev_str;
} }
showTrunks(trunk); ShowTrunks(trunk);
Console.WriteLine(" " + root.val); Console.WriteLine(" " + root.val);
if (prev != null) if (prev != null)
@ -104,14 +126,14 @@ public class PrintUtil
* Helper function to print branches of the binary tree * Helper function to print branches of the binary tree
* @param p * @param p
*/ */
public static void showTrunks(Trunk? p) public static void ShowTrunks(Trunk? p)
{ {
if (p == null) if (p == null)
{ {
return; return;
} }
showTrunks(p.prev); ShowTrunks(p.prev);
Console.Write(p.str); Console.Write(p.str);
} }
@ -121,7 +143,7 @@ public class PrintUtil
* @param <V> * @param <V>
* @param map * @param map
*/ */
public static void printHashMap<K, V>(Dictionary<K, V> map) where K : notnull public static void PrintHashMap<K, V>(Dictionary<K, V> map) where K : notnull
{ {
foreach (var kv in map.Keys) foreach (var kv in map.Keys)
{ {
@ -129,17 +151,17 @@ public class PrintUtil
} }
} }
public static void printHeap(Queue<int> queue) public static void PrintHeap(Queue<int> queue)
{ {
Console.Write("堆的数组表示:"); Console.Write("堆的数组表示:");
List<int> list = queue.ToList(); List<int> list = queue.ToList();
Console.WriteLine(string.Join(',', list)); Console.WriteLine(string.Join(',', list));
Console.WriteLine("堆的树状表示:"); Console.WriteLine("堆的树状表示:");
TreeNode tree = TreeNode.ArrToTree(list.Cast<int?>().ToArray()); TreeNode tree = TreeNode.ListToTree(list.Cast<int?>().ToList());
PrintTree(tree); PrintTree(tree);
} }
public static void printHeap(PriorityQueue<int, int> queue) public static void PrintHeap(PriorityQueue<int, int> queue)
{ {
var newQueue = new PriorityQueue<int, int>(queue.UnorderedItems, queue.Comparer); var newQueue = new PriorityQueue<int, int>(queue.UnorderedItems, queue.Comparer);
Console.Write("堆的数组表示:"); Console.Write("堆的数组表示:");
@ -150,17 +172,7 @@ public class PrintUtil
} }
Console.WriteLine("堆的树状表示:"); Console.WriteLine("堆的树状表示:");
Console.WriteLine(string.Join(',', list.ToList())); Console.WriteLine(string.Join(',', list.ToList()));
TreeNode tree = TreeNode.ArrToTree(list.Cast<int?>().ToArray()); TreeNode tree = TreeNode.ListToTree(list.Cast<int?>().ToList());
PrintTree(tree); PrintTree(tree);
} }
public static void printMatrix(List<List<int>> matrix)
{
Console.WriteLine("[");
foreach (List<int> row in matrix)
{
Console.WriteLine(" [" + string.Join(", ", row.Select(r => $"{r}")) + "],");
}
Console.WriteLine("]");
}
} }

View File

@ -23,25 +23,25 @@ public class TreeNode
* @param arr * @param arr
* @return * @return
*/ */
public static TreeNode? ArrToTree(int?[] arr) public static TreeNode? ListToTree(List<int?> arr)
{ {
if (arr.Length == 0 || arr[0] == null) if (arr.Count == 0 || arr[0] == null)
return null; return null;
TreeNode root = new TreeNode((int)arr[0]); TreeNode root = new TreeNode(arr[0]!.Value);
Queue<TreeNode> queue = new Queue<TreeNode>(); Queue<TreeNode> queue = new Queue<TreeNode>();
queue.Enqueue(root); queue.Enqueue(root);
int i = 0; int i = 0;
while (queue.Count != 0) while (queue.Count != 0)
{ {
TreeNode node = queue.Dequeue(); TreeNode node = queue.Dequeue();
if (++i >= arr.Length) break; if (++i >= arr.Count) break;
if (arr[i] != null) if (arr[i] != null)
{ {
node.left = new TreeNode((int)arr[i]); node.left = new TreeNode((int)arr[i]);
queue.Enqueue(node.left); queue.Enqueue(node.left);
} }
if (++i >= arr.Length) break; if (++i >= arr.Count) break;
if (arr[i] != null) if (arr[i] != null)
{ {
node.right = new TreeNode((int)arr[i]); node.right = new TreeNode((int)arr[i]);
@ -92,6 +92,6 @@ public class TreeNode
return root; return root;
TreeNode? left = GetTreeNode(root.left, val); TreeNode? left = GetTreeNode(root.left, val);
TreeNode? right = GetTreeNode(root.right, val); TreeNode? right = GetTreeNode(root.right, val);
return left != null ? left : right; return left ?? right;
} }
} }

View File

@ -9,14 +9,14 @@ namespace hello_algo.include;
/* 顶点类 */ /* 顶点类 */
public class Vertex public class Vertex
{ {
public int Val { get; init; } public int val;
public Vertex(int val) public Vertex(int val)
{ {
Val = val; this.val = val;
} }
/* 输入值列表 vals ,返回顶点列表 vets */ /* 输入值列表 vals ,返回顶点列表 vets */
public static Vertex[] valsToVets(int[] vals) public static Vertex[] ValsToVets(int[] vals)
{ {
Vertex[] vets = new Vertex[vals.Length]; Vertex[] vets = new Vertex[vals.Length];
for (int i = 0; i < vals.Length; i++) for (int i = 0; i < vals.Length; i++)
@ -27,12 +27,12 @@ public class Vertex
} }
/* 输入顶点列表 vets ,返回值列表 vals */ /* 输入顶点列表 vets ,返回值列表 vals */
public static List<int> vetsToVals(List<Vertex> vets) public static List<int> VetsToVals(List<Vertex> vets)
{ {
List<int> vals = new List<int>(); List<int> vals = new List<int>();
foreach (Vertex vet in vets) foreach (Vertex vet in vets)
{ {
vals.Add(vet.Val); vals.Add(vet.val);
} }
return vals; return vals;
} }