This commit is contained in:
krahets
2023-11-27 02:32:06 +08:00
parent 32d5bd97aa
commit a4a23e2488
31 changed files with 179 additions and 213 deletions
@@ -61,7 +61,7 @@ comments: true
```csharp title=""
/* 二叉树的数组表示 */
// 使用 int? 可空类型 ,就可以使用 null 来标记空位
int?[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
int?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
```
=== "Go"
@@ -410,13 +410,8 @@ comments: true
```csharp title="array_binary_tree.cs"
/* 数组表示下的二叉树类 */
class ArrayBinaryTree {
private readonly List<int?> tree;
/* 构造方法 */
public ArrayBinaryTree(List<int?> arr) {
tree = new List<int?>(arr);
}
class ArrayBinaryTree(List<int?> arr) {
List<int?> tree = new(arr);
/* 节点数量 */
public int Size() {
@@ -448,50 +443,50 @@ comments: true
/* 层序遍历 */
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;
}
+12 -13
View File
@@ -71,12 +71,11 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
```csharp title=""
/* AVL 树节点类 */
class TreeNode {
public int val; // 节点值
public int height; // 节点高度
public TreeNode? left; // 左子节点
public TreeNode? right; // 右子节点
public TreeNode(int x) { val = x; }
class TreeNode(int? x) {
public int? val = x; // 节点值
public int height; // 节点高度
public TreeNode? left; // 左子节点引用
public TreeNode? right; // 右子节点引用
}
```
@@ -689,7 +688,7 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
```csharp title="avl_tree.cs"
/* 右旋操作 */
TreeNode? RightRotate(TreeNode? node) {
TreeNode? child = node.left;
TreeNode? child = node?.left;
TreeNode? grandChild = child?.right;
// 以 child 为原点,将 node 向右旋转
child.right = node;
@@ -926,7 +925,7 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
```csharp title="avl_tree.cs"
/* 左旋操作 */
TreeNode? LeftRotate(TreeNode? node) {
TreeNode? child = node.right;
TreeNode? child = node?.right;
TreeNode? grandChild = child?.left;
// 以 child 为原点,将 node 向左旋转
child.left = node;
@@ -1236,23 +1235,23 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
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);
}
}
@@ -2059,7 +2058,7 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
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;
}
}
+2 -2
View File
@@ -980,7 +980,7 @@ comments: true
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;
@@ -997,7 +997,7 @@ comments: true
tmp = tmp.left;
}
// 递归删除节点 tmp
Remove(tmp.val);
Remove(tmp.val!.Value);
// 用 tmp 覆盖 cur
cur.val = tmp.val;
}
+4 -5
View File
@@ -45,11 +45,10 @@ comments: true
```csharp title=""
/* 二叉树节点类 */
class TreeNode {
int val; // 节点值
TreeNode? left; // 左子节点引用
TreeNode? right; // 右子节点引用
TreeNode(int x) { val = x; }
class TreeNode(int? x) {
public int? val = x; // 节点值
public TreeNode? left; // 左子节点引用
public TreeNode? right; // 右子节点引用
}
```
+5 -5
View File
@@ -96,10 +96,10 @@ comments: true
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)
@@ -449,7 +449,7 @@ comments: true
void PreOrder(TreeNode? root) {
if (root == null) return;
// 访问优先级:根节点 -> 左子树 -> 右子树
list.Add(root.val);
list.Add(root.val!.Value);
PreOrder(root.left);
PreOrder(root.right);
}
@@ -459,7 +459,7 @@ comments: true
if (root == null) return;
// 访问优先级:左子树 -> 根节点 -> 右子树
InOrder(root.left);
list.Add(root.val);
list.Add(root.val!.Value);
InOrder(root.right);
}
@@ -469,7 +469,7 @@ comments: true
// 访问优先级:左子树 -> 右子树 -> 根节点
PostOrder(root.left);
PostOrder(root.right);
list.Add(root.val);
list.Add(root.val!.Value);
}
```