mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Add TypeScript code and docs to AVL tree and the coding style for Typescript and JavaScript (#342)
* Add TypeScript code and docs to AVL tree and update JavaScript style * Update the coding style for Typescript and JavaScript
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
/**
|
||||
* File: avl_tree.cpp
|
||||
* File: avl_tree.js
|
||||
* Created Time: 2023-02-05
|
||||
* Author: what-is-me (whatisme@outlook.jp)
|
||||
*/
|
||||
|
||||
let { TreeNode } = require("../include/TreeNode");
|
||||
let { printTree } = require("../include/PrintUtil");
|
||||
const { TreeNode } = require("../include/TreeNode");
|
||||
const { printTree } = require("../include/PrintUtil");
|
||||
|
||||
/* AVL 树*/
|
||||
class AVLTree {
|
||||
@ -36,8 +36,8 @@ class AVLTree {
|
||||
|
||||
/* 右旋操作 */
|
||||
rightRotate(node) {
|
||||
let child = node.left;
|
||||
let grandChild = child.right;
|
||||
const child = node.left;
|
||||
const grandChild = child.right;
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
child.right = node;
|
||||
node.left = grandChild;
|
||||
@ -50,8 +50,8 @@ class AVLTree {
|
||||
|
||||
/* 左旋操作 */
|
||||
leftRotate(node) {
|
||||
let child = node.right;
|
||||
let grandChild = child.left;
|
||||
const child = node.right;
|
||||
const grandChild = child.left;
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
child.left = node;
|
||||
node.right = grandChild;
|
||||
@ -65,7 +65,7 @@ class AVLTree {
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
rotate(node) {
|
||||
// 获取结点 node 的平衡因子
|
||||
let balanceFactor = this.balanceFactor(node);
|
||||
const balanceFactor = this.balanceFactor(node);
|
||||
// 左偏树
|
||||
if (balanceFactor > 1) {
|
||||
if (this.balanceFactor(node.left) >= 0) {
|
||||
@ -126,14 +126,14 @@ class AVLTree {
|
||||
else if (val > node.val) node.right = this.removeHelper(node.right, val);
|
||||
else {
|
||||
if (node.left === null || node.right === null) {
|
||||
let child = node.left !== null ? node.left : node.right;
|
||||
const child = node.left !== null ? node.left : node.right;
|
||||
// 子结点数量 = 0 ,直接删除 node 并返回
|
||||
if (child === null) return null;
|
||||
// 子结点数量 = 1 ,直接删除 node
|
||||
else node = child;
|
||||
} else {
|
||||
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
||||
let temp = this.getInOrderNext(node.right);
|
||||
const temp = this.getInOrderNext(node.right);
|
||||
node.right = this.removeHelper(node.right, temp.val);
|
||||
node.val = temp.val;
|
||||
}
|
||||
@ -184,8 +184,9 @@ function testRemove(tree, val) {
|
||||
printTree(tree.root);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化空 AVL 树 */
|
||||
let avlTree = new AVLTree();
|
||||
const avlTree = new AVLTree();
|
||||
/* 插入结点 */
|
||||
// 请关注插入结点后,AVL 树是如何保持平衡的
|
||||
testInsert(avlTree, 1);
|
||||
@ -209,5 +210,5 @@ testRemove(avlTree, 5); // 删除度为 1 的结点
|
||||
testRemove(avlTree, 4); // 删除度为 2 的结点
|
||||
|
||||
/* 查询结点 */
|
||||
let node = avlTree.search(7);
|
||||
console.log("\n查找到的结点对象为 " + node + ",结点值 = " + node.val);
|
||||
const node = avlTree.search(7);
|
||||
console.log("\n查找到的结点对象为", node, ",结点值 = " + node.val);
|
||||
|
||||
Reference in New Issue
Block a user