mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-16 07:08:21 +08: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:
86
ja/codes/javascript/modules/PrintUtil.js
Normal file
86
ja/codes/javascript/modules/PrintUtil.js
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* File: PrintUtil.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require('./TreeNode');
|
||||
|
||||
/* 連結リストを出力 */
|
||||
function printLinkedList(head) {
|
||||
let list = [];
|
||||
while (head !== null) {
|
||||
list.push(head.val.toString());
|
||||
head = head.next;
|
||||
}
|
||||
console.log(list.join(' -> '));
|
||||
}
|
||||
|
||||
function Trunk(prev, str) {
|
||||
this.prev = prev;
|
||||
this.str = str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 二分木を出力
|
||||
* This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
*/
|
||||
function printTree(root) {
|
||||
printTree(root, null, false);
|
||||
}
|
||||
|
||||
/* 二分木を出力 */
|
||||
function printTree(root, prev, isRight) {
|
||||
if (root === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
let prev_str = ' ';
|
||||
let trunk = new Trunk(prev, prev_str);
|
||||
|
||||
printTree(root.right, trunk, true);
|
||||
|
||||
if (!prev) {
|
||||
trunk.str = '———';
|
||||
} else if (isRight) {
|
||||
trunk.str = '/———';
|
||||
prev_str = ' |';
|
||||
} else {
|
||||
trunk.str = '\\———';
|
||||
prev.str = prev_str;
|
||||
}
|
||||
|
||||
showTrunks(trunk);
|
||||
console.log(' ' + root.val);
|
||||
|
||||
if (prev) {
|
||||
prev.str = prev_str;
|
||||
}
|
||||
trunk.str = ' |';
|
||||
|
||||
printTree(root.left, trunk, false);
|
||||
}
|
||||
|
||||
function showTrunks(p) {
|
||||
if (!p) {
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p.prev);
|
||||
process.stdout.write(p.str);
|
||||
}
|
||||
|
||||
/* ヒープを出力 */
|
||||
function printHeap(arr) {
|
||||
console.log('ヒープの配列表現:');
|
||||
console.log(arr);
|
||||
console.log('ヒープの木構造表現:');
|
||||
printTree(arrToTree(arr));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
printLinkedList,
|
||||
printTree,
|
||||
printHeap,
|
||||
};
|
||||
Reference in New Issue
Block a user