mirror of
https://github.com/krahets/hello-algo.git
synced 2026-09-02 05:07:13 +00:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* File: print_util.dart
|
||||
* Created Time: 2023-01-23
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'list_node.dart';
|
||||
import 'tree_node.dart';
|
||||
|
||||
class Trunk {
|
||||
Trunk? prev;
|
||||
String str;
|
||||
|
||||
Trunk(this.prev, this.str);
|
||||
}
|
||||
|
||||
/* 行列を出力する (Array) */
|
||||
void printMatrix(List<List<int>> matrix) {
|
||||
print("[");
|
||||
for (List<int> row in matrix) {
|
||||
print(" $row,");
|
||||
}
|
||||
print("]");
|
||||
}
|
||||
|
||||
/* 連結リストを出力 */
|
||||
void printLinkedList(ListNode? head) {
|
||||
List<String> list = [];
|
||||
|
||||
while (head != null) {
|
||||
list.add('${head.val}');
|
||||
head = head.next;
|
||||
}
|
||||
|
||||
print(list.join(' -> '));
|
||||
}
|
||||
|
||||
/**
|
||||
* 二分木を出力
|
||||
* This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
*/
|
||||
void printTree(TreeNode? root, [Trunk? prev = null, bool isRight = false]) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String prev_str = ' ';
|
||||
Trunk trunk = Trunk(prev, prev_str);
|
||||
|
||||
printTree(root.right, trunk, true);
|
||||
|
||||
if (prev == null) {
|
||||
trunk.str = '———';
|
||||
} else if (isRight) {
|
||||
trunk.str = '/———';
|
||||
prev_str = ' |';
|
||||
} else {
|
||||
trunk.str = '\\———';
|
||||
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);
|
||||
}
|
||||
|
||||
/* ヒープを出力 */
|
||||
void printHeap(List<int> heap) {
|
||||
print("ヒープの配列表現:$heap");
|
||||
print("ヒープの木構造表示:");
|
||||
TreeNode? root = listToTree(heap);
|
||||
printTree(root);
|
||||
}
|
||||
Reference in New Issue
Block a user