Files
hello-algo/ko/codes/javascript/modules/PrintUtil.js
T
Yudong Jin 2f561d3d51 feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition

* Use UTF-8 for Python CI output on Windows

* Remove print-book promotion from the Korean site

* fix(ko): preserve emphasis before Korean particles
2026-10-29 03:46:43 +08:00

87 lines
1.6 KiB
JavaScript

/**
* File: PrintUtil.js
* Created Time: 2022-12-04
* Author: IsChristina ([email protected])
*/
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;
}
/**
* 이진 트리 출력
* 이 트리 출력 코드는 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,
};