You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
3.8 KiB
135 lines
3.8 KiB
2 years ago
|
/**
|
||
|
* File: PrintUtil.cs
|
||
|
* Created Time: 2022-12-23
|
||
|
* Author: haptear (haptear@hotmail.com), krahets (krahets@163.com)
|
||
|
*/
|
||
2 years ago
|
|
||
2 years ago
|
namespace hello_algo.utils;
|
||
2 years ago
|
|
||
2 years ago
|
public class Trunk {
|
||
2 years ago
|
public Trunk? prev;
|
||
2 years ago
|
public string str;
|
||
2 years ago
|
|
||
2 years ago
|
public Trunk(Trunk? prev, string str) {
|
||
2 years ago
|
this.prev = prev;
|
||
|
this.str = str;
|
||
|
}
|
||
|
};
|
||
2 years ago
|
|
||
2 years ago
|
public class PrintUtil {
|
||
|
/* Print a list */
|
||
|
public static void PrintList<T>(List<T> list) {
|
||
2 years ago
|
Console.WriteLine("[" + string.Join(", ", list) + "]");
|
||
|
}
|
||
|
|
||
2 years ago
|
/* Print a matrix (Array) */
|
||
2 years ago
|
public static void PrintMatrix<T>(T[][] matrix) {
|
||
2 years ago
|
Console.WriteLine("[");
|
||
2 years ago
|
foreach (T[] row in matrix) {
|
||
2 years ago
|
Console.WriteLine(" " + string.Join(", ", row) + ",");
|
||
|
}
|
||
|
Console.WriteLine("]");
|
||
|
}
|
||
|
|
||
|
/* Print a matrix (List) */
|
||
2 years ago
|
public static void PrintMatrix<T>(List<List<T>> matrix) {
|
||
2 years ago
|
Console.WriteLine("[");
|
||
2 years ago
|
foreach (List<T> row in matrix) {
|
||
2 years ago
|
Console.WriteLine(" " + string.Join(", ", row) + ",");
|
||
|
}
|
||
|
Console.WriteLine("]");
|
||
|
}
|
||
|
|
||
2 years ago
|
/* Print a linked list */
|
||
|
public static void PrintLinkedList(ListNode head) {
|
||
2 years ago
|
List<string> list = new();
|
||
2 years ago
|
while (head != null) {
|
||
2 years ago
|
list.Add(head.val.ToString());
|
||
|
head = head.next;
|
||
2 years ago
|
}
|
||
2 years ago
|
Console.Write(string.Join(" -> ", list));
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
/**
|
||
|
* The interface of the tree printer
|
||
|
* This tree printer is borrowed from TECHIE DELIGHT
|
||
|
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||
|
*/
|
||
2 years ago
|
public static void PrintTree(TreeNode? root) {
|
||
2 years ago
|
PrintTree(root, null, false);
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
/* Print a binary tree */
|
||
|
public static void PrintTree(TreeNode? root, Trunk? prev, bool isLeft) {
|
||
|
if (root == null) {
|
||
2 years ago
|
return;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
string prev_str = " ";
|
||
2 years ago
|
Trunk trunk = new Trunk(prev, prev_str);
|
||
2 years ago
|
|
||
2 years ago
|
PrintTree(root.right, trunk, true);
|
||
2 years ago
|
|
||
2 years ago
|
if (prev == null) {
|
||
2 years ago
|
trunk.str = "———";
|
||
2 years ago
|
} else if (isLeft) {
|
||
2 years ago
|
trunk.str = "/———";
|
||
|
prev_str = " |";
|
||
2 years ago
|
} else {
|
||
2 years ago
|
trunk.str = "\\———";
|
||
|
prev.str = prev_str;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
ShowTrunks(trunk);
|
||
2 years ago
|
Console.WriteLine(" " + root.val);
|
||
2 years ago
|
|
||
2 years ago
|
if (prev != null) {
|
||
2 years ago
|
prev.str = prev_str;
|
||
2 years ago
|
}
|
||
2 years ago
|
trunk.str = " |";
|
||
2 years ago
|
|
||
2 years ago
|
PrintTree(root.left, trunk, false);
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
/* Helper function to print branches of the binary tree */
|
||
|
public static void ShowTrunks(Trunk? p) {
|
||
|
if (p == null) {
|
||
2 years ago
|
return;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
ShowTrunks(p.prev);
|
||
2 years ago
|
Console.Write(p.str);
|
||
|
}
|
||
|
|
||
2 years ago
|
/* Print a hash map */
|
||
|
public static void PrintHashMap<K, V>(Dictionary<K, V> map) where K : notnull {
|
||
|
foreach (var kv in map.Keys) {
|
||
2 years ago
|
Console.WriteLine(kv.ToString() + " -> " + map[kv]?.ToString());
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
/* Print a heap */
|
||
|
public static void PrintHeap(Queue<int> queue) {
|
||
2 years ago
|
Console.Write("堆的数组表示:");
|
||
|
List<int> list = queue.ToList();
|
||
|
Console.WriteLine(string.Join(',', list));
|
||
|
Console.WriteLine("堆的树状表示:");
|
||
2 years ago
|
TreeNode tree = TreeNode.ListToTree(list.Cast<int?>().ToList());
|
||
2 years ago
|
PrintTree(tree);
|
||
|
}
|
||
|
|
||
2 years ago
|
/* Print a PriorityQueue */
|
||
|
public static void PrintHeap(PriorityQueue<int, int> queue) {
|
||
2 years ago
|
var newQueue = new PriorityQueue<int, int>(queue.UnorderedItems, queue.Comparer);
|
||
2 years ago
|
Console.Write("堆的数组表示:");
|
||
2 years ago
|
List<int> list = new List<int>();
|
||
2 years ago
|
while (newQueue.TryDequeue(out int element, out int priority)) {
|
||
2 years ago
|
list.Add(element);
|
||
|
}
|
||
|
Console.WriteLine("堆的树状表示:");
|
||
|
Console.WriteLine(string.Join(',', list.ToList()));
|
||
2 years ago
|
TreeNode tree = TreeNode.ListToTree(list.Cast<int?>().ToList());
|
||
2 years ago
|
PrintTree(tree);
|
||
|
}
|
||
2 years ago
|
}
|