Fomrat the JS and TS codes with prettier.

This commit is contained in:
krahets
2023-04-17 21:58:11 +08:00
parent 9a98ff8a5e
commit c4ea4e39f3
68 changed files with 634 additions and 510 deletions

View File

@@ -4,8 +4,8 @@
* Author: what-is-me (whatisme@outlook.jp)
*/
const { TreeNode } = require("../modules/TreeNode");
const { printTree } = require("../modules/PrintUtil");
const { TreeNode } = require('../modules/TreeNode');
const { printTree } = require('../modules/PrintUtil');
/* AVL 树*/
class AVLTree {
@@ -23,7 +23,8 @@ class AVLTree {
/* 更新节点高度 */
#updateHeight(node) {
// 节点高度等于最高子树高度 + 1
node.height = Math.max(this.height(node.left), this.height(node.right)) + 1;
node.height =
Math.max(this.height(node.left), this.height(node.right)) + 1;
}
/* 获取平衡因子 */
@@ -102,7 +103,8 @@ class AVLTree {
if (node === null) return new TreeNode(val);
/* 1. 查找插入位置,并插入节点 */
if (val < node.val) node.left = this.#insertHelper(node.left, val);
else if (val > node.val) node.right = this.#insertHelper(node.right, val);
else if (val > node.val)
node.right = this.#insertHelper(node.right, val);
else return node; // 重复节点不插入,直接返回
this.#updateHeight(node); // 更新节点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
@@ -121,7 +123,8 @@ class AVLTree {
if (node === null) return null;
/* 1. 查找节点,并删除之 */
if (val < node.val) node.left = this.#removeHelper(node.left, val);
else if (val > node.val) node.right = this.#removeHelper(node.right, val);
else if (val > node.val)
node.right = this.#removeHelper(node.right, val);
else {
if (node.left === null || node.right === null) {
const child = node.left !== null ? node.left : node.right;
@@ -165,13 +168,13 @@ class AVLTree {
function testInsert(tree, val) {
tree.insert(val);
console.log("\n插入节点 " + val + "AVL 树为");
console.log('\n插入节点 ' + val + 'AVL 树为');
printTree(tree.root);
}
function testRemove(tree, val) {
tree.remove(val);
console.log("\n删除节点 " + val + "AVL 树为");
console.log('\n删除节点 ' + val + 'AVL 树为');
printTree(tree.root);
}
@@ -202,4 +205,4 @@ testRemove(avlTree, 4); // 删除度为 2 的节点
/* 查询节点 */
const node = avlTree.search(7);
console.log("\n查找到的节点对象为", node, ",节点值 = " + node.val);
console.log('\n查找到的节点对象为', node, ',节点值 = ' + node.val);

View File

@@ -4,15 +4,17 @@
* Author: IsChristina (christinaxia77@foxmail.com)
*/
const { TreeNode } = require("../modules/TreeNode");
const { printTree } = require("../modules/PrintUtil");
const { TreeNode } = require('../modules/TreeNode');
const { printTree } = require('../modules/PrintUtil');
/* 二叉搜索树 */
let root;
function BinarySearchTree(nums) {
nums.sort((a, b) => { return a - b }); // 排序数组
root = buildTree(nums, 0, nums.length - 1); // 构建二叉搜索树
nums.sort((a, b) => {
return a - b;
}); // 排序数组
root = buildTree(nums, 0, nums.length - 1); // 构建二叉搜索树
}
/* 获取二叉树根节点 */
@@ -52,7 +54,8 @@ function search(num) {
function insert(num) {
// 若树为空,直接提前返回
if (root === null) return;
let cur = root, pre = null;
let cur = root,
pre = null;
// 循环查找,越过叶节点后跳出
while (cur !== null) {
// 找到重复节点,直接返回
@@ -73,7 +76,8 @@ function insert(num) {
function remove(num) {
// 若树为空,直接提前返回
if (root === null) return;
let cur = root, pre = null;
let cur = root,
pre = null;
// 循环查找,越过叶节点后跳出
while (cur !== null) {
// 找到待删除节点,跳出循环
@@ -112,25 +116,25 @@ function remove(num) {
/* 初始化二叉搜索树 */
const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
BinarySearchTree(nums);
console.log("\n初始化的二叉树为\n");
console.log('\n初始化的二叉树为\n');
printTree(getRoot());
/* 查找节点 */
let node = search(7);
console.log("\n查找到的节点对象为 " + node + ",节点值 = " + node.val);
console.log('\n查找到的节点对象为 ' + node + ',节点值 = ' + node.val);
/* 插入节点 */
insert(16);
console.log("\n插入节点 16 后,二叉树为\n");
console.log('\n插入节点 16 后,二叉树为\n');
printTree(getRoot());
/* 删除节点 */
remove(1);
console.log("\n删除节点 1 后,二叉树为\n");
console.log('\n删除节点 1 后,二叉树为\n');
printTree(getRoot());
remove(2);
console.log("\n删除节点 2 后,二叉树为\n");
console.log('\n删除节点 2 后,二叉树为\n');
printTree(getRoot());
remove(4);
console.log("\n删除节点 4 后,二叉树为\n");
console.log('\n删除节点 4 后,二叉树为\n');
printTree(getRoot());

View File

@@ -4,8 +4,8 @@
* Author: IsChristina (christinaxia77@foxmail.com)
*/
const { TreeNode } = require("../modules/TreeNode");
const { printTree } = require("../modules/PrintUtil");
const { TreeNode } = require('../modules/TreeNode');
const { printTree } = require('../modules/PrintUtil');
/* 初始化二叉树 */
// 初始化节点
@@ -19,7 +19,7 @@ n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
console.log("\n初始化二叉树\n");
console.log('\n初始化二叉树\n');
printTree(n1);
/* 插入与删除节点 */
@@ -27,9 +27,9 @@ const P = new TreeNode(0);
// 在 n1 -> n2 中间插入节点 P
n1.left = P;
P.left = n2;
console.log("\n插入节点 P 后\n");
console.log('\n插入节点 P 后\n');
printTree(n1);
// 删除节点 P
n1.left = n2;
console.log("\n删除节点 P 后\n");
console.log('\n删除节点 P 后\n');
printTree(n1);

View File

@@ -4,8 +4,8 @@
* Author: IsChristina (christinaxia77@foxmail.com)
*/
const { arrToTree } = require("../modules/TreeNode");
const { printTree } = require("../modules/PrintUtil");
const { arrToTree } = require('../modules/TreeNode');
const { printTree } = require('../modules/PrintUtil');
/* 层序遍历 */
function levelOrder(root) {
@@ -14,13 +14,10 @@ function levelOrder(root) {
// 初始化一个列表,用于保存遍历序列
const list = [];
while (queue.length) {
let node = queue.shift(); // 队列出队
list.push(node.val); // 保存节点值
if (node.left)
queue.push(node.left); // 子节点入队
if (node.right)
queue.push(node.right); // 右子节点入队
let node = queue.shift(); // 队列出队
list.push(node.val); // 保存节点值
if (node.left) queue.push(node.left); // 左子节点入队
if (node.right) queue.push(node.right); // 子节点入队
}
return list;
}
@@ -29,9 +26,9 @@ function levelOrder(root) {
/* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
console.log("\n初始化二叉树\n");
console.log('\n初始化二叉树\n');
printTree(root);
/* 层序遍历 */
const list = levelOrder(root);
console.log("\n层序遍历的节点打印序列 = " + list);
console.log('\n层序遍历的节点打印序列 = ' + list);

View File

@@ -4,8 +4,8 @@
* Author: IsChristina (christinaxia77@foxmail.com)
*/
const { arrToTree } = require("../modules/TreeNode");
const { printTree } = require("../modules/PrintUtil");
const { arrToTree } = require('../modules/TreeNode');
const { printTree } = require('../modules/PrintUtil');
// 初始化列表,用于存储遍历序列
const list = [];
@@ -41,20 +41,20 @@ function postOrder(root) {
/* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
console.log("\n初始化二叉树\n");
console.log('\n初始化二叉树\n');
printTree(root);
/* 前序遍历 */
list.length = 0;
preOrder(root);
console.log("\n前序遍历的节点打印序列 = " + list);
console.log('\n前序遍历的节点打印序列 = ' + list);
/* 中序遍历 */
list.length = 0;
inOrder(root);
console.log("\n中序遍历的节点打印序列 = " + list);
console.log('\n中序遍历的节点打印序列 = ' + list);
/* 后序遍历 */
list.length = 0;
postOrder(root);
console.log("\n后序遍历的节点打印序列 = " + list);
console.log('\n后序遍历的节点打印序列 = ' + list);