Fix remove() in binary search tree.

pull/518/head
krahets 2 years ago
parent ee716a2c23
commit b39e79be85

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save