This commit is contained in:
krahets
2024-07-30 16:54:41 +08:00
parent f9d935e34c
commit 03a6cd27ca
31 changed files with 94 additions and 92 deletions

View File

@@ -767,21 +767,21 @@ It can be observed that **the right and left rotation operations are logically s
[class]{AVLTree}-[func]{leftRotate}
```
### 3.   Right-left rotation
### 3.   Left-right rotation
For the unbalanced node 3 shown in Figure 7-30, using either left or right rotation alone cannot restore balance to the subtree. In this case, a "left rotation" needs to be performed on `child` first, followed by a "right rotation" on `node`.
![Right-left rotation](avl_tree.assets/avltree_left_right_rotate.png){ class="animation-figure" }
![Left-right rotation](avl_tree.assets/avltree_left_right_rotate.png){ class="animation-figure" }
<p align="center"> Figure 7-30 &nbsp; Right-left rotation </p>
<p align="center"> Figure 7-30 &nbsp; Left-right rotation </p>
### 4. &nbsp; Left-right rotation
### 4. &nbsp; Right-left rotation
As shown in Figure 7-31, for the mirror case of the above unbalanced binary tree, a "right rotation" needs to be performed on `child` first, followed by a "left rotation" on `node`.
![Left-right rotation](avl_tree.assets/avltree_right_left_rotate.png){ class="animation-figure" }
![Right-left rotation](avl_tree.assets/avltree_right_left_rotate.png){ class="animation-figure" }
<p align="center"> Figure 7-31 &nbsp; Left-right rotation </p>
<p align="center"> Figure 7-31 &nbsp; Right-left rotation </p>
### 5. &nbsp; Choice of rotation

View File

@@ -61,7 +61,7 @@ A <u>binary tree</u> is a non-linear data structure that represents the hierarch
Left *TreeNode
Right *TreeNode
}
/* 构造方法 */
/* Constructor */
func NewTreeNode(v int) *TreeNode {
return &TreeNode{
Left: nil, // Pointer to left child node
@@ -145,7 +145,7 @@ A <u>binary tree</u> is a non-linear data structure that represents the hierarch
}
impl TreeNode {
/* 构造方法 */
/* Constructor */
fn new(val: i32) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self {
val,
@@ -162,12 +162,12 @@ A <u>binary tree</u> is a non-linear data structure that represents the hierarch
/* Binary tree node */
typedef struct TreeNode {
int val; // Node value
int height; // 节点高度
int height; // Node height
struct TreeNode *left; // Pointer to left child node
struct TreeNode *right; // Pointer to right child node
} TreeNode;
/* 构造函数 */
/* Constructor */
TreeNode *newTreeNode(int val) {
TreeNode *node;