2023-02-16 02:23:06 +08:00
|
|
|
/**
|
2023-04-24 04:11:18 +08:00
|
|
|
* File: print_util.dart
|
2023-02-16 02:23:06 +08:00
|
|
|
* Created Time: 2023-01-23
|
|
|
|
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
|
|
|
*/
|
2023-04-10 03:14:11 +08:00
|
|
|
|
2023-02-16 02:23:06 +08:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'list_node.dart';
|
|
|
|
import 'tree_node.dart';
|
|
|
|
|
|
|
|
class Trunk {
|
|
|
|
Trunk? prev;
|
|
|
|
String str;
|
|
|
|
|
|
|
|
Trunk(this.prev, this.str);
|
2023-05-18 19:04:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void printMatrix(List<List<int>> matrix) {
|
|
|
|
print("[");
|
|
|
|
for (List<int> row in matrix) {
|
|
|
|
print(" $row,");
|
|
|
|
}
|
|
|
|
print("]");
|
2023-02-16 02:23:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void printLinkedList(ListNode? head) {
|
|
|
|
List<String> list = [];
|
|
|
|
|
|
|
|
while (head != null) {
|
|
|
|
list.add('${head.val}');
|
|
|
|
head = head.next;
|
|
|
|
}
|
|
|
|
|
|
|
|
print(list.join(' -> '));
|
|
|
|
}
|
|
|
|
|
|
|
|
void printTree(TreeNode? root, [Trunk? prev = null, bool isLeft = false]) {
|
|
|
|
if (root == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String prev_str = ' ';
|
|
|
|
Trunk trunk = Trunk(prev, prev_str);
|
|
|
|
|
|
|
|
printTree(root.right, trunk, true);
|
|
|
|
|
|
|
|
if (prev == null) {
|
2023-05-30 13:00:16 +08:00
|
|
|
trunk.str = '———';
|
2023-02-16 02:23:06 +08:00
|
|
|
} else if (isLeft) {
|
2023-05-30 13:00:16 +08:00
|
|
|
trunk.str = '/———';
|
2023-02-16 02:23:06 +08:00
|
|
|
prev_str = ' |';
|
|
|
|
} else {
|
2023-05-30 13:00:16 +08:00
|
|
|
trunk.str = '\\———';
|
2023-02-16 02:23:06 +08:00
|
|
|
prev.str = prev_str;
|
|
|
|
}
|
|
|
|
showTrunks(trunk);
|
|
|
|
print(' ${root.val}');
|
|
|
|
|
|
|
|
if (prev != null) {
|
|
|
|
prev.str = prev_str;
|
|
|
|
}
|
|
|
|
trunk.str = ' |';
|
|
|
|
|
|
|
|
printTree(root.left, trunk, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void showTrunks(Trunk? p) {
|
|
|
|
if (p == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
showTrunks(p.prev);
|
|
|
|
stdout.write(p.str);
|
|
|
|
}
|
2023-04-10 03:14:11 +08:00
|
|
|
|
|
|
|
void printHeap(List<int> heap) {
|
|
|
|
print("堆的数组表示:$heap");
|
|
|
|
print("堆的树状表示:");
|
|
|
|
TreeNode? root = listToTree(heap);
|
|
|
|
printTree(root);
|
|
|
|
}
|