Files
hello-algo/en/docs/chapter_tree/avl_tree.md
2025-12-31 19:47:59 +08:00

14 KiB

Avl Tree *

In the "Binary Search Tree" section, we mentioned that after multiple insertion and removal operations, a binary search tree may degenerate into a linked list. In this case, the time complexity of all operations degrades from O(\log n) to O(n).

As shown in the figure below, after two node removal operations, this binary search tree will degrade into a linked list.

Degradation of an AVL tree after removing nodes

For example, in the perfect binary tree shown in the figure below, after inserting two nodes, the tree will lean heavily to the left, and the time complexity of search operations will also degrade.

Degradation of an AVL tree after inserting nodes

In 1962, G. M. Adelson-Velsky and E. M. Landis proposed the AVL tree in their paper "An algorithm for the organization of information". The paper described in detail a series of operations ensuring that after continuously adding and removing nodes, the AVL tree does not degenerate, thus keeping the time complexity of various operations at the O(\log n) level. In other words, in scenarios requiring frequent insertions, deletions, searches, and modifications, the AVL tree can always maintain efficient data operation performance, making it very valuable in applications.

Common Terminology in Avl Trees

An AVL tree is both a binary search tree and a balanced binary tree, simultaneously satisfying all the properties of these two types of binary trees, hence it is a balanced binary search tree.

Node Height

Since the operations related to AVL trees require obtaining node heights, we need to add a height variable to the node class:

=== "Python"

```python title=""
class TreeNode:
    """AVL tree node"""
    def __init__(self, val: int):
        self.val: int = val                 # Node value
        self.height: int = 0                # Node height
        self.left: TreeNode | None = None   # Left child reference
        self.right: TreeNode | None = None  # Right child reference
```

=== "C++"

```cpp title=""
/* AVL tree node */
struct TreeNode {
    int val{};          // Node value
    int height = 0;     // Node height
    TreeNode *left{};   // Left child
    TreeNode *right{};  // Right child
    TreeNode() = default;
    explicit TreeNode(int x) : val(x){}
};
```

=== "Java"

```java title=""
/* AVL tree node */
class TreeNode {
    public int val;        // Node value
    public int height;     // Node height
    public TreeNode left;  // Left child
    public TreeNode right; // Right child
    public TreeNode(int x) { val = x; }
}
```

=== "C#"

```csharp title=""
/* AVL tree node */
class TreeNode(int? x) {
    public int? val = x;    // Node value
    public int height;      // Node height
    public TreeNode? left;  // Left child reference
    public TreeNode? right; // Right child reference
}
```

=== "Go"

```go title=""
/* AVL tree node */
type TreeNode struct {
    Val    int       // Node value
    Height int       // Node height
    Left   *TreeNode // Left child reference
    Right  *TreeNode // Right child reference
}
```

=== "Swift"

```swift title=""
/* AVL tree node */
class TreeNode {
    var val: Int // Node value
    var height: Int // Node height
    var left: TreeNode? // Left child
    var right: TreeNode? // Right child

    init(x: Int) {
        val = x
        height = 0
    }
}
```

=== "JS"

```javascript title=""
/* AVL tree node */
class TreeNode {
    val; // Node value
    height; // Node height
    left; // Left child pointer
    right; // Right child pointer
    constructor(val, left, right, height) {
        this.val = val === undefined ? 0 : val;
        this.height = height === undefined ? 0 : height;
        this.left = left === undefined ? null : left;
        this.right = right === undefined ? null : right;
    }
}
```

=== "TS"

```typescript title=""
/* AVL tree node */
class TreeNode {
    val: number;            // Node value
    height: number;         // Node height
    left: TreeNode | null;  // Left child pointer
    right: TreeNode | null; // Right child pointer
    constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {
        this.val = val === undefined ? 0 : val;
        this.height = height === undefined ? 0 : height; 
        this.left = left === undefined ? null : left; 
        this.right = right === undefined ? null : right; 
    }
}
```

=== "Dart"

```dart title=""
/* AVL tree node */
class TreeNode {
  int val;         // Node value
  int height;      // Node height
  TreeNode? left;  // Left child
  TreeNode? right; // Right child
  TreeNode(this.val, [this.height = 0, this.left, this.right]);
}
```

=== "Rust"

```rust title=""
use std::rc::Rc;
use std::cell::RefCell;

/* AVL tree node */
struct TreeNode {
    val: i32,                               // Node value
    height: i32,                            // Node height
    left: Option<Rc<RefCell<TreeNode>>>,    // Left child
    right: Option<Rc<RefCell<TreeNode>>>,   // Right child
}

impl TreeNode {
    /* Constructor */
    fn new(val: i32) -> Rc<RefCell<Self>> {
        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:

[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:

[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>" Steps of right rotation

=== "<2>" avltree_right_rotate_step2

=== "<3>" avltree_right_rotate_step3

=== "<4>" avltree_right_rotate_step4

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 with grand_child

"Right rotation" is a figurative term; in practice, it is achieved by modifying node pointers, as shown in the following code:

[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.

Left rotation operation

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.

Left rotation with grand_child

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:

[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.

Left-right rotation

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.

Right-left rotation

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.

The four rotation cases of AVL tree

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:

[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:

[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:

[file]{avl_tree}-[class]{avl_tree}-[func]{remove_helper}

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.