完善所以c#相关的文档和代码

This commit is contained in:
zhuzhiqing
2022-12-23 15:42:02 +08:00
parent 1646c284f6
commit a427cb1b4d
48 changed files with 4325 additions and 65 deletions
+182 -6
View File
@@ -78,7 +78,14 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C#"
```csharp title="avl_tree.cs"
/* AVL 树结点类 */
class TreeNode {
public int val; // 结点值
public int height; // 结点高度
public TreeNode left; // 左子结点
public TreeNode right; // 右子结点
public TreeNode(int x) { val = x; }
}
```
「结点高度」是最远叶结点到该结点的距离,即走过的「边」的数量。需要特别注意,**叶结点的高度为 0 ,空结点的高度为 -1** 。我们封装两个工具函数,分别用于获取与更新结点的高度。
@@ -138,7 +145,19 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C#"
```csharp title="avl_tree.cs"
/* 获取结点高度 */
public int height(TreeNode? node)
{
// 空结点高度为 -1 ,叶结点高度为 0
return node == null ? -1 : node.height;
}
/* 更新结点高度 */
private void updateHeight(TreeNode node)
{
// 结点高度等于最高子树高度 + 1
node.height = Math.Max(height(node.left), height(node.right)) + 1;
}
```
### 结点平衡因子
@@ -196,7 +215,14 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C#"
```csharp title="avl_tree.cs"
/* 获取平衡因子 */
public int balanceFactor(TreeNode? node)
{
// 空结点平衡因子为 0
if (node == null) return 0;
// 结点平衡因子 = 左子树高度 - 右子树高度
return height(node.left) - height(node.right);
}
```
!!! note
@@ -285,6 +311,23 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
=== "C#"
```csharp title="avl_tree.cs"
/* 右旋操作 */
TreeNode? rightRotate(TreeNode? node)
{
if (node == null)
return null;
TreeNode? child = node.left;
TreeNode? grandChild = child?.right;
// 以 child 为原点,将 node 向右旋转
child.right = node;
node.left = grandChild;
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根节点
return child;
}
```
@@ -353,7 +396,23 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
=== "C#"
```csharp title="avl_tree.cs"
/* 左旋操作 */
TreeNode? leftRotate(TreeNode? node)
{
if (node == null)
return null;
TreeNode? child = node.right;
TreeNode? grandChild = child?.left;
// 以 child 为原点,将 node 向左旋转
child.left = node;
node.right = grandChild;
// 更新结点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根节点
return child;
}
```
### Case 3 - 先左后右
@@ -462,7 +521,47 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
=== "C#"
```csharp title="avl_tree.cs"
/* 执行旋转操作,使该子树重新恢复平衡 */
TreeNode? rotate(TreeNode? node)
{
if (node == null)
return node;
// 获取结点 node 的平衡因子
int balanceFactorInt = balanceFactor(node);
// 左偏树
if (balanceFactorInt > 1)
{
if (balanceFactor(node.left) >= 0)
{
// 右旋
return rightRotate(node);
}
else
{
// 先左旋后右旋
node.left = leftRotate(node?.left);
return rightRotate(node);
}
}
// 右偏树
if (balanceFactorInt < -1)
{
if (balanceFactor(node.right) <= 0)
{
// 左旋
return leftRotate(node);
}
else
{
// 先右旋后左旋
node.right = rightRotate(node?.right);
return leftRotate(node);
}
}
// 平衡树,无需旋转,直接返回
return node;
}
```
## AVL 树常用操作
@@ -537,6 +636,30 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
=== "C#"
```csharp title="avl_tree.cs"
/* 插入结点 */
public TreeNode? insert(int val)
{
root = insertHelper(root, val);
return root;
}
/* 递归插入结点(辅助函数) */
private TreeNode? insertHelper(TreeNode? node, int val)
{
if (node == null) return new TreeNode(val);
/* 1. 查找插入位置,并插入结点 */
if (val < node.val)
node.left = insertHelper(node.left, val);
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
return node; // 重复结点不插入,直接返回
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根节点
return node;
}
```
@@ -634,7 +757,60 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
=== "C#"
```csharp title="avl_tree.cs"
/* 删除结点 */
public TreeNode? remove(int val)
{
root = removeHelper(root, val);
return root;
}
/* 递归删除结点(辅助函数) */
private TreeNode? removeHelper(TreeNode? node, int? val)
{
if (node == null) return null;
/* 1. 查找结点,并删除之 */
if (val < node.val)
node.left = removeHelper(node.left, val);
else if (val > node.val)
node.right = removeHelper(node.right, val);
else
{
if (node.left == null || node.right == null)
{
TreeNode? child = node.left != null ? node.left : node.right;
// 子结点数量 = 0 ,直接删除 node 并返回
if (child == null)
return null;
// 子结点数量 = 1 ,直接删除 node
else
node = child;
}
else
{
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
TreeNode? temp = minNode(node.right);
node.right = removeHelper(node.right, temp?.val);
node.val = temp?.val;
}
}
updateHeight(node); // 更新结点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根节点
return node;
}
/* 获取最小结点 */
private TreeNode? minNode(TreeNode? node)
{
if (node == null) return node;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (node.left != null)
{
node = node.left;
}
return node;
}
```
### 查找结点
+105 -1
View File
@@ -159,7 +159,23 @@ 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;
}
```
### 插入结点
@@ -335,7 +351,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}$ 时,我们也可以获取到其父结点,从而完成结点插入操作。
@@ -649,7 +691,69 @@ 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;
}
```
## 二叉搜索树的优势
+29 -4
View File
@@ -97,7 +97,13 @@ comments: true
=== "C#"
```csharp title=""
/* 链表结点类 */
class TreeNode {
int val; // 结点值
TreeNode left; // 左子结点指针
TreeNode right; // 右子结点指针
TreeNode(int x) { val = x; }
}
```
结点的两个指针分别指向「左子结点 Left Child Node」和「右子结点 Right Child Node」,并且称该结点为两个子结点的「父结点 Parent Node」。给定二叉树某结点,将左子结点以下的树称为该结点的「左子树 Left Subtree」,右子树同理。
@@ -232,7 +238,18 @@ comments: true
=== "C#"
```csharp title="binary_tree.cs"
/* 初始化二叉树 */
// 初始化结点
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
TreeNode n5 = new TreeNode(5);
// 构建引用指向(即指针)
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
```
**插入与删除结点。** 与链表类似,插入与删除结点都可以通过修改指针实现。
@@ -315,7 +332,13 @@ comments: true
=== "C#"
```csharp title="binary_tree.cs"
/* 插入与删除结点 */
TreeNode P = new TreeNode(0);
// 在 n1 -> n2 中间插入结点 P
n1.left = P;
P.left = n2;
// 删除结点 P
n1.left = n2;
```
!!! note
@@ -446,7 +469,9 @@ comments: true
=== "C#"
```csharp title=""
/* 二叉树的数组表示 */
// 使用 int?可空类型 ,就可以使用 null 来标记空位
int?[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
```
![array_representation_with_empty](binary_tree.assets/array_representation_with_empty.png)
@@ -149,6 +149,25 @@ comments: true
=== "C#"
```csharp title="binary_tree_bfs.cs"
/* 层序遍历 */
public List<int?> hierOrder(TreeNode root)
{
// 初始化队列,加入根结点
Queue<TreeNode> queue = new();
queue.Enqueue(root);
// 初始化一个列表,用于保存遍历序列
List<int?> list = new();
while (queue.Count != 0)
{
TreeNode node = queue.Dequeue(); // 队列出队
list.Add(node.val); // 保存结点值
if (node.left != null)
queue.Enqueue(node.left); // 左子结点入队
if (node.right != null)
queue.Enqueue(node.right); // 右子结点入队
}
return list;
}
```
@@ -354,6 +373,35 @@ comments: true
=== "C#"
```csharp title="binary_tree_dfs.cs"
/* 前序遍历 */
void preOrder(TreeNode? root)
{
if (root == null) return;
// 访问优先级:根结点 -> 左子树 -> 右子树
list.Add(root.val);
preOrder(root.left);
preOrder(root.right);
}
/* 中序遍历 */
void inOrder(TreeNode? root)
{
if (root == null) return;
// 访问优先级:左子树 -> 根结点 -> 右子树
inOrder(root.left);
list.Add(root.val);
inOrder(root.right);
}
/* 后序遍历 */
void postOrder(TreeNode? root)
{
if (root == null) return;
// 访问优先级:左子树 -> 右子树 -> 根结点
postOrder(root.left);
postOrder(root.right);
list.Add(root.val);
}
```