mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
完善所以c#相关的文档和代码
This commit is contained in:
125
codes/csharp/include/PrintUtil.cs
Normal file
125
codes/csharp/include/PrintUtil.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.SymbolStore;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace hello_algo.include
|
||||
{
|
||||
public class Trunk
|
||||
{
|
||||
public Trunk? prev;
|
||||
public String str;
|
||||
|
||||
public Trunk(Trunk? prev, String str)
|
||||
{
|
||||
this.prev = prev;
|
||||
this.str = str;
|
||||
}
|
||||
};
|
||||
|
||||
public class PrintUtil
|
||||
{
|
||||
/**
|
||||
* Print a linked list
|
||||
* @param head
|
||||
*/
|
||||
public static void printLinkedList(ListNode head)
|
||||
{
|
||||
List<String> list = new();
|
||||
while (head != null)
|
||||
{
|
||||
list.Add(head.val.ToString());
|
||||
head = head.next;
|
||||
}
|
||||
Console.Write(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/
|
||||
* @param root
|
||||
*/
|
||||
public static void printTree(TreeNode? root)
|
||||
{
|
||||
printTree(root, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a binary tree
|
||||
* @param root
|
||||
* @param prev
|
||||
* @param isLeft
|
||||
*/
|
||||
public static void printTree(TreeNode? root, Trunk? prev, bool isLeft)
|
||||
{
|
||||
if (root == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
String prev_str = " ";
|
||||
Trunk trunk = new Trunk(prev, prev_str);
|
||||
|
||||
printTree(root.right, trunk, true);
|
||||
|
||||
if (prev == null)
|
||||
{
|
||||
trunk.str = "———";
|
||||
}
|
||||
else if (isLeft)
|
||||
{
|
||||
trunk.str = "/———";
|
||||
prev_str = " |";
|
||||
}
|
||||
else
|
||||
{
|
||||
trunk.str = "\\———";
|
||||
prev.str = prev_str;
|
||||
}
|
||||
|
||||
showTrunks(trunk);
|
||||
Console.Write(" " + root.val);
|
||||
|
||||
if (prev != null)
|
||||
{
|
||||
prev.str = prev_str;
|
||||
}
|
||||
trunk.str = " |";
|
||||
|
||||
printTree(root.left, trunk, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to print branches of the binary tree
|
||||
* @param p
|
||||
*/
|
||||
public static void showTrunks(Trunk? p)
|
||||
{
|
||||
if (p == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p.prev);
|
||||
Console.Write(p.str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a hash map
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
* @param map
|
||||
*/
|
||||
public static void printHashMap<K, V>(Dictionary<K, V> map) where K : notnull
|
||||
{
|
||||
foreach (var kv in map.Keys)
|
||||
{
|
||||
Console.WriteLine(kv.ToString() + " -> " + map[kv]?.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
92
codes/csharp/include/TreeNode.cs
Normal file
92
codes/csharp/include/TreeNode.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
namespace hello_algo.include
|
||||
{
|
||||
public class TreeNode
|
||||
{
|
||||
public int? val; // 结点值
|
||||
public int height; // 结点高度
|
||||
public TreeNode? left; // 左子结点引用
|
||||
public TreeNode? right; // 右子结点引用
|
||||
|
||||
public TreeNode(int? x)
|
||||
{
|
||||
val = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a binary tree with an array
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode? arrToTree(int?[] arr)
|
||||
{
|
||||
if (arr.Length == 0)
|
||||
return null;
|
||||
|
||||
TreeNode root = new TreeNode(arr[0]);
|
||||
Queue<TreeNode> queue = new Queue<TreeNode>();
|
||||
queue.Enqueue(root);
|
||||
int i = 1;
|
||||
while (queue.Count!=0)
|
||||
{
|
||||
TreeNode node = queue.Dequeue();
|
||||
if (arr[i] != null)
|
||||
{
|
||||
node.left = new TreeNode(arr[i]);
|
||||
queue.Enqueue(node.left);
|
||||
}
|
||||
i++;
|
||||
if (arr[i] != null)
|
||||
{
|
||||
node.right = new TreeNode(arr[i]);
|
||||
queue.Enqueue(node.right);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a binary tree to a list
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
public static List<int?> treeToList(TreeNode root)
|
||||
{
|
||||
List<int?> list = new();
|
||||
if (root == null) return list;
|
||||
Queue<TreeNode?> queue = new();
|
||||
while (queue.Count != 0)
|
||||
{
|
||||
TreeNode? node = queue.Dequeue();
|
||||
if (node != null)
|
||||
{
|
||||
list.Add(node.val);
|
||||
queue.Enqueue(node.left);
|
||||
queue.Enqueue(node.right);
|
||||
}
|
||||
else
|
||||
{
|
||||
list.Add(null);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a tree node with specific value in a binary tree
|
||||
* @param root
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode? getTreeNode(TreeNode? root, int val)
|
||||
{
|
||||
if (root == null)
|
||||
return null;
|
||||
if (root.val == val)
|
||||
return root;
|
||||
TreeNode? left = getTreeNode(root.left, val);
|
||||
TreeNode? right = getTreeNode(root.right, val);
|
||||
return left != null ? left : right;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user