Merge branch 'master' into master

This commit is contained in:
Yudong Jin
2022-12-27 19:16:39 +08:00
committed by GitHub
139 changed files with 6761 additions and 913 deletions

View File

@@ -173,12 +173,28 @@ comments: true
=== "C#"
```csharp title="binary_search_tree.cs"
/* 查找结点 */
TreeNode? search(int num)
{
TreeNode? cur = root;
// 循环查找,越过叶结点后跳出
while (cur != null)
{
// 目标结点在 root 的右子树中
if (cur.val < num) cur = cur.right;
// 目标结点在 root 的左子树中
else if (cur.val > num) cur = cur.left;
// 找到目标结点,跳出循环
else break;
}
// 返回目标结点
return cur;
}
```
### 插入结点
给定一个待插入元素 `num` ,为了保持二叉搜索树 “左子树 < 根结点 < 右子树” 的性质,插入操作分为两步:
给定一个待插入元素 `num` ,为了保持二叉搜索树“左子树 < 根结点 < 右子树”的性质,插入操作分为两步:
1. **查找插入位置:** 与查找操作类似,我们从根结点出发,根据当前结点值和 `num` 的大小关系循环向下搜索,直到越过叶结点(遍历到 $\text{null}$ )时跳出循环;
2. **在该位置插入结点:** 初始化结点 `num` ,将该结点放到 $\text{null}$ 的位置
@@ -377,7 +393,33 @@ comments: true
=== "C#"
```csharp title="binary_search_tree.cs"
/* 插入结点 */
TreeNode? insert(int num)
{
// 若树为空,直接提前返回
if (root == null) return null;
TreeNode? cur = root, pre = null;
// 循环查找,越过叶结点后跳出
while (cur != null)
{
// 找到重复结点,直接返回
if (cur.val == num) return null;
pre = cur;
// 插入位置在 root 的右子树中
if (cur.val < num) cur = cur.right;
// 插入位置在 root 的左子树中
else cur = cur.left;
}
// 插入结点 val
TreeNode node = new TreeNode(num);
if (pre != null)
{
if (pre.val < num) pre.right = node;
else pre.left = node;
}
return node;
}
```
为了插入结点,需要借助 **辅助结点 `prev`** 保存上一轮循环的结点,这样在遍历到 $\text{null}$ 时,我们也可以获取到其父结点,从而完成结点插入操作。
@@ -386,7 +428,7 @@ comments: true
### 删除结点
与插入结点一样,我们需要在删除操作后维持二叉搜索树的 “左子树 < 根结点 < 右子树” 的性质。首先,我们需要在二叉树中执行查找操作,获取待删除结点。接下来,根据待删除结点的子结点数量,删除操作需要分为三种情况:
与插入结点一样,我们需要在删除操作后维持二叉搜索树的“左子树 < 根结点 < 右子树”的性质。首先,我们需要在二叉树中执行查找操作,获取待删除结点。接下来,根据待删除结点的子结点数量,删除操作需要分为三种情况:
**待删除结点的子结点数量 $= 0$ 。** 表明待删除结点是叶结点,直接删除即可。
@@ -744,7 +786,68 @@ comments: true
=== "C#"
```csharp title="binary_search_tree.cs"
/* 删除结点 */
TreeNode? remove(int num)
{
// 若树为空,直接提前返回
if (root == null) return null;
TreeNode? cur = root, pre = null;
// 循环查找,越过叶结点后跳出
while (cur != null)
{
// 找到待删除结点,跳出循环
if (cur.val == num) break;
pre = cur;
// 待删除结点在 root 的右子树中
if (cur.val < num) cur = cur.right;
// 待删除结点在 root 的左子树中
else cur = cur.left;
}
// 若无待删除结点,则直接返回
if (cur == null || pre == null) return null;
// 子结点数量 = 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)
{
pre.left = child;
}
else
{
pre.right = child;
}
}
// 子结点数量 = 2
else
{
// 获取中序遍历中 cur 的下一个结点
TreeNode? nex = min(cur.right);
if (nex != null)
{
int tmp = nex.val;
// 递归删除结点 nex
remove(nex.val);
// 将 nex 的值复制给 cur
cur.val = tmp;
}
}
return cur;
}
/* 获取最小结点 */
TreeNode? min(TreeNode? root)
{
if (root == null) return root;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (root.left != null)
{
root = root.left;
}
return root;
}
```
## 二叉搜索树的优势
@@ -763,7 +866,7 @@ comments: true
- **删除元素:** 与无序数组中的情况相同,使用 $O(n)$ 时间;
- **获取最小 / 最大元素:** 数组头部和尾部元素即是最小和最大元素,使用 $O(1)$ 时间;
观察发现,无序数组和有序数组中的各操作的时间复杂度是 “偏科” 的,即有的快有的慢;**而二叉搜索树的各项操作的时间复杂度都是对数阶,在数据量 $n$ 很大时有巨大优势**。
观察发现,无序数组和有序数组中的各操作的时间复杂度是“偏科”的,即有的快有的慢;**而二叉搜索树的各项操作的时间复杂度都是对数阶,在数据量 $n$ 很大时有巨大优势**。
<div class="center-table" markdown>
@@ -778,7 +881,7 @@ comments: true
## 二叉搜索树的退化
理想情况下,我们希望二叉搜索树的是 “左右平衡” 的(详见「平衡二叉树」章节),此时可以在 $\log n$ 轮循环内查找任意结点。
理想情况下,我们希望二叉搜索树的是“左右平衡”的(详见「平衡二叉树」章节),此时可以在 $\log n$ 轮循环内查找任意结点。
如果我们动态地在二叉搜索树中插入与删除结点,**则可能导致二叉树退化为链表**,此时各种操作的时间复杂度也退化之 $O(n)$ 。