mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-16 21:50:59 +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,107 @@
|
||||
/**
|
||||
* File: PrintUtil.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package utils
|
||||
|
||||
import java.util.*
|
||||
|
||||
class Trunk(var prev: Trunk?, var str: String)
|
||||
|
||||
/* 行列を出力する(Array) */
|
||||
fun <T> printMatrix(matrix: Array<Array<T>>) {
|
||||
println("[")
|
||||
for (row in matrix) {
|
||||
println(" $row,")
|
||||
}
|
||||
println("]")
|
||||
}
|
||||
|
||||
/* 行列を出力する(List) */
|
||||
fun <T> printMatrix(matrix: MutableList<MutableList<T>>) {
|
||||
println("[")
|
||||
for (row in matrix) {
|
||||
println(" $row,")
|
||||
}
|
||||
println("]")
|
||||
}
|
||||
|
||||
/* 連結リストを出力 */
|
||||
fun printLinkedList(h: ListNode?) {
|
||||
var head = h
|
||||
val list = mutableListOf<String>()
|
||||
while (head != null) {
|
||||
list.add(head._val.toString())
|
||||
head = head.next
|
||||
}
|
||||
println(list.joinToString(separator = " -> "))
|
||||
}
|
||||
|
||||
/* 二分木を出力 */
|
||||
fun printTree(root: TreeNode?) {
|
||||
printTree(root, null, false)
|
||||
}
|
||||
|
||||
/**
|
||||
* 二分木を出力
|
||||
* This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
*/
|
||||
fun printTree(root: TreeNode?, prev: Trunk?, isRight: Boolean) {
|
||||
if (root == null) {
|
||||
return
|
||||
}
|
||||
|
||||
var prevStr = " "
|
||||
val trunk = Trunk(prev, prevStr)
|
||||
|
||||
printTree(root.right, trunk, true)
|
||||
|
||||
if (prev == null) {
|
||||
trunk.str = "———"
|
||||
} else if (isRight) {
|
||||
trunk.str = "/———"
|
||||
prevStr = " |"
|
||||
} else {
|
||||
trunk.str = "\\———"
|
||||
prev.str = prevStr
|
||||
}
|
||||
|
||||
showTrunks(trunk)
|
||||
println(" ${root._val}")
|
||||
|
||||
if (prev != null) {
|
||||
prev.str = prevStr
|
||||
}
|
||||
trunk.str = " |"
|
||||
|
||||
printTree(root.left, trunk, false)
|
||||
}
|
||||
|
||||
fun showTrunks(p: Trunk?) {
|
||||
if (p == null) {
|
||||
return
|
||||
}
|
||||
showTrunks(p.prev)
|
||||
print(p.str)
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを出力 */
|
||||
fun <K, V> printHashMap(map: Map<K, V>) {
|
||||
for ((key, value) in map) {
|
||||
println("${key.toString()} -> $value")
|
||||
}
|
||||
}
|
||||
|
||||
/* ヒープを出力 */
|
||||
fun printHeap(queue: Queue<Int>?) {
|
||||
val list = mutableListOf<Int?>()
|
||||
queue?.let { list.addAll(it) }
|
||||
print("ヒープの配列表現:")
|
||||
println(list)
|
||||
println("ヒープの木構造表現:")
|
||||
val root = TreeNode.listToTree(list)
|
||||
printTree(root)
|
||||
}
|
||||
Reference in New Issue
Block a user