> {
Rc::new(RefCell::new(Self {
val,
height: 0,
left: None,
right: None
}))
}
}
```
=== "C"
```c title=""
/* AVL tree node */
typedef struct TreeNode {
int val;
int height;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
/* Constructor */
TreeNode *newTreeNode(int val) {
TreeNode *node;
node = (TreeNode *)malloc(sizeof(TreeNode));
node->val = val;
node->height = 0;
node->left = NULL;
node->right = NULL;
return node;
}
```
=== "Kotlin"
```kotlin title=""
/* AVL tree node */
class TreeNode(val _val: Int) { // Node value
val height: Int = 0 // Node height
val left: TreeNode? = null // Left child
val right: TreeNode? = null // Right child
}
```
=== "Ruby"
```ruby title=""
### AVL tree node class ###
class TreeNode
attr_accessor :val # Node value
attr_accessor :height # Node height
attr_accessor :left # Left child reference
attr_accessor :right # Right child reference
def initialize(val)
@val = val
@height = 0
end
end
```
The "node height" refers to the distance from that node to its farthest leaf node, i.e., the number of "edges" passed. It is important to note that the height of a leaf node is $0$, and the height of a null node is $-1$. We will create two utility functions for getting and updating the height of a node:
```src
[file]{avl_tree}-[class]{avl_tree}-[func]{update_height}
```
### Node Balance Factor
The balance factor of a node is defined as the height of the node's left subtree minus the height of its right subtree, and the balance factor of a null node is defined as $0$. We also encapsulate the function to obtain the node's balance factor for convenient subsequent use:
```src
[file]{avl_tree}-[class]{avl_tree}-[func]{balance_factor}
```
!!! tip
Let the balance factor be $f$, then the balance factor of any node in an AVL tree satisfies $-1 \le f \le 1$.
## Rotations in Avl Trees
The characteristic of AVL trees lies in the "rotation" operation, which can restore balance to unbalanced nodes without affecting the inorder traversal sequence of the binary tree. In other words, **rotation operations can both maintain the property of a "binary search tree" and make the tree return to a "balanced binary tree"**.
We call nodes with a balance factor absolute value $> 1$ "unbalanced nodes". Depending on the imbalance situation, rotation operations are divided into four types: right rotation, left rotation, left rotation then right rotation, and right rotation then left rotation. Below we describe these rotation operations in detail.
### Right Rotation
As shown in the figure below, the value below the node is the balance factor. From bottom to top, the first unbalanced node in the binary tree is "node 3". We focus on the subtree with this unbalanced node as the root, denoting the node as `node` and its left child as `child`, and perform a "right rotation" operation. After the right rotation is completed, the subtree regains balance and still maintains the properties of a binary search tree.
=== "<1>"

=== "<2>"

=== "<3>"

=== "<4>"

As shown in the figure below, when the `child` node has a right child (denoted as `grand_child`), a step needs to be added in the right rotation: set `grand_child` as the left child of `node`.

"Right rotation" is a figurative term; in practice, it is achieved by modifying node pointers, as shown in the following code:
```src
[file]{avl_tree}-[class]{avl_tree}-[func]{right_rotate}
```
### Left Rotation
Correspondingly, if considering the "mirror" of the above unbalanced binary tree, the "left rotation" operation shown in the figure below needs to be performed.

Similarly, as shown in the figure below, when the `child` node has a left child (denoted as `grand_child`), a step needs to be added in the left rotation: set `grand_child` as the right child of `node`.

It can be observed that **right rotation and left rotation operations are mirror symmetric in logic, and the two imbalance cases they solve are also symmetric**. Based on symmetry, we only need to replace all `left` in the right rotation implementation code with `right`, and all `right` with `left`, to obtain the left rotation implementation code:
```src
[file]{avl_tree}-[class]{avl_tree}-[func]{left_rotate}
```
### Left Rotation Then Right Rotation
For the unbalanced node 3 in the figure below, using either left rotation or right rotation alone cannot restore the subtree to balance. In this case, a "left rotation" needs to be performed on `child` first, followed by a "right rotation" on `node`.

### Right Rotation Then Left Rotation
As shown in the figure below, for the mirror case of the above unbalanced binary tree, a "right rotation" needs to be performed on `child` first, then a "left rotation" on `node`.

### Choice of Rotation
The four imbalances shown in the figure below correspond one-to-one with the above cases, requiring right rotation, left rotation then right rotation, right rotation then left rotation, and left rotation operations respectively.

As shown in the table below, we determine which case the unbalanced node belongs to by judging the signs of the balance factor of the unbalanced node and the balance factor of its taller-side child node.
Table Conditions for Choosing Among the Four Rotation Cases
| Balance factor of the unbalanced node | Balance factor of the child node | Rotation method to apply |
| -------------------------------------- | --------------------------------- | --------------------------------- |
| $> 1$ (left-leaning tree) | $\geq 0$ | Right rotation |
| $> 1$ (left-leaning tree) | $<0$ | Left rotation then right rotation |
| $< -1$ (right-leaning tree) | $\leq 0$ | Left rotation |
| $< -1$ (right-leaning tree) | $>0$ | Right rotation then left rotation |
For ease of use, we encapsulate the rotation operations into a function. **With this function, we can perform rotations for various imbalance situations, restoring balance to unbalanced nodes**. The code is as follows:
```src
[file]{avl_tree}-[class]{avl_tree}-[func]{rotate}
```
## Common Operations in Avl Trees
### Node Insertion
The node insertion operation in AVL trees is similar in principle to that in binary search trees. The only difference is that after inserting a node in an AVL tree, a series of unbalanced nodes may appear on the path from that node to the root. Therefore, **we need to start from this node and perform rotation operations from bottom to top, restoring balance to all unbalanced nodes**. The code is as follows:
```src
[file]{avl_tree}-[class]{avl_tree}-[func]{insert_helper}
```
### Node Removal
Similarly, on the basis of the binary search tree's node removal method, rotation operations need to be performed from bottom to top to restore balance to all unbalanced nodes. The code is as follows:
```src
[file]{avl_tree}-[class]{avl_tree}-[func]{remove_helper}
```
### Node Search
The node search operation in AVL trees is consistent with that in binary search trees, and will not be elaborated here.
## Typical Applications of Avl Trees
- Organizing and storing large-scale data, suitable for scenarios with high-frequency searches and low-frequency insertions and deletions.
- Used to build index systems in databases.
- Red-black trees are also a common type of balanced binary search tree. Compared to AVL trees, red-black trees have more relaxed balance conditions, require fewer rotation operations for node insertion and deletion, and have higher average efficiency for node addition and deletion operations.