mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 03:30:30 +08:00
Reformat the C# codes.
Disable creating new line before open brace.
This commit is contained in:
@@ -10,27 +10,23 @@ using NUnit.Framework;
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
/* AVL 树 */
|
||||
class AVLTree
|
||||
{
|
||||
class AVLTree {
|
||||
public TreeNode? root; // 根节点
|
||||
|
||||
/* 获取节点高度 */
|
||||
public int height(TreeNode? node)
|
||||
{
|
||||
public int height(TreeNode? node) {
|
||||
// 空节点高度为 -1 ,叶节点高度为 0
|
||||
return node == null ? -1 : node.height;
|
||||
}
|
||||
|
||||
/* 更新节点高度 */
|
||||
private void updateHeight(TreeNode node)
|
||||
{
|
||||
private void updateHeight(TreeNode node) {
|
||||
// 节点高度等于最高子树高度 + 1
|
||||
node.height = Math.Max(height(node.left), height(node.right)) + 1;
|
||||
}
|
||||
|
||||
/* 获取平衡因子 */
|
||||
public int balanceFactor(TreeNode? node)
|
||||
{
|
||||
public int balanceFactor(TreeNode? node) {
|
||||
// 空节点平衡因子为 0
|
||||
if (node == null) return 0;
|
||||
// 节点平衡因子 = 左子树高度 - 右子树高度
|
||||
@@ -38,8 +34,7 @@ class AVLTree
|
||||
}
|
||||
|
||||
/* 右旋操作 */
|
||||
TreeNode? rightRotate(TreeNode? node)
|
||||
{
|
||||
TreeNode? rightRotate(TreeNode? node) {
|
||||
TreeNode? child = node.left;
|
||||
TreeNode? grandChild = child?.right;
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
@@ -53,8 +48,7 @@ class AVLTree
|
||||
}
|
||||
|
||||
/* 左旋操作 */
|
||||
TreeNode? leftRotate(TreeNode? node)
|
||||
{
|
||||
TreeNode? leftRotate(TreeNode? node) {
|
||||
TreeNode? child = node.right;
|
||||
TreeNode? grandChild = child?.left;
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
@@ -68,35 +62,26 @@ class AVLTree
|
||||
}
|
||||
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
TreeNode? rotate(TreeNode? node)
|
||||
{
|
||||
TreeNode? rotate(TreeNode? node) {
|
||||
// 获取节点 node 的平衡因子
|
||||
int balanceFactorInt = balanceFactor(node);
|
||||
// 左偏树
|
||||
if (balanceFactorInt > 1)
|
||||
{
|
||||
if (balanceFactor(node.left) >= 0)
|
||||
{
|
||||
if (balanceFactorInt > 1) {
|
||||
if (balanceFactor(node.left) >= 0) {
|
||||
// 右旋
|
||||
return rightRotate(node);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// 先左旋后右旋
|
||||
node.left = leftRotate(node?.left);
|
||||
return rightRotate(node);
|
||||
}
|
||||
}
|
||||
// 右偏树
|
||||
if (balanceFactorInt < -1)
|
||||
{
|
||||
if (balanceFactor(node.right) <= 0)
|
||||
{
|
||||
if (balanceFactorInt < -1) {
|
||||
if (balanceFactor(node.right) <= 0) {
|
||||
// 左旋
|
||||
return leftRotate(node);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// 先右旋后左旋
|
||||
node.right = rightRotate(node?.right);
|
||||
return leftRotate(node);
|
||||
@@ -107,14 +92,12 @@ class AVLTree
|
||||
}
|
||||
|
||||
/* 插入节点 */
|
||||
public void insert(int val)
|
||||
{
|
||||
public void insert(int val) {
|
||||
root = insertHelper(root, val);
|
||||
}
|
||||
|
||||
/* 递归插入节点(辅助方法) */
|
||||
private TreeNode? insertHelper(TreeNode? node, int val)
|
||||
{
|
||||
private TreeNode? insertHelper(TreeNode? node, int val) {
|
||||
if (node == null) return new TreeNode(val);
|
||||
/* 1. 查找插入位置,并插入节点 */
|
||||
if (val < node.val)
|
||||
@@ -131,24 +114,20 @@ class AVLTree
|
||||
}
|
||||
|
||||
/* 删除节点 */
|
||||
public void remove(int val)
|
||||
{
|
||||
public void remove(int val) {
|
||||
root = removeHelper(root, val);
|
||||
}
|
||||
|
||||
/* 递归删除节点(辅助方法) */
|
||||
private TreeNode? removeHelper(TreeNode? node, int val)
|
||||
{
|
||||
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)
|
||||
{
|
||||
else {
|
||||
if (node.left == null || node.right == null) {
|
||||
TreeNode? child = node.left != null ? node.left : node.right;
|
||||
// 子节点数量 = 0 ,直接删除 node 并返回
|
||||
if (child == null)
|
||||
@@ -156,13 +135,10 @@ class AVLTree
|
||||
// 子节点数量 = 1 ,直接删除 node
|
||||
else
|
||||
node = child;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
|
||||
TreeNode? temp = node.right;
|
||||
while (temp.left != null)
|
||||
{
|
||||
while (temp.left != null) {
|
||||
temp = temp.left;
|
||||
}
|
||||
node.right = removeHelper(node.right, temp.val);
|
||||
@@ -177,12 +153,10 @@ class AVLTree
|
||||
}
|
||||
|
||||
/* 查找节点 */
|
||||
public TreeNode? search(int val)
|
||||
{
|
||||
public TreeNode? search(int val) {
|
||||
TreeNode? cur = root;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null)
|
||||
{
|
||||
while (cur != null) {
|
||||
// 目标节点在 cur 的右子树中
|
||||
if (cur.val < val)
|
||||
cur = cur.right;
|
||||
@@ -198,25 +172,21 @@ class AVLTree
|
||||
}
|
||||
}
|
||||
|
||||
public class avl_tree
|
||||
{
|
||||
static void testInsert(AVLTree tree, int val)
|
||||
{
|
||||
public class avl_tree {
|
||||
static void testInsert(AVLTree tree, int val) {
|
||||
tree.insert(val);
|
||||
Console.WriteLine("\n插入节点 " + val + " 后,AVL 树为");
|
||||
PrintUtil.PrintTree(tree.root);
|
||||
}
|
||||
|
||||
static void testRemove(AVLTree tree, int val)
|
||||
{
|
||||
static void testRemove(AVLTree tree, int val) {
|
||||
tree.remove(val);
|
||||
Console.WriteLine("\n删除节点 " + val + " 后,AVL 树为");
|
||||
PrintUtil.PrintTree(tree.root);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化空 AVL 树 */
|
||||
AVLTree avlTree = new AVLTree();
|
||||
|
||||
|
||||
@@ -9,25 +9,21 @@ using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
class BinarySearchTree
|
||||
{
|
||||
class BinarySearchTree {
|
||||
TreeNode? root;
|
||||
|
||||
public BinarySearchTree(int[] nums)
|
||||
{
|
||||
public BinarySearchTree(int[] nums) {
|
||||
Array.Sort(nums); // 排序数组
|
||||
root = buildTree(nums, 0, nums.Length - 1); // 构建二叉搜索树
|
||||
}
|
||||
|
||||
/* 获取二叉树根节点 */
|
||||
public TreeNode? getRoot()
|
||||
{
|
||||
public TreeNode? getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
/* 构建二叉搜索树 */
|
||||
public TreeNode? buildTree(int[] nums, int i, int j)
|
||||
{
|
||||
public TreeNode? buildTree(int[] nums, int i, int j) {
|
||||
if (i > j) return null;
|
||||
// 将数组中间节点作为根节点
|
||||
int mid = (i + j) / 2;
|
||||
@@ -39,12 +35,10 @@ class BinarySearchTree
|
||||
}
|
||||
|
||||
/* 查找节点 */
|
||||
public TreeNode? search(int num)
|
||||
{
|
||||
public TreeNode? search(int num) {
|
||||
TreeNode? cur = root;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null)
|
||||
{
|
||||
while (cur != null) {
|
||||
// 目标节点在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// 目标节点在 cur 的左子树中
|
||||
@@ -57,14 +51,12 @@ class BinarySearchTree
|
||||
}
|
||||
|
||||
/* 插入节点 */
|
||||
public void insert(int num)
|
||||
{
|
||||
public void insert(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == null) return;
|
||||
TreeNode? cur = root, pre = null;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null)
|
||||
{
|
||||
while (cur != null) {
|
||||
// 找到重复节点,直接返回
|
||||
if (cur.val == num) return;
|
||||
pre = cur;
|
||||
@@ -76,8 +68,7 @@ class BinarySearchTree
|
||||
|
||||
// 插入节点 val
|
||||
TreeNode node = new TreeNode(num);
|
||||
if (pre != null)
|
||||
{
|
||||
if (pre != null) {
|
||||
if (pre.val < num) pre.right = node;
|
||||
else pre.left = node;
|
||||
}
|
||||
@@ -85,14 +76,12 @@ class BinarySearchTree
|
||||
|
||||
|
||||
/* 删除节点 */
|
||||
public void remove(int num)
|
||||
{
|
||||
public void remove(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == null) return;
|
||||
TreeNode? cur = root, pre = null;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null)
|
||||
{
|
||||
while (cur != null) {
|
||||
// 找到待删除节点,跳出循环
|
||||
if (cur.val == num) break;
|
||||
pre = cur;
|
||||
@@ -104,27 +93,21 @@ class BinarySearchTree
|
||||
// 若无待删除节点,则直接返回
|
||||
if (cur == null || pre == null) return;
|
||||
// 子节点数量 = 0 or 1
|
||||
if (cur.left == null || cur.right == null)
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (pre.left == cur) {
|
||||
pre.left = child;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
pre.right = child;
|
||||
}
|
||||
}
|
||||
// 子节点数量 = 2
|
||||
else
|
||||
{
|
||||
else {
|
||||
// 获取中序遍历中 cur 的下一个节点
|
||||
TreeNode? tmp = cur.right;
|
||||
while (tmp.left != null)
|
||||
{
|
||||
while (tmp.left != null) {
|
||||
tmp = tmp.left;
|
||||
}
|
||||
// 递归删除节点 tmp
|
||||
@@ -135,11 +118,9 @@ class BinarySearchTree
|
||||
}
|
||||
}
|
||||
|
||||
public class binary_search_tree
|
||||
{
|
||||
public class binary_search_tree {
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化二叉搜索树 */
|
||||
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
|
||||
BinarySearchTree bst = new BinarySearchTree(nums);
|
||||
|
||||
@@ -9,11 +9,9 @@ using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
public class binary_tree
|
||||
{
|
||||
public class binary_tree {
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化二叉树 */
|
||||
// 初始化节点
|
||||
TreeNode n1 = new TreeNode(1);
|
||||
|
||||
@@ -9,19 +9,16 @@ using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
public class binary_tree_bfs
|
||||
{
|
||||
public class binary_tree_bfs {
|
||||
|
||||
/* 层序遍历 */
|
||||
public List<int> levelOrder(TreeNode root)
|
||||
{
|
||||
public List<int> levelOrder(TreeNode root) {
|
||||
// 初始化队列,加入根节点
|
||||
Queue<TreeNode> queue = new();
|
||||
queue.Enqueue(root);
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
List<int> list = new();
|
||||
while (queue.Count != 0)
|
||||
{
|
||||
while (queue.Count != 0) {
|
||||
TreeNode node = queue.Dequeue(); // 队列出队
|
||||
list.Add(node.val); // 保存节点值
|
||||
if (node.left != null)
|
||||
@@ -33,8 +30,7 @@ public class binary_tree_bfs
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode? root = TreeNode.ListToTree(new List<int?> { 1, 2, 3, 4, 5, 6, 7 });
|
||||
|
||||
@@ -9,13 +9,11 @@ using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_tree;
|
||||
|
||||
public class binary_tree_dfs
|
||||
{
|
||||
public class binary_tree_dfs {
|
||||
List<int> list = new();
|
||||
|
||||
/* 前序遍历 */
|
||||
void preOrder(TreeNode? root)
|
||||
{
|
||||
void preOrder(TreeNode? root) {
|
||||
if (root == null) return;
|
||||
// 访问优先级:根节点 -> 左子树 -> 右子树
|
||||
list.Add(root.val);
|
||||
@@ -24,8 +22,7 @@ public class binary_tree_dfs
|
||||
}
|
||||
|
||||
/* 中序遍历 */
|
||||
void inOrder(TreeNode? root)
|
||||
{
|
||||
void inOrder(TreeNode? root) {
|
||||
if (root == null) return;
|
||||
// 访问优先级:左子树 -> 根节点 -> 右子树
|
||||
inOrder(root.left);
|
||||
@@ -34,8 +31,7 @@ public class binary_tree_dfs
|
||||
}
|
||||
|
||||
/* 后序遍历 */
|
||||
void postOrder(TreeNode? root)
|
||||
{
|
||||
void postOrder(TreeNode? root) {
|
||||
if (root == null) return;
|
||||
// 访问优先级:左子树 -> 右子树 -> 根节点
|
||||
postOrder(root.left);
|
||||
@@ -44,8 +40,7 @@ public class binary_tree_dfs
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode? root = TreeNode.ListToTree(new List<int?> { 1, 2, 3, 4, 5, 6, 7 });
|
||||
|
||||
Reference in New Issue
Block a user