Fix remove() in binary search tree.

This commit is contained in:
krahets 2023-05-26 20:34:22 +08:00
parent ee716a2c23
commit b39e79be85
9 changed files with 111 additions and 50 deletions

View file

@ -112,10 +112,15 @@ class BinarySearchTree {
// 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点 // 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点
TreeNode *child = cur->left != nullptr ? cur->left : cur->right; TreeNode *child = cur->left != nullptr ? cur->left : cur->right;
// 删除节点 cur // 删除节点 cur
if (pre->left == cur) if (cur != root) {
pre->left = child; if (pre->left == cur)
else pre->left = child;
pre->right = child; else
pre->right = child;
} else {
// 若删除节点为根节点,则重新指定根节点
root = child;
}
// 释放内存 // 释放内存
delete cur; delete cur;
} }

View file

@ -21,13 +21,15 @@ class BinarySearchTree {
/* 构建二叉搜索树 */ /* 构建二叉搜索树 */
public TreeNode? buildTree(int[] nums, int i, int j) { public TreeNode? buildTree(int[] nums, int i, int j) {
if (i > j) return null; if (i > j)
return null;
// 将数组中间节点作为根节点 // 将数组中间节点作为根节点
int mid = (i + j) / 2; int mid = (i + j) / 2;
TreeNode root = new TreeNode(nums[mid]); TreeNode root = new TreeNode(nums[mid]);
// 递归建立左子树和右子树 // 递归建立左子树和右子树
root.left = buildTree(nums, i, mid - 1); root.left = buildTree(nums, i, mid - 1);
root.right = buildTree(nums, mid + 1, j); root.right = buildTree(nums, mid + 1, j);
return root; return root;
} }
@ -37,11 +39,14 @@ class BinarySearchTree {
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur != null) { while (cur != null) {
// 目标节点在 cur 的右子树中 // 目标节点在 cur 的右子树中
if (cur.val < num) cur = cur.right; if (cur.val < num) cur =
cur.right;
// 目标节点在 cur 的左子树中 // 目标节点在 cur 的左子树中
else if (cur.val > num) cur = cur.left; else if (cur.val > num)
cur = cur.left;
// 找到目标节点,跳出循环 // 找到目标节点,跳出循环
else break; else
break;
} }
// 返回目标节点 // 返回目标节点
return cur; return cur;
@ -50,24 +55,30 @@ class BinarySearchTree {
/* 插入节点 */ /* 插入节点 */
public void insert(int num) { public void insert(int num) {
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if (root == null) return; if (root == null)
return;
TreeNode? cur = root, pre = null; TreeNode? cur = root, pre = null;
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur != null) { while (cur != null) {
// 找到重复节点,直接返回 // 找到重复节点,直接返回
if (cur.val == num) return; if (cur.val == num)
return;
pre = cur; pre = cur;
// 插入位置在 cur 的右子树中 // 插入位置在 cur 的右子树中
if (cur.val < num) cur = cur.right; if (cur.val < num)
cur = cur.right;
// 插入位置在 cur 的左子树中 // 插入位置在 cur 的左子树中
else cur = cur.left; else
cur = cur.left;
} }
// 插入节点 // 插入节点
TreeNode node = new TreeNode(num); TreeNode node = new TreeNode(num);
if (pre != null) { if (pre != null) {
if (pre.val < num) pre.right = node; if (pre.val < num)
else pre.left = node; pre.right = node;
else
pre.left = node;
} }
} }
@ -75,29 +86,38 @@ class BinarySearchTree {
/* 删除节点 */ /* 删除节点 */
public void remove(int num) { public void remove(int num) {
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if (root == null) return; if (root == null)
return;
TreeNode? cur = root, pre = null; TreeNode? cur = root, pre = null;
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur != null) { while (cur != null) {
// 找到待删除节点,跳出循环 // 找到待删除节点,跳出循环
if (cur.val == num) break; if (cur.val == num)
break;
pre = cur; pre = cur;
// 待删除节点在 cur 的右子树中 // 待删除节点在 cur 的右子树中
if (cur.val < num) cur = cur.right; if (cur.val < num)
cur = cur.right;
// 待删除节点在 cur 的左子树中 // 待删除节点在 cur 的左子树中
else cur = cur.left; else
cur = cur.left;
} }
// 若无待删除节点,则直接返回 // 若无待删除节点,则直接返回
if (cur == null || pre == null) return; if (cur == null || pre == null)
return;
// 子节点数量 = 0 or 1 // 子节点数量 = 0 or 1
if (cur.left == null || cur.right == null) { if (cur.left == null || cur.right == null) {
// 当子节点数量 = 0 / 1 时, child = null / 该子节点 // 当子节点数量 = 0 / 1 时, child = null / 该子节点
TreeNode? child = cur.left != null ? cur.left : cur.right; TreeNode? child = cur.left != null ? cur.left : cur.right;
// 删除节点 cur // 删除节点 cur
if (pre.left == cur) { if (cur != root) {
pre.left = child; if (pre.left == cur)
pre.left = child;
else
pre.right = child;
} else { } else {
pre.right = child; // 若删除节点为根节点,则重新指定根节点
root = child;
} }
} }
// 子节点数量 = 2 // 子节点数量 = 2

View file

@ -104,10 +104,16 @@ void remove(int num) {
// = 0 / 1 child = null / // = 0 / 1 child = null /
TreeNode? child = cur.left ?? cur.right; TreeNode? child = cur.left ?? cur.right;
// cur // cur
if (pre!.left == cur) if (cur != root) {
pre.left = child; if (pre!.left == cur)
else pre.left = child;
pre.right = child; else
pre.right = child;
} else {
//
root = child;
}
} else { } else {
// = 2 // = 2
// cur // cur

View file

@ -128,12 +128,18 @@ func (bst *binarySearchTree) remove(num int) {
} else { } else {
child = cur.Right child = cur.Right
} }
// 将子节点替换为待删除节点 // 删除节点 cur
if pre.Left == cur { if cur != bst.root {
pre.Left = child if pre.Left == cur {
pre.Left = child
} else {
pre.Right = child
}
} else { } else {
pre.Right = child // 若删除节点为根节点,则重新指定根节点
bst.root = child
} }
// 子节点数为 2 // 子节点数为 2
} else { } else {
// 获取中序遍历中待删除节点 cur 的下一个节点 // 获取中序遍历中待删除节点 cur 的下一个节点

View file

@ -109,10 +109,15 @@ class BinarySearchTree {
// 当子节点数量 = 0 / 1 child = null / 该子节点 // 当子节点数量 = 0 / 1 child = null / 该子节点
TreeNode child = cur.left != null ? cur.left : cur.right; TreeNode child = cur.left != null ? cur.left : cur.right;
// 删除节点 cur // 删除节点 cur
if (pre.left == cur) if (cur != root) {
pre.left = child; if (pre.left == cur)
else pre.left = child;
pre.right = child; else
pre.right = child;
} else {
// 若删除节点为根节点则重新指定根节点
root = child;
}
} }
// 子节点数量 = 2 // 子节点数量 = 2
else { else {

View file

@ -95,8 +95,13 @@ function remove(num) {
// 当子节点数量 = 0 / 1 时, child = null / 该子节点 // 当子节点数量 = 0 / 1 时, child = null / 该子节点
let child = cur.left !== null ? cur.left : cur.right; let child = cur.left !== null ? cur.left : cur.right;
// 删除节点 cur // 删除节点 cur
if (pre.left === cur) pre.left = child; if (cur != root) {
else pre.right = child; if (pre.left === cur) pre.left = child;
else pre.right = child;
} else {
// 若删除节点为根节点,则重新指定根节点
root = child;
}
} }
// 子节点数量 = 2 // 子节点数量 = 2
else { else {

View file

@ -43,7 +43,7 @@ class BinarySearchTree:
def search(self, num: int) -> TreeNode | None: def search(self, num: int) -> TreeNode | None:
"""查找节点""" """查找节点"""
cur: TreeNode | None = self.__root cur: TreeNode | None = self.root
# 循环查找,越过叶节点后跳出 # 循环查找,越过叶节点后跳出
while cur is not None: while cur is not None:
# 目标节点在 cur 的右子树中 # 目标节点在 cur 的右子树中
@ -60,11 +60,11 @@ class BinarySearchTree:
def insert(self, num: int) -> None: def insert(self, num: int) -> None:
"""插入节点""" """插入节点"""
# 若树为空,直接提前返回 # 若树为空,直接提前返回
if self.__root is None: if self.root is None:
return return
# 循环查找,越过叶节点后跳出 # 循环查找,越过叶节点后跳出
cur, pre = self.__root, None cur, pre = self.root, None
while cur is not None: while cur is not None:
# 找到重复节点,直接返回 # 找到重复节点,直接返回
if cur.val == num: if cur.val == num:
@ -87,11 +87,11 @@ class BinarySearchTree:
def remove(self, num: int) -> None: def remove(self, num: int) -> None:
"""删除节点""" """删除节点"""
# 若树为空,直接提前返回 # 若树为空,直接提前返回
if self.__root is None: if self.root is None:
return return
# 循环查找,越过叶节点后跳出 # 循环查找,越过叶节点后跳出
cur, pre = self.__root, None cur, pre = self.root, None
while cur is not None: while cur is not None:
# 找到待删除节点,跳出循环 # 找到待删除节点,跳出循环
if cur.val == num: if cur.val == num:
@ -112,10 +112,14 @@ class BinarySearchTree:
# 当子节点数量 = 0 / 1 时, child = null / 该子节点 # 当子节点数量 = 0 / 1 时, child = null / 该子节点
child = cur.left or cur.right child = cur.left or cur.right
# 删除节点 cur # 删除节点 cur
if pre.left == cur: if cur != self.root:
pre.left = child if pre.left == cur:
pre.left = child
else:
pre.right = child
else: else:
pre.right = child # 若删除节点为根节点,则重新指定根节点
self.__root = cur
# 子节点数量 = 2 # 子节点数量 = 2
else: else:
# 获取中序遍历中 cur 的下一个节点 # 获取中序遍历中 cur 的下一个节点

View file

@ -123,10 +123,15 @@ class BinarySearchTree {
// = 0 / 1 child = null / // = 0 / 1 child = null /
let child = cur?.left != nil ? cur?.left : cur?.right let child = cur?.left != nil ? cur?.left : cur?.right
// cur // cur
if pre?.left === cur { if cur != root {
pre?.left = child if pre?.left === cur {
pre?.left = child
} else {
pre?.right = child
}
} else { } else {
pre?.right = child //
root = cur;
} }
} }
// = 2 // = 2

View file

@ -110,10 +110,15 @@ function remove(num: number): void {
// 当子节点数量 = 0 / 1 时, child = null / 该子节点 // 当子节点数量 = 0 / 1 时, child = null / 该子节点
let child = cur.left !== null ? cur.left : cur.right; let child = cur.left !== null ? cur.left : cur.right;
// 删除节点 cur // 删除节点 cur
if (pre!.left === cur) { if (cur != root) {
pre!.left = child; if (pre!.left === cur) {
pre!.left = child;
} else {
pre!.right = child;
}
} else { } else {
pre!.right = child; // 若删除节点为根节点,则重新指定根节点
root = child;
} }
} }
// 子节点数量 = 2 // 子节点数量 = 2