From a171414c7bb932add01e6990640c5f1d00d56551 Mon Sep 17 00:00:00 2001 From: justin Date: Tue, 13 Dec 2022 21:32:47 +0800 Subject: [PATCH 1/3] Update JavaScript style (Chapter of Tree) --- codes/javascript/chapter_tree/binary_search_tree.js | 6 +++--- codes/javascript/chapter_tree/binary_tree.js | 12 ++++++------ codes/javascript/chapter_tree/binary_tree_bfs.js | 4 ++-- codes/javascript/chapter_tree/binary_tree_dfs.js | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/codes/javascript/chapter_tree/binary_search_tree.js b/codes/javascript/chapter_tree/binary_search_tree.js index 74b725080..1e0d92738 100644 --- a/codes/javascript/chapter_tree/binary_search_tree.js +++ b/codes/javascript/chapter_tree/binary_search_tree.js @@ -11,7 +11,7 @@ const { printTree } = require("../include/PrintUtil"); var root; function BinarySearchTree(nums) { - nums.sort((a,b) => { return a-b }); // 排序数组 + nums.sort((a, b) => { return a - b }); // 排序数组 root = buildTree(nums, 0, nums.length - 1); // 构建二叉搜索树 } @@ -120,8 +120,8 @@ function min(root) { /* Driver Code */ /* 初始化二叉搜索树 */ -var nums = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]; -BinarySearchTree(nums) +var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; +BinarySearchTree(nums); console.log("\n初始化的二叉树为\n"); printTree(getRoot()); diff --git a/codes/javascript/chapter_tree/binary_tree.js b/codes/javascript/chapter_tree/binary_tree.js index 1b91ef5a9..4f3d78525 100644 --- a/codes/javascript/chapter_tree/binary_tree.js +++ b/codes/javascript/chapter_tree/binary_tree.js @@ -10,17 +10,17 @@ const { printTree } = require("../include/PrintUtil"); /* 初始化二叉树 */ // 初始化结点 let n1 = new Tree.TreeNode(1), -n2 = new Tree.TreeNode(2), -n3 = new Tree.TreeNode(3), -n4 = new Tree.TreeNode(4), -n5 = new Tree.TreeNode(5); + n2 = new Tree.TreeNode(2), + n3 = new Tree.TreeNode(3), + n4 = new Tree.TreeNode(4), + n5 = new Tree.TreeNode(5); // 构建引用指向(即指针) n1.left = n2; n1.right = n3; n2.left = n4; n2.right = n5; -console.log("\n初始化二叉树\n") -printTree(n1) +console.log("\n初始化二叉树\n"); +printTree(n1); /* 插入与删除结点 */ let P = new Tree.TreeNode(0); diff --git a/codes/javascript/chapter_tree/binary_tree_bfs.js b/codes/javascript/chapter_tree/binary_tree_bfs.js index f7e6aa0e3..b52f52820 100644 --- a/codes/javascript/chapter_tree/binary_tree_bfs.js +++ b/codes/javascript/chapter_tree/binary_tree_bfs.js @@ -20,7 +20,7 @@ function hierOrder(root) { queue.push(node.left); // 左子结点入队 if (node.right) queue.push(node.right); // 右子结点入队 - + } return list; } @@ -28,7 +28,7 @@ function hierOrder(root) { /* Driver Code */ /* 初始化二叉树 */ // 这里借助了一个从数组直接生成二叉树的函数 -var root = arrToTree([1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, null ]); +var root = arrToTree([1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, null]); console.log("\n初始化二叉树\n"); printTree(root); diff --git a/codes/javascript/chapter_tree/binary_tree_dfs.js b/codes/javascript/chapter_tree/binary_tree_dfs.js index 9dd3083ff..51e557336 100644 --- a/codes/javascript/chapter_tree/binary_tree_dfs.js +++ b/codes/javascript/chapter_tree/binary_tree_dfs.js @@ -8,16 +8,16 @@ const { arrToTree } = require("../include/TreeNode"); const { printTree } = require("../include/PrintUtil"); // 初始化列表,用于存储遍历序列 -var list = [] +var list = []; /* 前序遍历 */ -function preOrder(root){ +function preOrder(root) { if (root === null) return; // 访问优先级:根结点 -> 左子树 -> 右子树 list.push(root.val); preOrder(root.left); preOrder(root.right); - } +} /* 中序遍历 */ function inOrder(root) { From 2a375ebcbb455ef6514a1bbc1350806204da9b13 Mon Sep 17 00:00:00 2001 From: justin Date: Tue, 13 Dec 2022 21:38:18 +0800 Subject: [PATCH 2/3] Fix the PrintUtil.js showTrunks --- codes/javascript/include/PrintUtil.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/javascript/include/PrintUtil.js b/codes/javascript/include/PrintUtil.js index daeb24a7d..3590f333b 100644 --- a/codes/javascript/include/PrintUtil.js +++ b/codes/javascript/include/PrintUtil.js @@ -79,10 +79,10 @@ function showTrunks(p) { } showTrunks(p.prev); - console.log(p.str); + process.stdout.write(p.str); } -module.exports = { +module.exports = { printTree, printLinkedList, } From 07ca137e786da16f70ac4ead9787abbb8da652a8 Mon Sep 17 00:00:00 2001 From: justin Date: Tue, 13 Dec 2022 21:44:14 +0800 Subject: [PATCH 3/3] Update JavaScript code to docs style (Chapter of Tree) --- codes/javascript/include/TreeNode.js | 6 +++--- docs/chapter_tree/binary_tree.md | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/codes/javascript/include/TreeNode.js b/codes/javascript/include/TreeNode.js index 20c709e96..6add12f0c 100644 --- a/codes/javascript/include/TreeNode.js +++ b/codes/javascript/include/TreeNode.js @@ -8,9 +8,9 @@ * Definition for a binary tree node. */ function TreeNode(val, left, right) { - this.val = (val === undefined ? 0 : val) // 结点值 - this.left = (left === undefined ? null : left) // 左子结点指针 - this.right = (right === undefined ? null : right) // 右子结点指针 + this.val = (val === undefined ? 0 : val); // 结点值 + this.left = (left === undefined ? null : left); // 左子结点指针 + this.right = (right === undefined ? null : right); // 右子结点指针 } /** diff --git a/docs/chapter_tree/binary_tree.md b/docs/chapter_tree/binary_tree.md index 5913a1a43..84270be01 100644 --- a/docs/chapter_tree/binary_tree.md +++ b/docs/chapter_tree/binary_tree.md @@ -65,9 +65,9 @@ comments: true ```js title="" /* 链表结点类 */ function TreeNode(val, left, right) { - this.val = (val === undefined ? 0 : val) // 结点值 - this.left = (left === undefined ? null : left) // 左子结点指针 - this.right = (right === undefined ? null : right) // 右子结点指针 + this.val = (val === undefined ? 0 : val); // 结点值 + this.left = (left === undefined ? null : left); // 左子结点指针 + this.right = (right === undefined ? null : right); // 右子结点指针 } ``` @@ -201,10 +201,10 @@ comments: true /* 初始化二叉树 */ // 初始化结点 let n1 = new TreeNode(1), - n2 = new TreeNode(2), - n3 = new TreeNode(3), - n4 = new TreeNode(4), - n5 = new TreeNode(5); + n2 = new TreeNode(2), + n3 = new TreeNode(3), + n4 = new TreeNode(4), + n5 = new TreeNode(5); // 构建引用指向(即指针) n1.left = n2; n1.right = n3;