diff --git a/codes/javascript/chapter_tree/binary_search_tree.js b/codes/javascript/chapter_tree/binary_search_tree.js index e97c3c926..332525f2a 100644 --- a/codes/javascript/chapter_tree/binary_search_tree.js +++ b/codes/javascript/chapter_tree/binary_search_tree.js @@ -5,6 +5,7 @@ */ const Tree = require("../include/TreeNode"); +const { printTree } = require("../include/PrintUtil"); /* 二叉搜索树 */ var root; @@ -122,7 +123,7 @@ function min(root) { var nums = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]; BinarySearchTree(nums) console.log("\n初始化的二叉树为\n"); -console.log(getRoot()); +printTree(getRoot()); /* 查找结点 */ let node = search(5); @@ -131,15 +132,15 @@ console.log("\n查找到的结点对象为 " + node + ",结点值 = " + node.v /* 插入结点 */ node = insert(16); console.log("\n插入结点 16 后,二叉树为\n"); -console.log(getRoot()); +printTree(getRoot()); /* 删除结点 */ remove(1); console.log("\n删除结点 1 后,二叉树为\n"); -console.log(getRoot()); +printTree(getRoot()); remove(2); console.log("\n删除结点 2 后,二叉树为\n"); -console.log(getRoot()); +printTree(getRoot()); remove(4); console.log("\n删除结点 4 后,二叉树为\n"); -console.log(getRoot()); \ No newline at end of file +printTree(getRoot()); \ No newline at end of file diff --git a/codes/javascript/chapter_tree/binary_tree.js b/codes/javascript/chapter_tree/binary_tree.js index 7cf94414c..1b91ef5a9 100644 --- a/codes/javascript/chapter_tree/binary_tree.js +++ b/codes/javascript/chapter_tree/binary_tree.js @@ -5,7 +5,8 @@ */ const Tree = require("../include/TreeNode"); - +const { printTree } = require("../include/PrintUtil"); + /* 初始化二叉树 */ // 初始化结点 let n1 = new Tree.TreeNode(1), @@ -19,7 +20,7 @@ n1.right = n3; n2.left = n4; n2.right = n5; console.log("\n初始化二叉树\n") -console.log(n1) +printTree(n1) /* 插入与删除结点 */ let P = new Tree.TreeNode(0); @@ -27,8 +28,8 @@ let P = new Tree.TreeNode(0); n1.left = P; P.left = n2; console.log("\n插入结点 P 后\n"); -console.log(n1); +printTree(n1); // 删除结点 P n1.left = n2; console.log("\n删除结点 P 后\n"); -console.log(n1); +printTree(n1); diff --git a/codes/javascript/chapter_tree/binary_tree_bfs.js b/codes/javascript/chapter_tree/binary_tree_bfs.js index 8d8bb0943..f7e6aa0e3 100644 --- a/codes/javascript/chapter_tree/binary_tree_bfs.js +++ b/codes/javascript/chapter_tree/binary_tree_bfs.js @@ -4,7 +4,8 @@ * Author: IsChristina (christinaxia77@foxmail.com) */ -let { arrToTree } = require("../include/TreeNode"); +const { arrToTree } = require("../include/TreeNode"); +const { printTree } = require("../include/PrintUtil"); /* 层序遍历 */ function hierOrder(root) { @@ -29,7 +30,7 @@ function hierOrder(root) { // 这里借助了一个从数组直接生成二叉树的函数 var root = arrToTree([1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, null ]); console.log("\n初始化二叉树\n"); -console.log(root); +printTree(root); /* 层序遍历 */ let list = hierOrder(root); diff --git a/codes/javascript/chapter_tree/binary_tree_dfs.js b/codes/javascript/chapter_tree/binary_tree_dfs.js index 072d95f89..9dd3083ff 100644 --- a/codes/javascript/chapter_tree/binary_tree_dfs.js +++ b/codes/javascript/chapter_tree/binary_tree_dfs.js @@ -4,7 +4,8 @@ * Author: IsChristina (christinaxia77@foxmail.com) */ -let { arrToTree } = require("../include/TreeNode"); +const { arrToTree } = require("../include/TreeNode"); +const { printTree } = require("../include/PrintUtil"); // 初始化列表,用于存储遍历序列 var list = [] @@ -41,7 +42,7 @@ function postOrder(root) { // 这里借助了一个从数组直接生成二叉树的函数 var root = arrToTree([1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, null]); console.log("\n初始化二叉树\n"); -console.log(root); +printTree(root); /* 前序遍历 */ list.length = 0; diff --git a/codes/javascript/include/PrintUtil.js b/codes/javascript/include/PrintUtil.js new file mode 100644 index 000000000..daeb24a7d --- /dev/null +++ b/codes/javascript/include/PrintUtil.js @@ -0,0 +1,88 @@ +/** + * File: PrintUtil.js + * Created Time: 2022-12-04 + * Author: IsChristina (christinaxia77@foxmail.com) + */ + +function Trunk(prev, str) { + this.prev = prev; + this.str = str; +} + +/** + * Print a linked list + * @param head + */ +function printLinkedList(head) { + let list = []; + while (head !== null) { + list.push(head.val.toString()); + head = head.next; + } + console.log(list.join(" -> ")); +} + +/** + * The interface of the tree printer + * This tree printer is borrowed from TECHIE DELIGHT + * https://www.techiedelight.com/c-program-print-binary-tree/ + * @param root + */ +function printTree(root) { + printTree(root, null, false); +} + +/** + * Print a binary tree + * @param root + * @param prev + * @param isLeft + */ +function printTree(root, prev, isLeft) { + 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 (isLeft) { + 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); +} + +/** + * Helper function to print branches of the binary tree + * @param p + */ +function showTrunks(p) { + if (!p) { + return; + } + + showTrunks(p.prev); + console.log(p.str); +} + +module.exports = { + printTree, + printLinkedList, +}