2023-02-08 15:20:18 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
# 7.3. 二叉搜索树
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
「二叉搜索树 Binary Search Tree」满足以下条件:
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
1. 对于根节点,左子树中所有节点的值 $<$ 根节点的值 $<$ 右子树中所有节点的值;
|
2023-04-10 23:59:31 +08:00
|
|
|
|
2. 任意节点的左、右子树也是二叉搜索树,即同样满足条件 `1.` ;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![二叉搜索树](binary_search_tree.assets/binary_search_tree.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 二叉搜索树 </p>
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 7.3.1. 二叉搜索树的操作
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
### 查找节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
给定目标节点值 `num` ,可以根据二叉搜索树的性质来查找。我们声明一个节点 `cur` ,从二叉树的根节点 `root` 出发,循环比较节点值 `cur.val` 和 `num` 之间的大小关系
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
- 若 `cur.val < num` ,说明目标节点在 `cur` 的右子树中,因此执行 `cur = cur.right` ;
|
|
|
|
|
- 若 `cur.val > num` ,说明目标节点在 `cur` 的左子树中,因此执行 `cur = cur.left` ;
|
2023-04-10 23:59:31 +08:00
|
|
|
|
- 若 `cur.val = num` ,说明找到目标节点,跳出循环并返回该节点;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<1>"
|
2023-04-10 23:59:31 +08:00
|
|
|
|
![bst_search_step1](binary_search_tree.assets/bst_search_step1.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<2>"
|
2023-02-26 01:51:44 +08:00
|
|
|
|
![bst_search_step2](binary_search_tree.assets/bst_search_step2.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<3>"
|
2023-02-26 01:51:44 +08:00
|
|
|
|
![bst_search_step3](binary_search_tree.assets/bst_search_step3.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<4>"
|
2023-02-26 01:51:44 +08:00
|
|
|
|
![bst_search_step4](binary_search_tree.assets/bst_search_step4.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
二叉搜索树的查找操作与二分查找算法的工作原理一致,都是每轮排除一半情况。循环次数最多为二叉树的高度,当二叉树平衡时,使用 $O(\log n)$ 时间。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_search_tree.java"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 查找节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode search(int num) {
|
|
|
|
|
TreeNode cur = root;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur != null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的右子树中
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (cur.val < num)
|
|
|
|
|
cur = cur.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的左子树中
|
2023-04-14 04:01:38 +08:00
|
|
|
|
else if (cur.val > num)
|
|
|
|
|
cur = cur.left;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到目标节点,跳出循环
|
2023-04-14 04:01:38 +08:00
|
|
|
|
else
|
|
|
|
|
break;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 返回目标节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_search_tree.cpp"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 查找节点 */
|
2023-04-14 04:01:38 +08:00
|
|
|
|
TreeNode *search(int num) {
|
|
|
|
|
TreeNode *cur = root;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur != nullptr) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的右子树中
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (cur->val < num)
|
|
|
|
|
cur = cur->right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的左子树中
|
2023-04-14 04:01:38 +08:00
|
|
|
|
else if (cur->val > num)
|
|
|
|
|
cur = cur->left;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到目标节点,跳出循环
|
2023-04-14 04:01:38 +08:00
|
|
|
|
else
|
|
|
|
|
break;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 返回目标节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_search_tree.py"
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def search(self, num: int) -> TreeNode | None:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""查找节点"""
|
2023-03-23 18:56:56 +08:00
|
|
|
|
cur: TreeNode | None = self.__root
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while cur is not None:
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 目标节点在 cur 的右子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur.val < num:
|
|
|
|
|
cur = cur.right
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 目标节点在 cur 的左子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
elif cur.val > num:
|
|
|
|
|
cur = cur.left
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 找到目标节点,跳出循环
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else:
|
|
|
|
|
break
|
|
|
|
|
return cur
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_search_tree.go"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 查找节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (bst *binarySearchTree) search(num int) *TreeNode {
|
|
|
|
|
node := bst.root
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for node != nil {
|
|
|
|
|
if node.Val < num {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的右子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
node = node.Right
|
|
|
|
|
} else if node.Val > num {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的左子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
node = node.Left
|
|
|
|
|
} else {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到目标节点,跳出循环
|
2023-02-08 15:20:18 +08:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 返回目标节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="binary_search_tree.js"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 查找节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function search(num) {
|
|
|
|
|
let cur = root;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur !== null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的右子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.val < num) cur = cur.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的左子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else if (cur.val > num) cur = cur.left;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到目标节点,跳出循环
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else break;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 返回目标节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_search_tree.ts"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 查找节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function search(num: number): TreeNode | null {
|
|
|
|
|
let cur = root;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur !== null) {
|
|
|
|
|
if (cur.val < num) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
cur = cur.right; // 目标节点在 cur 的右子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
} else if (cur.val > num) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
cur = cur.left; // 目标节点在 cur 的左子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
} else {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
break; // 找到目标节点,跳出循环
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 返回目标节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_search_tree.c"
|
2023-02-11 18:21:44 +08:00
|
|
|
|
[class]{binarySearchTree}-[func]{search}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_search_tree.cs"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 查找节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode? search(int num)
|
|
|
|
|
{
|
|
|
|
|
TreeNode? cur = root;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur != null)
|
|
|
|
|
{
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的右子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.val < num) cur = cur.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的左子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else if (cur.val > num) cur = cur.left;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到目标节点,跳出循环
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else break;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 返回目标节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="binary_search_tree.swift"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 查找节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func search(num: Int) -> TreeNode? {
|
|
|
|
|
var cur = root
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while cur != nil {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的右子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur!.val < num {
|
|
|
|
|
cur = cur?.right
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的左子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else if cur!.val > num {
|
|
|
|
|
cur = cur?.left
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到目标节点,跳出循环
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 返回目标节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return cur
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="binary_search_tree.zig"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 查找节点
|
2023-02-09 22:55:29 +08:00
|
|
|
|
fn search(self: *Self, num: T) ?*inc.TreeNode(T) {
|
|
|
|
|
var cur = self.root;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-09 22:55:29 +08:00
|
|
|
|
while (cur != null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的右子树中
|
2023-02-09 22:55:29 +08:00
|
|
|
|
if (cur.?.val < num) {
|
|
|
|
|
cur = cur.?.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 目标节点在 cur 的左子树中
|
2023-02-09 22:55:29 +08:00
|
|
|
|
} else if (cur.?.val > num) {
|
|
|
|
|
cur = cur.?.left;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到目标节点,跳出循环
|
2023-02-09 22:55:29 +08:00
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 返回目标节点
|
2023-02-09 22:55:29 +08:00
|
|
|
|
return cur;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
### 插入节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
给定一个待插入元素 `num` ,为了保持二叉搜索树“左子树 < 根节点 < 右子树”的性质,插入操作分为两步:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
1. **查找插入位置**:与查找操作相似,从根节点出发,根据当前节点值和 `num` 的大小关系循环向下搜索,直到越过叶节点(遍历至 $\text{null}$ )时跳出循环;
|
|
|
|
|
2. **在该位置插入节点**:初始化节点 `num` ,将该节点置于 $\text{null}$ 的位置;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
二叉搜索树不允许存在重复节点,否则将违反其定义。因此,若待插入节点在树中已存在,则不执行插入,直接返回。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
![在二叉搜索树中插入节点](binary_search_tree.assets/bst_insert.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
<p align="center"> Fig. 在二叉搜索树中插入节点 </p>
|
2023-02-26 19:53:26 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_search_tree.java"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 插入节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode insert(int num) {
|
|
|
|
|
// 若树为空,直接提前返回
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (root == null)
|
|
|
|
|
return null;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode cur = root, pre = null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur != null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到重复节点,直接返回
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (cur.val == num)
|
|
|
|
|
return null;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
pre = cur;
|
|
|
|
|
// 插入位置在 cur 的右子树中
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (cur.val < num)
|
|
|
|
|
cur = cur.right;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 插入位置在 cur 的左子树中
|
2023-04-14 04:01:38 +08:00
|
|
|
|
else
|
|
|
|
|
cur = cur.left;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 插入节点 val
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode node = new TreeNode(num);
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (pre.val < num)
|
|
|
|
|
pre.right = node;
|
|
|
|
|
else
|
|
|
|
|
pre.left = node;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_search_tree.cpp"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 插入节点 */
|
2023-04-14 04:01:38 +08:00
|
|
|
|
TreeNode *insert(int num) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 若树为空,直接提前返回
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (root == nullptr)
|
|
|
|
|
return nullptr;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode *cur = root, *pre = nullptr;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur != nullptr) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到重复节点,直接返回
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (cur->val == num)
|
|
|
|
|
return nullptr;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
pre = cur;
|
|
|
|
|
// 插入位置在 cur 的右子树中
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (cur->val < num)
|
|
|
|
|
cur = cur->right;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 插入位置在 cur 的左子树中
|
2023-04-14 04:01:38 +08:00
|
|
|
|
else
|
|
|
|
|
cur = cur->left;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 插入节点 val
|
2023-04-14 04:01:38 +08:00
|
|
|
|
TreeNode *node = new TreeNode(num);
|
|
|
|
|
if (pre->val < num)
|
|
|
|
|
pre->right = node;
|
|
|
|
|
else
|
|
|
|
|
pre->left = node;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_search_tree.py"
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def insert(self, num: int) -> TreeNode | None:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""插入节点"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 若树为空,直接提前返回
|
2023-03-12 18:46:03 +08:00
|
|
|
|
if self.__root is None:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return None
|
2023-04-09 05:12:22 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 循环查找,越过叶节点后跳出
|
2023-03-12 18:46:03 +08:00
|
|
|
|
cur, pre = self.__root, None
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while cur is not None:
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 找到重复节点,直接返回
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur.val == num:
|
|
|
|
|
return None
|
|
|
|
|
pre = cur
|
|
|
|
|
# 插入位置在 cur 的右子树中
|
|
|
|
|
if cur.val < num:
|
|
|
|
|
cur = cur.right
|
|
|
|
|
# 插入位置在 cur 的左子树中
|
|
|
|
|
else:
|
|
|
|
|
cur = cur.left
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 插入节点 val
|
2023-02-08 15:20:18 +08:00
|
|
|
|
node = TreeNode(num)
|
|
|
|
|
if pre.val < num:
|
|
|
|
|
pre.right = node
|
|
|
|
|
else:
|
|
|
|
|
pre.left = node
|
|
|
|
|
return node
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_search_tree.go"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 插入节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (bst *binarySearchTree) insert(num int) *TreeNode {
|
|
|
|
|
cur := bst.root
|
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
|
if cur == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待插入节点之前的节点位置
|
2023-02-08 15:20:18 +08:00
|
|
|
|
var pre *TreeNode = nil
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for cur != nil {
|
|
|
|
|
if cur.Val == num {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
pre = cur
|
|
|
|
|
if cur.Val < num {
|
|
|
|
|
cur = cur.Right
|
|
|
|
|
} else {
|
|
|
|
|
cur = cur.Left
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 插入节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
node := NewTreeNode(num)
|
|
|
|
|
if pre.Val < num {
|
|
|
|
|
pre.Right = node
|
|
|
|
|
} else {
|
|
|
|
|
pre.Left = node
|
|
|
|
|
}
|
|
|
|
|
return cur
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="binary_search_tree.js"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 插入节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function insert(num) {
|
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
|
if (root === null) return null;
|
|
|
|
|
let cur = root, pre = null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur !== null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到重复节点,直接返回
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.val === num) return null;
|
|
|
|
|
pre = cur;
|
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
|
if (cur.val < num) cur = cur.right;
|
|
|
|
|
// 插入位置在 cur 的左子树中
|
|
|
|
|
else cur = cur.left;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 插入节点 val
|
2023-03-03 02:46:12 +08:00
|
|
|
|
let node = new TreeNode(num);
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (pre.val < num) pre.right = node;
|
|
|
|
|
else pre.left = node;
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_search_tree.ts"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 插入节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function insert(num: number): TreeNode | null {
|
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
|
if (root === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
let cur = root,
|
|
|
|
|
pre: TreeNode | null = null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur !== null) {
|
|
|
|
|
if (cur.val === num) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
return null; // 找到重复节点,直接返回
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
pre = cur;
|
|
|
|
|
if (cur.val < num) {
|
|
|
|
|
cur = cur.right as TreeNode; // 插入位置在 cur 的右子树中
|
|
|
|
|
} else {
|
|
|
|
|
cur = cur.left as TreeNode; // 插入位置在 cur 的左子树中
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 插入节点 val
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let node = new TreeNode(num);
|
|
|
|
|
if (pre!.val < num) {
|
|
|
|
|
pre!.right = node;
|
|
|
|
|
} else {
|
|
|
|
|
pre!.left = node;
|
|
|
|
|
}
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_search_tree.c"
|
2023-02-11 18:21:44 +08:00
|
|
|
|
[class]{binarySearchTree}-[func]{insert}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_search_tree.cs"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 插入节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode? insert(int num)
|
|
|
|
|
{
|
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
|
if (root == null) return null;
|
|
|
|
|
TreeNode? cur = root, pre = null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur != null)
|
|
|
|
|
{
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到重复节点,直接返回
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.val == num) return null;
|
|
|
|
|
pre = cur;
|
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
|
if (cur.val < num) cur = cur.right;
|
|
|
|
|
// 插入位置在 cur 的左子树中
|
|
|
|
|
else cur = cur.left;
|
|
|
|
|
}
|
2023-02-08 22:16:25 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 插入节点 val
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode node = new TreeNode(num);
|
|
|
|
|
if (pre != null)
|
|
|
|
|
{
|
|
|
|
|
if (pre.val < num) pre.right = node;
|
|
|
|
|
else pre.left = node;
|
|
|
|
|
}
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="binary_search_tree.swift"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 插入节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func insert(num: Int) -> TreeNode? {
|
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
|
if root == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var cur = root
|
|
|
|
|
var pre: TreeNode?
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while cur != nil {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到重复节点,直接返回
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur!.val == num {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
pre = cur
|
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
|
if cur!.val < num {
|
|
|
|
|
cur = cur?.right
|
|
|
|
|
}
|
|
|
|
|
// 插入位置在 cur 的左子树中
|
|
|
|
|
else {
|
|
|
|
|
cur = cur?.left
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 插入节点 val
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let node = TreeNode(x: num)
|
|
|
|
|
if pre!.val < num {
|
|
|
|
|
pre?.right = node
|
|
|
|
|
} else {
|
|
|
|
|
pre?.left = node
|
|
|
|
|
}
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="binary_search_tree.zig"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 插入节点
|
2023-02-09 22:55:29 +08:00
|
|
|
|
fn insert(self: *Self, num: T) !?*inc.TreeNode(T) {
|
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
|
if (self.root == null) return null;
|
|
|
|
|
var cur = self.root;
|
|
|
|
|
var pre: ?*inc.TreeNode(T) = null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-09 22:55:29 +08:00
|
|
|
|
while (cur != null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到重复节点,直接返回
|
2023-02-09 22:55:29 +08:00
|
|
|
|
if (cur.?.val == num) return null;
|
|
|
|
|
pre = cur;
|
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
|
if (cur.?.val < num) {
|
|
|
|
|
cur = cur.?.right;
|
|
|
|
|
// 插入位置在 cur 的左子树中
|
|
|
|
|
} else {
|
|
|
|
|
cur = cur.?.left;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 插入节点 val
|
2023-02-09 22:55:29 +08:00
|
|
|
|
var node = try self.mem_allocator.create(inc.TreeNode(T));
|
|
|
|
|
node.init(num);
|
|
|
|
|
if (pre.?.val < num) {
|
|
|
|
|
pre.?.right = node;
|
|
|
|
|
} else {
|
|
|
|
|
pre.?.left = node;
|
|
|
|
|
}
|
|
|
|
|
return node;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
为了插入节点,我们需要利用辅助节点 `pre` 保存上一轮循环的节点,这样在遍历至 $\text{null}$ 时,我们可以获取到其父节点,从而完成节点插入操作。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
与查找节点相同,插入节点使用 $O(\log n)$ 时间。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
### 删除节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
与插入节点类似,我们需要在删除操作后维持二叉搜索树的“左子树 < 根节点 < 右子树”的性质。首先,我们需要在二叉树中执行查找操作,获取待删除节点。接下来,根据待删除节点的子节点数量,删除操作需分为三种情况:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
当待删除节点的子节点数量 $= 0$ 时,表示待删除节点是叶节点,可以直接删除。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
![在二叉搜索树中删除节点(度为 0)](binary_search_tree.assets/bst_remove_case1.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
<p align="center"> Fig. 在二叉搜索树中删除节点(度为 0) </p>
|
2023-02-26 19:53:26 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
当待删除节点的子节点数量 $= 1$ 时,将待删除节点替换为其子节点即可。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
![在二叉搜索树中删除节点(度为 1)](binary_search_tree.assets/bst_remove_case2.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
<p align="center"> Fig. 在二叉搜索树中删除节点(度为 1) </p>
|
2023-02-26 19:53:26 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
当待删除节点的子节点数量 $= 2$ 时,删除操作分为三步:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
1. 找到待删除节点在“中序遍历序列”中的下一个节点,记为 nex;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
2. 在树中递归删除节点 `nex` ;
|
|
|
|
|
3. 使用 `nex` 替换待删除节点;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<1>"
|
2023-04-10 23:59:31 +08:00
|
|
|
|
![bst_remove_case3_step1](binary_search_tree.assets/bst_remove_case3_step1.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<2>"
|
2023-02-26 01:51:44 +08:00
|
|
|
|
![bst_remove_case3_step2](binary_search_tree.assets/bst_remove_case3_step2.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<3>"
|
2023-02-26 01:51:44 +08:00
|
|
|
|
![bst_remove_case3_step3](binary_search_tree.assets/bst_remove_case3_step3.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<4>"
|
2023-02-26 01:51:44 +08:00
|
|
|
|
![bst_remove_case3_step4](binary_search_tree.assets/bst_remove_case3_step4.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
删除节点操作同样使用 $O(\log n)$ 时间,其中查找待删除节点需要 $O(\log n)$ 时间,获取中序遍历后继节点需要 $O(\log n)$ 时间。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="binary_search_tree.java"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 删除节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode remove(int num) {
|
|
|
|
|
// 若树为空,直接提前返回
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (root == null)
|
|
|
|
|
return null;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode cur = root, pre = null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur != null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到待删除节点,跳出循环
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (cur.val == num)
|
|
|
|
|
break;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
pre = cur;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在 cur 的右子树中
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (cur.val < num)
|
|
|
|
|
cur = cur.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在 cur 的左子树中
|
2023-04-14 04:01:38 +08:00
|
|
|
|
else
|
|
|
|
|
cur = cur.left;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若无待删除节点,则直接返回
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (cur == null)
|
|
|
|
|
return null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 0 or 1
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.left == null || cur.right == null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode child = cur.left != null ? cur.left : cur.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点 cur
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (pre.left == cur)
|
|
|
|
|
pre.left = child;
|
|
|
|
|
else
|
|
|
|
|
pre.right = child;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 2
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode nex = getInOrderNext(cur.right);
|
|
|
|
|
int tmp = nex.val;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 递归删除节点 nex
|
2023-02-08 15:20:18 +08:00
|
|
|
|
remove(nex.val);
|
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
|
cur.val = tmp;
|
|
|
|
|
}
|
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode getInOrderNext(TreeNode root) {
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (root == null)
|
|
|
|
|
return root;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (root.left != null) {
|
|
|
|
|
root = root.left;
|
|
|
|
|
}
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_search_tree.cpp"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 删除节点 */
|
2023-04-14 04:01:38 +08:00
|
|
|
|
TreeNode *remove(int num) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 若树为空,直接提前返回
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (root == nullptr)
|
|
|
|
|
return nullptr;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode *cur = root, *pre = nullptr;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur != nullptr) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到待删除节点,跳出循环
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (cur->val == num)
|
|
|
|
|
break;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
pre = cur;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在 cur 的右子树中
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (cur->val < num)
|
|
|
|
|
cur = cur->right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在 cur 的左子树中
|
2023-04-14 04:01:38 +08:00
|
|
|
|
else
|
|
|
|
|
cur = cur->left;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若无待删除节点,则直接返回
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (cur == nullptr)
|
|
|
|
|
return nullptr;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 0 or 1
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur->left == nullptr || cur->right == nullptr) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点
|
2023-04-14 04:01:38 +08:00
|
|
|
|
TreeNode *child = cur->left != nullptr ? cur->left : cur->right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点 cur
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (pre->left == cur)
|
|
|
|
|
pre->left = child;
|
|
|
|
|
else
|
|
|
|
|
pre->right = child;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 释放内存
|
|
|
|
|
delete cur;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 2
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
2023-04-14 04:01:38 +08:00
|
|
|
|
TreeNode *nex = getInOrderNext(cur->right);
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int tmp = nex->val;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 递归删除节点 nex
|
2023-02-08 15:20:18 +08:00
|
|
|
|
remove(nex->val);
|
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
|
cur->val = tmp;
|
|
|
|
|
}
|
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
2023-04-14 04:01:38 +08:00
|
|
|
|
TreeNode *getInOrderNext(TreeNode *root) {
|
|
|
|
|
if (root == nullptr)
|
|
|
|
|
return root;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (root->left != nullptr) {
|
|
|
|
|
root = root->left;
|
|
|
|
|
}
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="binary_search_tree.py"
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def remove(self, num: int) -> TreeNode | None:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""删除节点"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 若树为空,直接提前返回
|
2023-03-12 18:46:03 +08:00
|
|
|
|
if self.__root is None:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return None
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 循环查找,越过叶节点后跳出
|
2023-03-12 18:46:03 +08:00
|
|
|
|
cur, pre = self.__root, None
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while cur is not None:
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 找到待删除节点,跳出循环
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur.val == num:
|
|
|
|
|
break
|
|
|
|
|
pre = cur
|
2023-04-09 04:34:58 +08:00
|
|
|
|
if cur.val < num: # 待删除节点在 cur 的右子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
cur = cur.right
|
2023-04-09 04:34:58 +08:00
|
|
|
|
else: # 待删除节点在 cur 的左子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
cur = cur.left
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 若无待删除节点,则直接返回
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur is None:
|
|
|
|
|
return None
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 子节点数量 = 0 or 1
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur.left is None or cur.right is None:
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
child = cur.left or cur.right
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 删除节点 cur
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if pre.left == cur:
|
|
|
|
|
pre.left = child
|
|
|
|
|
else:
|
|
|
|
|
pre.right = child
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 子节点数量 = 2
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else:
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 获取中序遍历中 cur 的下一个节点
|
2023-03-12 18:46:03 +08:00
|
|
|
|
nex: TreeNode = self.get_inorder_next(cur.right)
|
|
|
|
|
tmp: int = nex.val
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 递归删除节点 nex
|
2023-02-08 15:20:18 +08:00
|
|
|
|
self.remove(nex.val)
|
|
|
|
|
# 将 nex 的值复制给 cur
|
|
|
|
|
cur.val = tmp
|
|
|
|
|
return cur
|
|
|
|
|
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def get_inorder_next(self, root: TreeNode | None) -> TreeNode | None:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况)"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if root is None:
|
|
|
|
|
return root
|
2023-04-09 04:34:58 +08:00
|
|
|
|
# 循环访问左子节点,直到叶节点时为最小节点,跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while root.left is not None:
|
|
|
|
|
root = root.left
|
|
|
|
|
return root
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="binary_search_tree.go"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 删除节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (bst *binarySearchTree) remove(num int) *TreeNode {
|
|
|
|
|
cur := bst.root
|
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
|
if cur == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点之前的节点位置
|
2023-02-08 15:20:18 +08:00
|
|
|
|
var pre *TreeNode = nil
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for cur != nil {
|
|
|
|
|
if cur.Val == num {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
pre = cur
|
|
|
|
|
if cur.Val < num {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在右子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
cur = cur.Right
|
|
|
|
|
} else {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在左子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
cur = cur.Left
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若无待删除节点,则直接返回
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数为 0 或 1
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur.Left == nil || cur.Right == nil {
|
|
|
|
|
var child *TreeNode = nil
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 取出待删除节点的子节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur.Left != nil {
|
|
|
|
|
child = cur.Left
|
|
|
|
|
} else {
|
|
|
|
|
child = cur.Right
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 将子节点替换为待删除节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if pre.Left == cur {
|
|
|
|
|
pre.Left = child
|
|
|
|
|
} else {
|
|
|
|
|
pre.Right = child
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数为 2
|
2023-02-08 15:20:18 +08:00
|
|
|
|
} else {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取中序遍历中待删除节点 cur 的下一个节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
next := bst.getInOrderNext(cur)
|
|
|
|
|
temp := next.Val
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 递归删除节点 next
|
2023-02-08 15:20:18 +08:00
|
|
|
|
bst.remove(next.Val)
|
|
|
|
|
// 将 next 的值复制给 cur
|
|
|
|
|
cur.Val = temp
|
|
|
|
|
}
|
|
|
|
|
return cur
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取中序遍历的下一个节点(仅适用于 root 有左子节点的情况) */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func (bst *binarySearchTree) getInOrderNext(node *TreeNode) *TreeNode {
|
|
|
|
|
if node == nil {
|
|
|
|
|
return node
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for node.Left != nil {
|
|
|
|
|
node = node.Left
|
|
|
|
|
}
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="binary_search_tree.js"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 删除节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function remove(num) {
|
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
|
if (root === null) return null;
|
|
|
|
|
let cur = root, pre = null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur !== null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到待删除节点,跳出循环
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.val === num) break;
|
|
|
|
|
pre = cur;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在 cur 的右子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.val < num) cur = cur.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在 cur 的左子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else cur = cur.left;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若无待删除节点,则直接返回
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur === null) return null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 0 or 1
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.left === null || cur.right === null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let child = cur.left !== null ? cur.left : cur.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点 cur
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (pre.left === cur) pre.left = child;
|
|
|
|
|
else pre.right = child;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 2
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let nex = getInOrderNext(cur.right);
|
|
|
|
|
let tmp = nex.val;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 递归删除节点 nex
|
2023-02-08 15:20:18 +08:00
|
|
|
|
remove(nex.val);
|
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
|
cur.val = tmp;
|
|
|
|
|
}
|
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function getInOrderNext(root) {
|
|
|
|
|
if (root === null) return root;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (root.left !== null) {
|
|
|
|
|
root = root.left;
|
|
|
|
|
}
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_search_tree.ts"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 删除节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function remove(num: number): TreeNode | null {
|
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
|
if (root === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
let cur = root,
|
|
|
|
|
pre: TreeNode | null = null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur !== null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到待删除节点,跳出循环
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.val === num) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pre = cur;
|
|
|
|
|
if (cur.val < num) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
cur = cur.right as TreeNode; // 待删除节点在 cur 的右子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
} else {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
cur = cur.left as TreeNode; // 待删除节点在 cur 的左子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若无待删除节点,则直接返回
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 0 or 1
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.left === null || cur.right === null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let child = cur.left !== null ? cur.left : cur.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点 cur
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (pre!.left === cur) {
|
|
|
|
|
pre!.left = child;
|
|
|
|
|
} else {
|
|
|
|
|
pre!.right = child;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 2
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let next = getInOrderNext(cur.right);
|
|
|
|
|
let tmp = next!.val;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 递归删除节点 nex
|
2023-02-08 15:20:18 +08:00
|
|
|
|
remove(next!.val);
|
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
|
cur.val = tmp;
|
|
|
|
|
}
|
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function getInOrderNext(root: TreeNode | null): TreeNode | null {
|
|
|
|
|
if (root === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (root.left !== null) {
|
|
|
|
|
root = root.left;
|
|
|
|
|
}
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="binary_search_tree.c"
|
2023-02-11 18:21:44 +08:00
|
|
|
|
[class]{binarySearchTree}-[func]{remove}
|
|
|
|
|
|
|
|
|
|
[class]{binarySearchTree}-[func]{getInOrderNext}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_search_tree.cs"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 删除节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode? remove(int num)
|
|
|
|
|
{
|
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
|
if (root == null) return null;
|
|
|
|
|
TreeNode? cur = root, pre = null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (cur != null)
|
|
|
|
|
{
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到待删除节点,跳出循环
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.val == num) break;
|
|
|
|
|
pre = cur;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在 cur 的右子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.val < num) cur = cur.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在 cur 的左子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else cur = cur.left;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若无待删除节点,则直接返回
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur == null || pre == null) return null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 0 or 1
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (cur.left == null || cur.right == null)
|
|
|
|
|
{
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode? child = cur.left != null ? cur.left : cur.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点 cur
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (pre.left == cur)
|
|
|
|
|
{
|
|
|
|
|
pre.left = child;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pre.right = child;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 2
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode? nex = getInOrderNext(cur.right);
|
|
|
|
|
if (nex != null)
|
|
|
|
|
{
|
|
|
|
|
int tmp = nex.val;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 递归删除节点 nex
|
2023-02-08 15:20:18 +08:00
|
|
|
|
remove(nex.val);
|
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
|
cur.val = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
2023-02-08 22:16:25 +08:00
|
|
|
|
TreeNode? getInOrderNext(TreeNode? root)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
{
|
|
|
|
|
if (root == null) return root;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while (root.left != null)
|
|
|
|
|
{
|
|
|
|
|
root = root.left;
|
|
|
|
|
}
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="binary_search_tree.swift"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 删除节点 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
@discardableResult
|
|
|
|
|
func remove(num: Int) -> TreeNode? {
|
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
|
if root == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var cur = root
|
|
|
|
|
var pre: TreeNode?
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while cur != nil {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到待删除节点,跳出循环
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur!.val == num {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
pre = cur
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在 cur 的右子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur!.val < num {
|
|
|
|
|
cur = cur?.right
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在 cur 的左子树中
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else {
|
|
|
|
|
cur = cur?.left
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若无待删除节点,则直接返回
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 0 or 1
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if cur?.left == nil || cur?.right == nil {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let child = cur?.left != nil ? cur?.left : cur?.right
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点 cur
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if pre?.left === cur {
|
|
|
|
|
pre?.left = child
|
|
|
|
|
} else {
|
|
|
|
|
pre?.right = child
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 2
|
2023-02-08 15:20:18 +08:00
|
|
|
|
else {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let nex = getInOrderNext(root: cur?.right)
|
|
|
|
|
let tmp = nex!.val
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 递归删除节点 nex
|
2023-02-08 15:20:18 +08:00
|
|
|
|
remove(num: nex!.val)
|
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
|
cur?.val = tmp
|
|
|
|
|
}
|
|
|
|
|
return cur
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func getInOrderNext(root: TreeNode?) -> TreeNode? {
|
|
|
|
|
var root = root
|
|
|
|
|
if root == nil {
|
|
|
|
|
return root
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
while root?.left != nil {
|
|
|
|
|
root = root?.left
|
|
|
|
|
}
|
|
|
|
|
return root
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="binary_search_tree.zig"
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点
|
2023-02-09 22:55:29 +08:00
|
|
|
|
fn remove(self: *Self, num: T) ?*inc.TreeNode(T) {
|
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
|
if (self.root == null) return null;
|
|
|
|
|
var cur = self.root;
|
|
|
|
|
var pre: ?*inc.TreeNode(T) = null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环查找,越过叶节点后跳出
|
2023-02-09 22:55:29 +08:00
|
|
|
|
while (cur != null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 找到待删除节点,跳出循环
|
2023-02-09 22:55:29 +08:00
|
|
|
|
if (cur.?.val == num) break;
|
|
|
|
|
pre = cur;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在 cur 的右子树中
|
2023-02-09 22:55:29 +08:00
|
|
|
|
if (cur.?.val < num) {
|
|
|
|
|
cur = cur.?.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 待删除节点在 cur 的左子树中
|
2023-02-09 22:55:29 +08:00
|
|
|
|
} else {
|
|
|
|
|
cur = cur.?.left;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 若无待删除节点,则直接返回
|
2023-02-09 22:55:29 +08:00
|
|
|
|
if (cur == null) return null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 0 or 1
|
2023-02-09 22:55:29 +08:00
|
|
|
|
if (cur.?.left == null or cur.?.right == null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
2023-02-09 22:55:29 +08:00
|
|
|
|
var child = if (cur.?.left != null) cur.?.left else cur.?.right;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 删除节点 cur
|
2023-02-09 22:55:29 +08:00
|
|
|
|
if (pre.?.left == cur) {
|
|
|
|
|
pre.?.left = child;
|
|
|
|
|
} else {
|
|
|
|
|
pre.?.right = child;
|
|
|
|
|
}
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 子节点数量 = 2
|
2023-02-09 22:55:29 +08:00
|
|
|
|
} else {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
2023-02-09 22:55:29 +08:00
|
|
|
|
var nex = self.getInOrderNext(cur.?.right);
|
|
|
|
|
var tmp = nex.?.val;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 递归删除节点 nex
|
2023-02-09 22:55:29 +08:00
|
|
|
|
_ = self.remove(nex.?.val);
|
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
|
cur.?.val = tmp;
|
|
|
|
|
}
|
|
|
|
|
return cur;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况)
|
2023-02-09 22:55:29 +08:00
|
|
|
|
fn getInOrderNext(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {
|
|
|
|
|
_ = self;
|
|
|
|
|
var node_tmp = node;
|
|
|
|
|
if (node_tmp == null) return null;
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
2023-02-09 22:55:29 +08:00
|
|
|
|
while (node_tmp.?.left != null) {
|
|
|
|
|
node_tmp = node_tmp.?.left;
|
|
|
|
|
}
|
|
|
|
|
return node_tmp;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 排序
|
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
我们知道,二叉树的中序遍历遵循“左 $\rightarrow$ 根 $\rightarrow$ 右”的遍历顺序,而二叉搜索树满足“左子节点 $<$ 根节点 $<$ 右子节点”的大小关系。因此,在二叉搜索树中进行中序遍历时,总是会优先遍历下一个最小节点,从而得出一个重要性质:**二叉搜索树的中序遍历序列是升序的**。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
利用中序遍历升序的性质,我们在二叉搜索树中获取有序数据仅需 $O(n)$ 时间,无需额外排序,非常高效。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![二叉搜索树的中序遍历序列](binary_search_tree.assets/bst_inorder_traversal.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 二叉搜索树的中序遍历序列 </p>
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 7.3.2. 二叉搜索树的效率
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
假设给定 $n$ 个数字,最常见的存储方式是「数组」。对于这串乱序的数字,常见操作的效率如下:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
- **查找元素**:由于数组是无序的,因此需要遍历数组来确定,使用 $O(n)$ 时间;
|
|
|
|
|
- **插入元素**:只需将元素添加至数组尾部即可,使用 $O(1)$ 时间;
|
|
|
|
|
- **删除元素**:先查找元素,使用 $O(n)$ 时间,再在数组中删除该元素,使用 $O(n)$ 时间;
|
|
|
|
|
- **获取最小 / 最大元素**:需要遍历数组来确定,使用 $O(n)$ 时间;
|
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
为了获得先验信息,我们可以预先将数组元素进行排序,得到一个「排序数组」。此时操作效率如下:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
- **查找元素**:由于数组已排序,可以使用二分查找,平均使用 $O(\log n)$ 时间;
|
|
|
|
|
- **插入元素**:先查找插入位置,使用 $O(\log n)$ 时间,再插入到指定位置,使用 $O(n)$ 时间;
|
|
|
|
|
- **删除元素**:先查找元素,使用 $O(\log n)$ 时间,再在数组中删除该元素,使用 $O(n)$ 时间;
|
|
|
|
|
- **获取最小 / 最大元素**:数组头部和尾部元素即是最小和最大元素,使用 $O(1)$ 时间;
|
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
观察可知,无序数组和有序数组中的各项操作的时间复杂度呈现“偏科”的特点,即有的快有的慢。**然而,二叉搜索树的各项操作的时间复杂度都是对数阶,在数据量 $n$ 较大时具有显著优势**。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
<div class="center-table" markdown>
|
|
|
|
|
|
|
|
|
|
| | 无序数组 | 有序数组 | 二叉搜索树 |
|
|
|
|
|
| ------------------- | -------- | ----------- | ----------- |
|
|
|
|
|
| 查找指定元素 | $O(n)$ | $O(\log n)$ | $O(\log n)$ |
|
|
|
|
|
| 插入元素 | $O(1)$ | $O(n)$ | $O(\log n)$ |
|
|
|
|
|
| 删除元素 | $O(n)$ | $O(n)$ | $O(\log n)$ |
|
|
|
|
|
| 获取最小 / 最大元素 | $O(n)$ | $O(1)$ | $O(\log n)$ |
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 7.3.3. 二叉搜索树的退化
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
在理想情况下,我们希望二叉搜索树是“平衡”的,这样就可以在 $\log n$ 轮循环内查找任意节点。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
然而,如果我们在二叉搜索树中不断地插入和删除节点,可能导致二叉树退化为链表,这时各种操作的时间复杂度也会退化为 $O(n)$ 。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![二叉搜索树的平衡与退化](binary_search_tree.assets/bst_degradation.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 二叉搜索树的平衡与退化 </p>
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 7.3.4. 二叉搜索树常见应用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 23:59:31 +08:00
|
|
|
|
- 用作系统中的多级索引,实现高效的查找、插入、删除操作。
|
|
|
|
|
- 作为某些搜索算法的底层数据结构。
|
|
|
|
|
- 用于存储数据流,以保持其有序状态。
|