Skip to content

Commit

Permalink
Update JavaScript style (Chapter of Tree)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tse committed Dec 13, 2022
1 parent 58ca52d commit a171414
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions codes/javascript/chapter_tree/binary_search_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 构建二叉搜索树
}

Expand Down Expand Up @@ -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());

Expand Down
12 changes: 6 additions & 6 deletions codes/javascript/chapter_tree/binary_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions codes/javascript/chapter_tree/binary_tree_bfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ function hierOrder(root) {
queue.push(node.left); // 左子结点入队
if (node.right)
queue.push(node.right); // 右子结点入队

}
return list;
}

/* 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);

Expand Down
6 changes: 3 additions & 3 deletions codes/javascript/chapter_tree/binary_tree_dfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a171414

Please sign in to comment.