mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 03:30:30 +08:00
Fix the return type of binary search tree and avl tree
This commit is contained in:
@@ -94,9 +94,8 @@ class AVLTree {
|
||||
}
|
||||
|
||||
/* 插入节点 */
|
||||
insert(val: number): TreeNode {
|
||||
insert(val: number): void {
|
||||
this.root = this.insertHelper(this.root, val);
|
||||
return this.root;
|
||||
}
|
||||
|
||||
/* 递归插入节点(辅助方法) */
|
||||
@@ -118,9 +117,8 @@ class AVLTree {
|
||||
}
|
||||
|
||||
/* 删除节点 */
|
||||
remove(val: number): TreeNode {
|
||||
remove(val: number): void {
|
||||
this.root = this.removeHelper(this.root, val);
|
||||
return this.root;
|
||||
}
|
||||
|
||||
/* 递归删除节点(辅助方法) */
|
||||
@@ -143,7 +141,10 @@ class AVLTree {
|
||||
}
|
||||
} else {
|
||||
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
|
||||
const temp = this.getInOrderNext(node.right);
|
||||
let temp = node.right;
|
||||
while (temp.left !== null) {
|
||||
temp = temp.left;
|
||||
}
|
||||
node.right = this.removeHelper(node.right, temp.val);
|
||||
node.val = temp.val;
|
||||
}
|
||||
@@ -155,16 +156,6 @@ class AVLTree {
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
||||
private getInOrderNext(node: TreeNode): TreeNode {
|
||||
if (node === null) return node;
|
||||
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
||||
while (node.left !== null) {
|
||||
node = node.left;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 查找节点 */
|
||||
search(val: number): TreeNode {
|
||||
let cur = this.root;
|
||||
|
||||
@@ -52,17 +52,17 @@ function search(num: number): TreeNode | null {
|
||||
}
|
||||
|
||||
/* 插入节点 */
|
||||
function insert(num: number): TreeNode | null {
|
||||
function insert(num: number): void {
|
||||
// 若树为空,直接提前返回
|
||||
if (root === null) {
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
let cur = root,
|
||||
pre: TreeNode | null = null;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur !== null) {
|
||||
if (cur.val === num) {
|
||||
return null; // 找到重复节点,直接返回
|
||||
return; // 找到重复节点,直接返回
|
||||
}
|
||||
pre = cur;
|
||||
if (cur.val < num) {
|
||||
@@ -78,14 +78,13 @@ function insert(num: number): TreeNode | null {
|
||||
} else {
|
||||
pre!.left = node;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 删除节点 */
|
||||
function remove(num: number): TreeNode | null {
|
||||
function remove(num: number): void {
|
||||
// 若树为空,直接提前返回
|
||||
if (root === null) {
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
let cur = root,
|
||||
pre: TreeNode | null = null;
|
||||
@@ -104,7 +103,7 @@ function remove(num: number): TreeNode | null {
|
||||
}
|
||||
// 若无待删除节点,则直接返回
|
||||
if (cur === null) {
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
// 子节点数量 = 0 or 1
|
||||
if (cur.left === null || cur.right === null) {
|
||||
@@ -120,26 +119,15 @@ function remove(num: number): TreeNode | null {
|
||||
// 子节点数量 = 2
|
||||
else {
|
||||
// 获取中序遍历中 cur 的下一个节点
|
||||
let next = getInOrderNext(cur.right);
|
||||
let tmp = next!.val;
|
||||
// 递归删除节点 nex
|
||||
remove(next!.val);
|
||||
// 将 nex 的值复制给 cur
|
||||
cur.val = tmp;
|
||||
let tmp = cur.right;
|
||||
while (tmp.left !== null) {
|
||||
tmp = tmp.left;
|
||||
}
|
||||
// 递归删除节点 tmp
|
||||
remove(tmp!.val);
|
||||
// 用 tmp 覆盖 cur
|
||||
cur.val = tmp.val;
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
|
||||
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
||||
function getInOrderNext(root: TreeNode | null): TreeNode | null {
|
||||
if (root === null) {
|
||||
return null;
|
||||
}
|
||||
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
||||
while (root.left !== null) {
|
||||
root = root.left;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
@@ -154,7 +142,7 @@ let node = search(7);
|
||||
console.log('\n查找到的节点对象为 ' + node + ',节点值 = ' + node!.val);
|
||||
|
||||
/* 插入节点 */
|
||||
node = insert(16);
|
||||
insert(16);
|
||||
console.log('\n插入节点 16 后,二叉树为\n');
|
||||
printTree(getRoot());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user