mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08: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:
@ -7,13 +7,8 @@
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
/* 数组表示下的二叉树类 */
|
||||
public class ArrayBinaryTree {
|
||||
private readonly List<int?> tree;
|
||||
|
||||
/* 构造方法 */
|
||||
public ArrayBinaryTree(List<int?> arr) {
|
||||
tree = new List<int?>(arr);
|
||||
}
|
||||
public class ArrayBinaryTree(List<int?> arr) {
|
||||
List<int?> tree = new(arr);
|
||||
|
||||
/* 节点数量 */
|
||||
public int Size() {
|
||||
@ -45,50 +40,50 @@ public class ArrayBinaryTree {
|
||||
|
||||
/* 层序遍历 */
|
||||
public List<int> LevelOrder() {
|
||||
List<int> res = new();
|
||||
List<int> res = [];
|
||||
// 直接遍历数组
|
||||
for (int i = 0; i < Size(); i++) {
|
||||
if (Val(i).HasValue)
|
||||
res.Add(Val(i).Value);
|
||||
res.Add(Val(i)!.Value);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 深度优先遍历 */
|
||||
private void DFS(int i, string order, List<int> res) {
|
||||
void DFS(int i, string order, List<int> res) {
|
||||
// 若为空位,则返回
|
||||
if (!Val(i).HasValue)
|
||||
return;
|
||||
// 前序遍历
|
||||
if (order == "pre")
|
||||
res.Add(Val(i).Value);
|
||||
res.Add(Val(i)!.Value);
|
||||
DFS(Left(i), order, res);
|
||||
// 中序遍历
|
||||
if (order == "in")
|
||||
res.Add(Val(i).Value);
|
||||
res.Add(Val(i)!.Value);
|
||||
DFS(Right(i), order, res);
|
||||
// 后序遍历
|
||||
if (order == "post")
|
||||
res.Add(Val(i).Value);
|
||||
res.Add(Val(i)!.Value);
|
||||
}
|
||||
|
||||
/* 前序遍历 */
|
||||
public List<int> PreOrder() {
|
||||
List<int> res = new();
|
||||
List<int> res = [];
|
||||
DFS(0, "pre", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 中序遍历 */
|
||||
public List<int> InOrder() {
|
||||
List<int> res = new();
|
||||
List<int> res = [];
|
||||
DFS(0, "in", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 后序遍历 */
|
||||
public List<int> PostOrder() {
|
||||
List<int> res = new();
|
||||
List<int> res = [];
|
||||
DFS(0, "post", res);
|
||||
return res;
|
||||
}
|
||||
@ -99,9 +94,9 @@ public class array_binary_tree {
|
||||
public void Test() {
|
||||
// 初始化二叉树
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
List<int?> arr = new() { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
|
||||
List<int?> arr = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
|
||||
|
||||
TreeNode root = TreeNode.ListToTree(arr);
|
||||
TreeNode? root = TreeNode.ListToTree(arr);
|
||||
Console.WriteLine("\n初始化二叉树\n");
|
||||
Console.WriteLine("二叉树的数组表示:");
|
||||
Console.WriteLine(arr.PrintList());
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,7 +99,7 @@ class BinarySearchTree {
|
||||
TreeNode? child = cur.left ?? cur.right;
|
||||
// 删除节点 cur
|
||||
if (cur != root) {
|
||||
if (pre.left == cur)
|
||||
if (pre!.left == cur)
|
||||
pre.left = child;
|
||||
else
|
||||
pre.right = child;
|
||||
@ -116,7 +116,7 @@ class BinarySearchTree {
|
||||
tmp = tmp.left;
|
||||
}
|
||||
// 递归删除节点 tmp
|
||||
Remove(tmp.val);
|
||||
Remove(tmp.val!.Value);
|
||||
// 用 tmp 覆盖 cur
|
||||
cur.val = tmp.val;
|
||||
}
|
||||
@ -129,7 +129,7 @@ public class binary_search_tree {
|
||||
/* 初始化二叉搜索树 */
|
||||
BinarySearchTree bst = new();
|
||||
// 请注意,不同的插入顺序会生成不同的二叉树,该序列可以生成一个完美二叉树
|
||||
int[] nums = { 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };
|
||||
int[] nums = [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15];
|
||||
foreach (int num in nums) {
|
||||
bst.Insert(num);
|
||||
}
|
||||
@ -139,7 +139,7 @@ public class binary_search_tree {
|
||||
|
||||
/* 查找节点 */
|
||||
TreeNode? node = bst.Search(7);
|
||||
Console.WriteLine("\n查找到的节点对象为 " + node + ",节点值 = " + node.val);
|
||||
Console.WriteLine("\n查找到的节点对象为 " + node + ",节点值 = " + node?.val);
|
||||
|
||||
/* 插入节点 */
|
||||
bst.Insert(16);
|
||||
|
||||
@ -9,15 +9,15 @@ namespace hello_algo.chapter_tree;
|
||||
public class binary_tree_bfs {
|
||||
|
||||
/* 层序遍历 */
|
||||
public List<int> LevelOrder(TreeNode root) {
|
||||
List<int> LevelOrder(TreeNode root) {
|
||||
// 初始化队列,加入根节点
|
||||
Queue<TreeNode> queue = new();
|
||||
queue.Enqueue(root);
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
List<int> list = new();
|
||||
List<int> list = [];
|
||||
while (queue.Count != 0) {
|
||||
TreeNode node = queue.Dequeue(); // 队列出队
|
||||
list.Add(node.val); // 保存节点值
|
||||
list.Add(node.val!.Value); // 保存节点值
|
||||
if (node.left != null)
|
||||
queue.Enqueue(node.left); // 左子节点入队
|
||||
if (node.right != null)
|
||||
@ -30,11 +30,11 @@ public class binary_tree_bfs {
|
||||
public void Test() {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode? root = TreeNode.ListToTree(new List<int?> { 1, 2, 3, 4, 5, 6, 7 });
|
||||
TreeNode? root = TreeNode.ListToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\n初始化二叉树\n");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
List<int> list = LevelOrder(root);
|
||||
List<int> list = LevelOrder(root!);
|
||||
Console.WriteLine("\n层序遍历的节点打印序列 = " + string.Join(",", list));
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,13 +7,13 @@
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
public class binary_tree_dfs {
|
||||
readonly List<int> list = new();
|
||||
List<int> list = [];
|
||||
|
||||
/* 前序遍历 */
|
||||
void PreOrder(TreeNode? root) {
|
||||
if (root == null) return;
|
||||
// 访问优先级:根节点 -> 左子树 -> 右子树
|
||||
list.Add(root.val);
|
||||
list.Add(root.val!.Value);
|
||||
PreOrder(root.left);
|
||||
PreOrder(root.right);
|
||||
}
|
||||
@ -23,7 +23,7 @@ public class binary_tree_dfs {
|
||||
if (root == null) return;
|
||||
// 访问优先级:左子树 -> 根节点 -> 右子树
|
||||
InOrder(root.left);
|
||||
list.Add(root.val);
|
||||
list.Add(root.val!.Value);
|
||||
InOrder(root.right);
|
||||
}
|
||||
|
||||
@ -33,14 +33,14 @@ public class binary_tree_dfs {
|
||||
// 访问优先级:左子树 -> 右子树 -> 根节点
|
||||
PostOrder(root.left);
|
||||
PostOrder(root.right);
|
||||
list.Add(root.val);
|
||||
list.Add(root.val!.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode? root = TreeNode.ListToTree(new List<int?> { 1, 2, 3, 4, 5, 6, 7 });
|
||||
TreeNode? root = TreeNode.ListToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\n初始化二叉树\n");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user