This commit is contained in:
krahets
2023-04-23 14:58:03 +08:00
parent 881ece517f
commit fe8027e64a
26 changed files with 344 additions and 626 deletions

View File

@@ -315,15 +315,13 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
```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;
}
@@ -460,8 +458,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
```csharp title="avl_tree.cs"
/* 获取平衡因子 */
int balanceFactor(TreeNode? node)
{
int balanceFactor(TreeNode? node) {
// 空节点平衡因子为 0
if (node == null) return 0;
// 节点平衡因子 = 左子树高度 - 右子树高度
@@ -657,8 +654,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
```csharp title="avl_tree.cs"
/* 右旋操作 */
TreeNode? rightRotate(TreeNode? node)
{
TreeNode? rightRotate(TreeNode? node) {
TreeNode? child = node.left;
TreeNode? grandChild = child?.right;
// 以 child 为原点,将 node 向右旋转
@@ -854,8 +850,7 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
```csharp title="avl_tree.cs"
/* 左旋操作 */
TreeNode? leftRotate(TreeNode? node)
{
TreeNode? leftRotate(TreeNode? node) {
TreeNode? child = node.right;
TreeNode? grandChild = child?.left;
// 以 child 为原点,将 node 向左旋转
@@ -1182,35 +1177,26 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
```csharp title="avl_tree.cs"
/* 执行旋转操作,使该子树重新恢复平衡 */
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);
@@ -1491,14 +1477,12 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
```csharp title="avl_tree.cs"
/* 插入节点 */
void insert(int 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)
@@ -1904,24 +1888,20 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
```csharp title="avl_tree.cs"
/* 删除节点 */
void remove(int 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);
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)
@@ -1929,13 +1909,10 @@ AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉
// 子节点数量 = 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);

View File

@@ -196,12 +196,10 @@ comments: true
```csharp title="binary_search_tree.cs"
/* 查找节点 */
TreeNode? search(int num)
{
TreeNode? search(int num) {
TreeNode? cur = root;
// 循环查找,越过叶节点后跳出
while (cur != null)
{
while (cur != null) {
// 目标节点在 cur 的右子树中
if (cur.val < num) cur = cur.right;
// 目标节点在 cur 的左子树中
@@ -501,14 +499,12 @@ comments: true
```csharp title="binary_search_tree.cs"
/* 插入节点 */
void insert(int num)
{
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;
@@ -520,8 +516,7 @@ comments: true
// 插入节点 val
TreeNode node = new TreeNode(num);
if (pre != null)
{
if (pre != null) {
if (pre.val < num) pre.right = node;
else pre.left = node;
}
@@ -1004,14 +999,12 @@ comments: true
```csharp title="binary_search_tree.cs"
/* 删除节点 */
void remove(int num)
{
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;
@@ -1023,27 +1016,21 @@ comments: true
// 若无待删除节点,则直接返回
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

View File

@@ -203,15 +203,13 @@ 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);
// 初始化一个列表,用于保存遍历序列
List<int> list = new();
while (queue.Count != 0)
{
while (queue.Count != 0) {
TreeNode node = queue.Dequeue(); // 队列出队
list.Add(node.val); // 保存节点值
if (node.left != null)
@@ -548,8 +546,7 @@ comments: true
```csharp title="binary_tree_dfs.cs"
/* 前序遍历 */
void preOrder(TreeNode? root)
{
void preOrder(TreeNode? root) {
if (root == null) return;
// 访问优先级:根节点 -> 左子树 -> 右子树
list.Add(root.val);
@@ -558,8 +555,7 @@ comments: true
}
/* 中序遍历 */
void inOrder(TreeNode? root)
{
void inOrder(TreeNode? root) {
if (root == null) return;
// 访问优先级:左子树 -> 根节点 -> 右子树
inOrder(root.left);
@@ -568,8 +564,7 @@ comments: true
}
/* 后序遍历 */
void postOrder(TreeNode? root)
{
void postOrder(TreeNode? root) {
if (root == null) return;
// 访问优先级:左子树 -> 右子树 -> 根节点
postOrder(root.left);