mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-12 19:47:33 +08:00
Add build scripts for C# and
unify the coding style.
This commit is contained in:
@@ -253,19 +253,9 @@ 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;
|
||||
}
|
||||
[class]{AVLTree}-[func]{height}
|
||||
|
||||
/* 更新结点高度 */
|
||||
private void updateHeight(TreeNode node)
|
||||
{
|
||||
// 结点高度等于最高子树高度 + 1
|
||||
node.height = Math.Max(height(node.left), height(node.right)) + 1;
|
||||
}
|
||||
[class]{AVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -349,14 +339,7 @@ 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);
|
||||
}
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -460,20 +443,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
/* 右旋操作 */
|
||||
TreeNode? rightRotate(TreeNode? node)
|
||||
{
|
||||
TreeNode? child = node.left;
|
||||
TreeNode? grandChild = child?.right;
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
child.right = node;
|
||||
node.left = grandChild;
|
||||
// 更新结点高度
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -559,20 +529,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
/* 左旋操作 */
|
||||
TreeNode? leftRotate(TreeNode? node)
|
||||
{
|
||||
TreeNode? child = node.right;
|
||||
TreeNode? grandChild = child?.left;
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
child.left = node;
|
||||
node.right = grandChild;
|
||||
// 更新结点高度
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -694,44 +651,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
TreeNode? rotate(TreeNode? 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;
|
||||
}
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -889,30 +809,9 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
/* 插入结点 */
|
||||
public TreeNode? insert(int val)
|
||||
{
|
||||
root = insertHelper(root, val);
|
||||
return root;
|
||||
}
|
||||
[class]{AVLTree}-[func]{insert}
|
||||
|
||||
/* 递归插入结点(辅助函数) */
|
||||
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;
|
||||
}
|
||||
[class]{AVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -1186,48 +1085,11 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
/* 删除结点 */
|
||||
public TreeNode? remove(int val)
|
||||
{
|
||||
root = removeHelper(root, val);
|
||||
return root;
|
||||
}
|
||||
[class]{AVLTree}-[func]{remove}
|
||||
|
||||
/* 递归删除结点(辅助函数) */
|
||||
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 = getInOrderNext(node.right);
|
||||
node.right = removeHelper(node.right, temp.val);
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
[class]{AVLTree}-[func]{removeHelper}
|
||||
|
||||
[class]{AVLTree}-[func]{getInOrderNext}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
@@ -98,23 +98,7 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="binary_search_tree.cs"
|
||||
/* 查找结点 */
|
||||
TreeNode? search(int num)
|
||||
{
|
||||
TreeNode? cur = root;
|
||||
// 循环查找,越过叶结点后跳出
|
||||
while (cur != null)
|
||||
{
|
||||
// 目标结点在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// 目标结点在 cur 的左子树中
|
||||
else if (cur.val > num) cur = cur.left;
|
||||
// 找到目标结点,跳出循环
|
||||
else break;
|
||||
}
|
||||
// 返回目标结点
|
||||
return cur;
|
||||
}
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -214,33 +198,7 @@ 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;
|
||||
// 插入位置在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// 插入位置在 cur 的左子树中
|
||||
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;
|
||||
}
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -411,68 +369,9 @@ 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;
|
||||
// 待删除结点在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// 待删除结点在 cur 的左子树中
|
||||
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 = getInOrderNext(cur.right);
|
||||
if (nex != null)
|
||||
{
|
||||
int tmp = nex.val;
|
||||
// 递归删除结点 nex
|
||||
remove(nex.val);
|
||||
// 将 nex 的值复制给 cur
|
||||
cur.val = tmp;
|
||||
}
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
|
||||
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
|
||||
private TreeNode? getInOrderNext(TreeNode? root)
|
||||
{
|
||||
if (root == null) return root;
|
||||
// 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
while (root.left != null)
|
||||
{
|
||||
root = root.left;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
[class]{BinarySearchTree}-[func]{getInOrderNext}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
@@ -85,26 +85,7 @@ 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;
|
||||
}
|
||||
|
||||
[class]{binary_tree_bfs}-[func]{hierOrder}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -235,35 +216,11 @@ 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);
|
||||
}
|
||||
[class]{binary_tree_dfs}-[func]{preOrder}
|
||||
|
||||
/* 中序遍历 */
|
||||
void inOrder(TreeNode? root)
|
||||
{
|
||||
if (root == null) return;
|
||||
// 访问优先级:左子树 -> 根结点 -> 右子树
|
||||
inOrder(root.left);
|
||||
list.Add(root.val);
|
||||
inOrder(root.right);
|
||||
}
|
||||
[class]{binary_tree_dfs}-[func]{inOrder}
|
||||
|
||||
/* 后序遍历 */
|
||||
void postOrder(TreeNode? root)
|
||||
{
|
||||
if (root == null) return;
|
||||
// 访问优先级:左子树 -> 右子树 -> 根结点
|
||||
postOrder(root.left);
|
||||
postOrder(root.right);
|
||||
list.Add(root.val);
|
||||
}
|
||||
[class]{binary_tree_dfs}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
Reference in New Issue
Block a user