mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 07:26:07 +00:00
feat(csharp) .NET 8.0 code migration (#966)
* .net 8.0 migration * update docs * revert change * revert change and update appendix docs * remove static * Update binary_search_insertion.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -11,13 +11,13 @@ class AVLTree {
|
||||
public TreeNode? root; // 根节点
|
||||
|
||||
/* 获取节点高度 */
|
||||
public int Height(TreeNode? node) {
|
||||
int Height(TreeNode? node) {
|
||||
// 空节点高度为 -1 ,叶节点高度为 0
|
||||
return node == null ? -1 : node.height;
|
||||
}
|
||||
|
||||
/* 更新节点高度 */
|
||||
private void UpdateHeight(TreeNode node) {
|
||||
void UpdateHeight(TreeNode node) {
|
||||
// 节点高度等于最高子树高度 + 1
|
||||
node.height = Math.Max(Height(node.left), Height(node.right)) + 1;
|
||||
}
|
||||
@@ -32,7 +32,7 @@ class AVLTree {
|
||||
|
||||
/* 右旋操作 */
|
||||
TreeNode? RightRotate(TreeNode? node) {
|
||||
TreeNode? child = node.left;
|
||||
TreeNode? child = node?.left;
|
||||
TreeNode? grandChild = child?.right;
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
child.right = node;
|
||||
@@ -46,7 +46,7 @@ class AVLTree {
|
||||
|
||||
/* 左旋操作 */
|
||||
TreeNode? LeftRotate(TreeNode? node) {
|
||||
TreeNode? child = node.right;
|
||||
TreeNode? child = node?.right;
|
||||
TreeNode? grandChild = child?.left;
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
child.left = node;
|
||||
@@ -64,23 +64,23 @@ class AVLTree {
|
||||
int balanceFactorInt = BalanceFactor(node);
|
||||
// 左偏树
|
||||
if (balanceFactorInt > 1) {
|
||||
if (BalanceFactor(node.left) >= 0) {
|
||||
if (BalanceFactor(node?.left) >= 0) {
|
||||
// 右旋
|
||||
return RightRotate(node);
|
||||
} else {
|
||||
// 先左旋后右旋
|
||||
node.left = LeftRotate(node?.left);
|
||||
node!.left = LeftRotate(node!.left);
|
||||
return RightRotate(node);
|
||||
}
|
||||
}
|
||||
// 右偏树
|
||||
if (balanceFactorInt < -1) {
|
||||
if (BalanceFactor(node.right) <= 0) {
|
||||
if (BalanceFactor(node?.right) <= 0) {
|
||||
// 左旋
|
||||
return LeftRotate(node);
|
||||
} else {
|
||||
// 先右旋后左旋
|
||||
node.right = RightRotate(node?.right);
|
||||
node!.right = RightRotate(node!.right);
|
||||
return LeftRotate(node);
|
||||
}
|
||||
}
|
||||
@@ -94,7 +94,7 @@ class AVLTree {
|
||||
}
|
||||
|
||||
/* 递归插入节点(辅助方法) */
|
||||
private TreeNode? InsertHelper(TreeNode? node, int val) {
|
||||
TreeNode? InsertHelper(TreeNode? node, int val) {
|
||||
if (node == null) return new TreeNode(val);
|
||||
/* 1. 查找插入位置,并插入节点 */
|
||||
if (val < node.val)
|
||||
@@ -116,7 +116,7 @@ class AVLTree {
|
||||
}
|
||||
|
||||
/* 递归删除节点(辅助方法) */
|
||||
private TreeNode? RemoveHelper(TreeNode? node, int val) {
|
||||
TreeNode? RemoveHelper(TreeNode? node, int val) {
|
||||
if (node == null) return null;
|
||||
/* 1. 查找节点,并删除之 */
|
||||
if (val < node.val)
|
||||
@@ -138,7 +138,7 @@ class AVLTree {
|
||||
while (temp.left != null) {
|
||||
temp = temp.left;
|
||||
}
|
||||
node.right = RemoveHelper(node.right, temp.val);
|
||||
node.right = RemoveHelper(node.right, temp.val!.Value);
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user