2022-12-04 23:14:09 +08:00
|
|
|
/**
|
|
|
|
* File: PrintUtil.js
|
|
|
|
* Created Time: 2022-12-04
|
|
|
|
* Author: IsChristina (christinaxia77@foxmail.com)
|
|
|
|
*/
|
|
|
|
|
2023-04-17 21:58:11 +08:00
|
|
|
const { arrToTree } = require('./TreeNode');
|
2023-02-07 20:48:54 +08:00
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
/* 打印链表 */
|
2022-12-04 23:14:09 +08:00
|
|
|
function printLinkedList(head) {
|
|
|
|
let list = [];
|
|
|
|
while (head !== null) {
|
|
|
|
list.push(head.val.toString());
|
|
|
|
head = head.next;
|
|
|
|
}
|
2023-04-17 21:58:11 +08:00
|
|
|
console.log(list.join(' -> '));
|
2022-12-04 23:14:09 +08:00
|
|
|
}
|
|
|
|
|
2023-02-01 03:23:29 +08:00
|
|
|
function Trunk(prev, str) {
|
|
|
|
this.prev = prev;
|
|
|
|
this.str = str;
|
|
|
|
}
|
|
|
|
|
2022-12-04 23:14:09 +08:00
|
|
|
/**
|
2024-03-31 03:06:41 +08:00
|
|
|
* 打印二叉树
|
2022-12-04 23:14:09 +08:00
|
|
|
* This tree printer is borrowed from TECHIE DELIGHT
|
|
|
|
* https://www.techiedelight.com/c-program-print-binary-tree/
|
|
|
|
*/
|
|
|
|
function printTree(root) {
|
|
|
|
printTree(root, null, false);
|
|
|
|
}
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
/* 打印二叉树 */
|
2023-11-01 05:14:22 +08:00
|
|
|
function printTree(root, prev, isRight) {
|
2022-12-04 23:14:09 +08:00
|
|
|
if (root === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-17 21:58:11 +08:00
|
|
|
let prev_str = ' ';
|
2022-12-04 23:14:09 +08:00
|
|
|
let trunk = new Trunk(prev, prev_str);
|
|
|
|
|
|
|
|
printTree(root.right, trunk, true);
|
|
|
|
|
|
|
|
if (!prev) {
|
2023-04-17 21:58:11 +08:00
|
|
|
trunk.str = '———';
|
2023-11-01 05:14:22 +08:00
|
|
|
} else if (isRight) {
|
2023-04-17 21:58:11 +08:00
|
|
|
trunk.str = '/———';
|
|
|
|
prev_str = ' |';
|
2022-12-04 23:14:09 +08:00
|
|
|
} else {
|
2023-04-17 21:58:11 +08:00
|
|
|
trunk.str = '\\———';
|
2022-12-04 23:14:09 +08:00
|
|
|
prev.str = prev_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
showTrunks(trunk);
|
2023-04-17 21:58:11 +08:00
|
|
|
console.log(' ' + root.val);
|
2022-12-04 23:14:09 +08:00
|
|
|
|
|
|
|
if (prev) {
|
|
|
|
prev.str = prev_str;
|
|
|
|
}
|
2023-04-17 21:58:11 +08:00
|
|
|
trunk.str = ' |';
|
2022-12-04 23:14:09 +08:00
|
|
|
|
|
|
|
printTree(root.left, trunk, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function showTrunks(p) {
|
|
|
|
if (!p) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
showTrunks(p.prev);
|
2022-12-13 21:38:18 +08:00
|
|
|
process.stdout.write(p.str);
|
2022-12-04 23:14:09 +08:00
|
|
|
}
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
/* 打印堆 */
|
2023-02-07 20:48:54 +08:00
|
|
|
function printHeap(arr) {
|
2023-04-17 21:58:11 +08:00
|
|
|
console.log('堆的数组表示:');
|
2023-02-07 20:48:54 +08:00
|
|
|
console.log(arr);
|
2023-04-17 21:58:11 +08:00
|
|
|
console.log('堆的树状表示:');
|
2023-02-07 20:48:54 +08:00
|
|
|
printTree(arrToTree(arr));
|
|
|
|
}
|
|
|
|
|
2022-12-13 21:38:18 +08:00
|
|
|
module.exports = {
|
2022-12-04 23:14:09 +08:00
|
|
|
printLinkedList,
|
2023-02-07 20:48:54 +08:00
|
|
|
printTree,
|
2023-04-17 21:58:11 +08:00
|
|
|
printHeap,
|
2023-02-07 20:48:54 +08:00
|
|
|
};
|