This commit is contained in:
krahets
2023-10-08 01:43:28 +08:00
parent 3d2d669b43
commit baac2d11a7
52 changed files with 999 additions and 625 deletions
+28 -28
View File
@@ -411,7 +411,7 @@ comments: true
```csharp title="array_binary_tree.cs"
/* 数组表示下的二叉树类 */
class ArrayBinaryTree {
private List<int?> tree;
private readonly List<int?> tree;
/* 构造方法 */
public ArrayBinaryTree(List<int?> arr) {
@@ -419,80 +419,80 @@ comments: true
}
/* 节点数量 */
public int size() {
public int Size() {
return tree.Count;
}
/* 获取索引为 i 节点的值 */
public int? val(int i) {
public int? Val(int i) {
// 若索引越界,则返回 null ,代表空位
if (i < 0 || i >= size())
if (i < 0 || i >= Size())
return null;
return tree[i];
}
/* 获取索引为 i 节点的左子节点的索引 */
public int left(int i) {
public int Left(int i) {
return 2 * i + 1;
}
/* 获取索引为 i 节点的右子节点的索引 */
public int right(int i) {
public int Right(int i) {
return 2 * i + 2;
}
/* 获取索引为 i 节点的父节点的索引 */
public int parent(int i) {
public int Parent(int i) {
return (i - 1) / 2;
}
/* 层序遍历 */
public List<int> levelOrder() {
List<int> res = new List<int>();
public List<int> LevelOrder() {
List<int> res = new();
// 直接遍历数组
for (int i = 0; i < size(); i++) {
if (val(i).HasValue)
res.Add(val(i).Value);
for (int i = 0; i < Size(); i++) {
if (Val(i).HasValue)
res.Add(Val(i).Value);
}
return res;
}
/* 深度优先遍历 */
private void dfs(int i, string order, List<int> res) {
private void Dfs(int i, string order, List<int> res) {
// 若为空位,则返回
if (!val(i).HasValue)
if (!Val(i).HasValue)
return;
// 前序遍历
if (order == "pre")
res.Add(val(i).Value);
dfs(left(i), order, res);
res.Add(Val(i).Value);
Dfs(Left(i), order, res);
// 中序遍历
if (order == "in")
res.Add(val(i).Value);
dfs(right(i), order, res);
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>();
dfs(0, "pre", res);
public List<int> PreOrder() {
List<int> res = new();
Dfs(0, "pre", res);
return res;
}
/* 中序遍历 */
public List<int> inOrder() {
List<int> res = new List<int>();
dfs(0, "in", res);
public List<int> InOrder() {
List<int> res = new();
Dfs(0, "in", res);
return res;
}
/* 后序遍历 */
public List<int> postOrder() {
List<int> res = new List<int>();
dfs(0, "post", res);
public List<int> PostOrder() {
List<int> res = new();
Dfs(0, "post", res);
return res;
}
}
+37 -37
View File
@@ -271,15 +271,15 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
```csharp title="avl_tree.cs"
/* 获取节点高度 */
int height(TreeNode? node) {
int Height(TreeNode? node) {
// 空节点高度为 -1 ,叶节点高度为 0
return node == null ? -1 : node.height;
}
/* 更新节点高度 */
void updateHeight(TreeNode node) {
void UpdateHeight(TreeNode node) {
// 节点高度等于最高子树高度 + 1
node.height = Math.Max(height(node.left), height(node.right)) + 1;
node.height = Math.Max(Height(node.left), Height(node.right)) + 1;
}
```
@@ -485,11 +485,11 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
```csharp title="avl_tree.cs"
/* 获取平衡因子 */
int balanceFactor(TreeNode? node) {
int BalanceFactor(TreeNode? node) {
// 空节点平衡因子为 0
if (node == null) return 0;
// 节点平衡因子 = 左子树高度 - 右子树高度
return height(node.left) - height(node.right);
return Height(node.left) - Height(node.right);
}
```
@@ -690,15 +690,15 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
```csharp title="avl_tree.cs"
/* 右旋操作 */
TreeNode? rightRotate(TreeNode? node) {
TreeNode? RightRotate(TreeNode? node) {
TreeNode? child = node.left;
TreeNode? grandChild = child?.right;
// 以 child 为原点,将 node 向右旋转
child.right = node;
node.left = grandChild;
// 更新节点高度
updateHeight(node);
updateHeight(child);
UpdateHeight(node);
UpdateHeight(child);
// 返回旋转后子树的根节点
return child;
}
@@ -927,15 +927,15 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
```csharp title="avl_tree.cs"
/* 左旋操作 */
TreeNode? leftRotate(TreeNode? node) {
TreeNode? LeftRotate(TreeNode? node) {
TreeNode? child = node.right;
TreeNode? grandChild = child?.left;
// 以 child 为原点,将 node 向左旋转
child.left = node;
node.right = grandChild;
// 更新节点高度
updateHeight(node);
updateHeight(child);
UpdateHeight(node);
UpdateHeight(child);
// 返回旋转后子树的根节点
return child;
}
@@ -1233,29 +1233,29 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
```csharp title="avl_tree.cs"
/* 执行旋转操作,使该子树重新恢复平衡 */
TreeNode? rotate(TreeNode? node) {
TreeNode? Rotate(TreeNode? node) {
// 获取节点 node 的平衡因子
int balanceFactorInt = balanceFactor(node);
int balanceFactorInt = BalanceFactor(node);
// 左偏树
if (balanceFactorInt > 1) {
if (balanceFactor(node.left) >= 0) {
if (BalanceFactor(node.left) >= 0) {
// 右旋
return rightRotate(node);
return RightRotate(node);
} else {
// 先左旋后右旋
node.left = leftRotate(node?.left);
return rightRotate(node);
node.left = LeftRotate(node?.left);
return RightRotate(node);
}
}
// 右偏树
if (balanceFactorInt < -1) {
if (balanceFactor(node.right) <= 0) {
if (BalanceFactor(node.right) <= 0) {
// 左旋
return leftRotate(node);
return LeftRotate(node);
} else {
// 先右旋后左旋
node.right = rightRotate(node?.right);
return leftRotate(node);
node.right = RightRotate(node?.right);
return LeftRotate(node);
}
}
// 平衡树,无须旋转,直接返回
@@ -1630,23 +1630,23 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
```csharp title="avl_tree.cs"
/* 插入节点 */
void insert(int val) {
root = insertHelper(root, val);
void Insert(int val) {
root = InsertHelper(root, val);
}
/* 递归插入节点(辅助方法) */
TreeNode? insertHelper(TreeNode? node, int val) {
TreeNode? InsertHelper(TreeNode? node, int val) {
if (node == null) return new TreeNode(val);
/* 1. 查找插入位置,并插入节点 */
if (val < node.val)
node.left = insertHelper(node.left, val);
node.left = InsertHelper(node.left, val);
else if (val > node.val)
node.right = insertHelper(node.right, val);
node.right = InsertHelper(node.right, val);
else
return node; // 重复节点不插入,直接返回
updateHeight(node); // 更新节点高度
UpdateHeight(node); // 更新节点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
node = Rotate(node);
// 返回子树的根节点
return node;
}
@@ -2034,21 +2034,21 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
```csharp title="avl_tree.cs"
/* 删除节点 */
void remove(int val) {
root = removeHelper(root, val);
void Remove(int val) {
root = RemoveHelper(root, val);
}
/* 递归删除节点(辅助方法) */
TreeNode? removeHelper(TreeNode? node, int val) {
TreeNode? RemoveHelper(TreeNode? node, int val) {
if (node == null) return null;
/* 1. 查找节点,并删除之 */
if (val < node.val)
node.left = removeHelper(node.left, val);
node.left = RemoveHelper(node.left, val);
else if (val > node.val)
node.right = removeHelper(node.right, val);
node.right = RemoveHelper(node.right, val);
else {
if (node.left == null || node.right == null) {
TreeNode? child = node.left != null ? node.left : node.right;
TreeNode? child = node.left ?? node.right;
// 子节点数量 = 0 ,直接删除 node 并返回
if (child == null)
return null;
@@ -2061,13 +2061,13 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
while (temp.left != null) {
temp = temp.left;
}
node.right = removeHelper(node.right, temp.val);
node.right = RemoveHelper(node.right, temp.val);
node.val = temp.val;
}
}
updateHeight(node); // 更新节点高度
UpdateHeight(node); // 更新节点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
node = Rotate(node);
// 返回子树的根节点
return node;
}
+6 -6
View File
@@ -111,7 +111,7 @@ comments: true
```csharp title="binary_search_tree.cs"
/* 查找节点 */
TreeNode? search(int num) {
TreeNode? Search(int num) {
TreeNode? cur = root;
// 循环查找,越过叶节点后跳出
while (cur != null) {
@@ -434,7 +434,7 @@ comments: true
```csharp title="binary_search_tree.cs"
/* 插入节点 */
void insert(int num) {
void Insert(int num) {
// 若树为空,则初始化根节点
if (root == null) {
root = new TreeNode(num);
@@ -456,7 +456,7 @@ comments: true
}
// 插入节点
TreeNode node = new TreeNode(num);
TreeNode node = new(num);
if (pre != null) {
if (pre.val < num)
pre.right = node;
@@ -953,7 +953,7 @@ comments: true
```csharp title="binary_search_tree.cs"
/* 删除节点 */
void remove(int num) {
void Remove(int num) {
// 若树为空,直接提前返回
if (root == null)
return;
@@ -977,7 +977,7 @@ comments: true
// 子节点数量 = 0 or 1
if (cur.left == null || cur.right == null) {
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
TreeNode? child = cur.left != null ? cur.left : cur.right;
TreeNode? child = cur.left ?? cur.right;
// 删除节点 cur
if (cur != root) {
if (pre.left == cur)
@@ -997,7 +997,7 @@ comments: true
tmp = tmp.left;
}
// 递归删除节点 tmp
remove(tmp.val);
Remove(tmp.val);
// 用 tmp 覆盖 cur
cur.val = tmp.val;
}
+6 -6
View File
@@ -279,11 +279,11 @@ comments: true
```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);
TreeNode n1 = new(1);
TreeNode n2 = new(2);
TreeNode n3 = new(3);
TreeNode n4 = new(4);
TreeNode n5 = new(5);
// 构建引用指向(即指针)
n1.left = n2;
n1.right = n3;
@@ -461,7 +461,7 @@ comments: true
```csharp title="binary_tree.cs"
/* 插入与删除节点 */
TreeNode P = new TreeNode(0);
TreeNode P = new(0);
// 在 n1 -> n2 中间插入节点 P
n1.left = P;
P.left = n2;
+10 -10
View File
@@ -91,7 +91,7 @@ comments: true
```csharp title="binary_tree_bfs.cs"
/* 层序遍历 */
List<int> levelOrder(TreeNode root) {
List<int> LevelOrder(TreeNode root) {
// 初始化队列,加入根节点
Queue<TreeNode> queue = new();
queue.Enqueue(root);
@@ -446,29 +446,29 @@ comments: true
```csharp title="binary_tree_dfs.cs"
/* 前序遍历 */
void preOrder(TreeNode? root) {
void PreOrder(TreeNode? root) {
if (root == null) return;
// 访问优先级:根节点 -> 左子树 -> 右子树
list.Add(root.val);
preOrder(root.left);
preOrder(root.right);
PreOrder(root.left);
PreOrder(root.right);
}
/* 中序遍历 */
void inOrder(TreeNode? root) {
void InOrder(TreeNode? root) {
if (root == null) return;
// 访问优先级:左子树 -> 根节点 -> 右子树
inOrder(root.left);
InOrder(root.left);
list.Add(root.val);
inOrder(root.right);
InOrder(root.right);
}
/* 后序遍历 */
void postOrder(TreeNode? root) {
void PostOrder(TreeNode? root) {
if (root == null) return;
// 访问优先级:左子树 -> 右子树 -> 根节点
postOrder(root.left);
postOrder(root.right);
PostOrder(root.left);
PostOrder(root.right);
list.Add(root.val);
}
```