You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hello-algo/en/docs/chapter_tree/avl_tree.md

1379 lines
38 KiB

8 months ago
---
comments: true
---
# 7.5   AVL tree *
In the "Binary Search Tree" section, we mentioned that after multiple insertions and removals, a binary search tree might degrade to a linked list. In such cases, the time complexity of all operations degrades from $O(\log n)$ to $O(n)$.
7 months ago
As shown in Figure 7-24, after two node removal operations, this binary search tree will degrade into a linked list.
8 months ago
![Degradation of an AVL tree after removing nodes](avl_tree.assets/avltree_degradation_from_removing_node.png){ class="animation-figure" }
<p align="center"> Figure 7-24 &nbsp; Degradation of an AVL tree after removing nodes </p>
7 months ago
For example, in the perfect binary tree shown in Figure 7-25, after inserting two nodes, the tree will lean heavily to the left, and the time complexity of search operations will also degrade.
8 months ago
![Degradation of an AVL tree after inserting nodes](avl_tree.assets/avltree_degradation_from_inserting_node.png){ class="animation-figure" }
<p align="center"> Figure 7-25 &nbsp; Degradation of an AVL tree after inserting nodes </p>
7 months ago
In 1962, G. M. Adelson-Velsky and E. M. Landis proposed the <u>AVL Tree</u> in their paper "An algorithm for the organization of information". The paper detailed a series of operations to ensure that after continuously adding and removing nodes, the AVL tree would not degrade, thus maintaining the time complexity of various operations at $O(\log n)$ level. In other words, in scenarios where frequent additions, removals, searches, and modifications are needed, the AVL tree can always maintain efficient data operation performance, which has great application value.
8 months ago
## 7.5.1 &nbsp; Common terminology in AVL trees
7 months ago
An AVL tree is both a binary search tree and a balanced binary tree, satisfying all properties of these two types of binary trees, hence it is a <u>balanced binary search tree</u>.
8 months ago
### 1. &nbsp; 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 */
TreeNode 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=""
```
=== "Zig"
```zig title=""
```
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:
=== "Python"
```python title="avl_tree.py"
def height(self, node: TreeNode | None) -> int:
7 months ago
"""Get node height"""
# Empty node height is -1, leaf node height is 0
8 months ago
if node is not None:
return node.height
return -1
def update_height(self, node: TreeNode | None):
7 months ago
"""Update node height"""
# Node height equals the height of the tallest subtree + 1
8 months ago
node.height = max([self.height(node.left), self.height(node.right)]) + 1
```
=== "C++"
```cpp title="avl_tree.cpp"
6 months ago
/* Get node height */
int height(TreeNode *node) {
// Empty node height is -1, leaf node height is 0
return node == nullptr ? -1 : node->height;
}
8 months ago
6 months ago
/* Update node height */
void updateHeight(TreeNode *node) {
// Node height equals the height of the tallest subtree + 1
node->height = max(height(node->left), height(node->right)) + 1;
}
8 months ago
```
=== "Java"
```java title="avl_tree.java"
7 months ago
/* Get node height */
8 months ago
int height(TreeNode node) {
7 months ago
// Empty node height is -1, leaf node height is 0
8 months ago
return node == null ? -1 : node.height;
}
7 months ago
/* Update node height */
8 months ago
void updateHeight(TreeNode node) {
7 months ago
// Node height equals the height of the tallest subtree + 1
8 months ago
node.height = Math.max(height(node.left), height(node.right)) + 1;
}
```
=== "C#"
```csharp title="avl_tree.cs"
7 months ago
[class]{AVLTree}-[func]{Height}
8 months ago
7 months ago
[class]{AVLTree}-[func]{UpdateHeight}
8 months ago
```
=== "Go"
```go title="avl_tree.go"
7 months ago
[class]{aVLTree}-[func]{height}
8 months ago
7 months ago
[class]{aVLTree}-[func]{updateHeight}
8 months ago
```
=== "Swift"
```swift title="avl_tree.swift"
7 months ago
[class]{AVLTree}-[func]{height}
8 months ago
7 months ago
[class]{AVLTree}-[func]{updateHeight}
8 months ago
```
=== "JS"
```javascript title="avl_tree.js"
7 months ago
[class]{AVLTree}-[func]{height}
8 months ago
7 months ago
[class]{AVLTree}-[func]{updateHeight}
8 months ago
```
=== "TS"
```typescript title="avl_tree.ts"
7 months ago
[class]{AVLTree}-[func]{height}
8 months ago
7 months ago
[class]{AVLTree}-[func]{updateHeight}
8 months ago
```
=== "Dart"
```dart title="avl_tree.dart"
7 months ago
[class]{AVLTree}-[func]{height}
8 months ago
7 months ago
[class]{AVLTree}-[func]{updateHeight}
8 months ago
```
=== "Rust"
```rust title="avl_tree.rs"
7 months ago
[class]{AVLTree}-[func]{height}
8 months ago
7 months ago
[class]{AVLTree}-[func]{update_height}
8 months ago
```
=== "C"
```c title="avl_tree.c"
7 months ago
[class]{}-[func]{height}
8 months ago
7 months ago
[class]{}-[func]{updateHeight}
8 months ago
```
=== "Kotlin"
```kotlin title="avl_tree.kt"
7 months ago
[class]{AVLTree}-[func]{height}
8 months ago
7 months ago
[class]{AVLTree}-[func]{updateHeight}
8 months ago
```
=== "Ruby"
```ruby title="avl_tree.rb"
7 months ago
[class]{AVLTree}-[func]{height}
8 months ago
7 months ago
[class]{AVLTree}-[func]{update_height}
8 months ago
```
=== "Zig"
```zig title="avl_tree.zig"
7 months ago
[class]{AVLTree}-[func]{height}
8 months ago
7 months ago
[class]{AVLTree}-[func]{updateHeight}
8 months ago
```
### 2. &nbsp; Node balance factor
7 months ago
The <u>balance factor</u> of a node is defined as the height of the node's left subtree minus the height of its right subtree, with the balance factor of a null node defined as $0$. We will also encapsulate the functionality of obtaining the node balance factor into a function for easy use later on:
8 months ago
=== "Python"
```python title="avl_tree.py"
def balance_factor(self, node: TreeNode | None) -> int:
7 months ago
"""Get balance factor"""
# Empty node balance factor is 0
8 months ago
if node is None:
return 0
7 months ago
# Node balance factor = left subtree height - right subtree height
8 months ago
return self.height(node.left) - self.height(node.right)
```
=== "C++"
```cpp title="avl_tree.cpp"
6 months ago
/* Get balance factor */
int balanceFactor(TreeNode *node) {
// Empty node balance factor is 0
if (node == nullptr)
return 0;
// Node balance factor = left subtree height - right subtree height
return height(node->left) - height(node->right);
}
8 months ago
```
=== "Java"
```java title="avl_tree.java"
7 months ago
/* Get balance factor */
8 months ago
int balanceFactor(TreeNode node) {
7 months ago
// Empty node balance factor is 0
8 months ago
if (node == null)
return 0;
7 months ago
// Node balance factor = left subtree height - right subtree height
8 months ago
return height(node.left) - height(node.right);
}
```
=== "C#"
```csharp title="avl_tree.cs"
7 months ago
[class]{AVLTree}-[func]{BalanceFactor}
8 months ago
```
=== "Go"
```go title="avl_tree.go"
7 months ago
[class]{aVLTree}-[func]{balanceFactor}
8 months ago
```
=== "Swift"
```swift title="avl_tree.swift"
7 months ago
[class]{AVLTree}-[func]{balanceFactor}
8 months ago
```
=== "JS"
```javascript title="avl_tree.js"
7 months ago
[class]{AVLTree}-[func]{balanceFactor}
8 months ago
```
=== "TS"
```typescript title="avl_tree.ts"
7 months ago
[class]{AVLTree}-[func]{balanceFactor}
8 months ago
```
=== "Dart"
```dart title="avl_tree.dart"
7 months ago
[class]{AVLTree}-[func]{balanceFactor}
8 months ago
```
=== "Rust"
```rust title="avl_tree.rs"
7 months ago
[class]{AVLTree}-[func]{balance_factor}
8 months ago
```
=== "C"
```c title="avl_tree.c"
7 months ago
[class]{}-[func]{balanceFactor}
8 months ago
```
=== "Kotlin"
```kotlin title="avl_tree.kt"
7 months ago
[class]{AVLTree}-[func]{balanceFactor}
8 months ago
```
=== "Ruby"
```ruby title="avl_tree.rb"
7 months ago
[class]{AVLTree}-[func]{balance_factor}
8 months ago
```
=== "Zig"
```zig title="avl_tree.zig"
7 months ago
[class]{AVLTree}-[func]{balanceFactor}
8 months ago
```
7 months ago
!!! tip
8 months ago
Let the balance factor be $f$, then the balance factor of any node in an AVL tree satisfies $-1 \le f \le 1$.
## 7.5.2 &nbsp; Rotations in AVL trees
The characteristic feature of an AVL tree is the "rotation" operation, which can restore balance to an unbalanced node without affecting the in-order traversal sequence of the binary tree. In other words, **the rotation operation can maintain the property of a "binary search tree" while also turning the tree back into a "balanced binary tree"**.
We call nodes with an absolute balance factor $> 1$ "unbalanced nodes". Depending on the type of imbalance, there are four kinds of rotations: right rotation, left rotation, right-left rotation, and left-right rotation. Below, we detail these rotation operations.
### 1. &nbsp; Right rotation
7 months ago
As shown in Figure 7-26, the first unbalanced node from the bottom up in the binary tree is "node 3". Focusing on the subtree with this unbalanced node as the root, denoted as `node`, and its left child as `child`, perform a "right rotation". After the right rotation, the subtree is balanced again while still maintaining the properties of a binary search tree.
8 months ago
=== "<1>"
![Steps of right rotation](avl_tree.assets/avltree_right_rotate_step1.png){ class="animation-figure" }
=== "<2>"
![avltree_right_rotate_step2](avl_tree.assets/avltree_right_rotate_step2.png){ class="animation-figure" }
=== "<3>"
![avltree_right_rotate_step3](avl_tree.assets/avltree_right_rotate_step3.png){ class="animation-figure" }
=== "<4>"
![avltree_right_rotate_step4](avl_tree.assets/avltree_right_rotate_step4.png){ class="animation-figure" }
<p align="center"> Figure 7-26 &nbsp; Steps of right rotation </p>
7 months ago
As shown in Figure 7-27, 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`.
8 months ago
![Right rotation with grand_child](avl_tree.assets/avltree_right_rotate_with_grandchild.png){ class="animation-figure" }
<p align="center"> Figure 7-27 &nbsp; Right rotation with grand_child </p>
"Right rotation" is a figurative term; in practice, it is achieved by modifying node pointers, as shown in the following code:
=== "Python"
```python title="avl_tree.py"
def right_rotate(self, node: TreeNode | None) -> TreeNode | None:
7 months ago
"""Right rotation operation"""
8 months ago
child = node.left
grand_child = child.right
7 months ago
# Rotate node to the right around child
8 months ago
child.right = node
node.left = grand_child
7 months ago
# Update node height
8 months ago
self.update_height(node)
self.update_height(child)
7 months ago
# Return the root of the subtree after rotation
8 months ago
return child
```
=== "C++"
```cpp title="avl_tree.cpp"
6 months ago
/* Right rotation operation */
TreeNode *rightRotate(TreeNode *node) {
TreeNode *child = node->left;
TreeNode *grandChild = child->right;
// Rotate node to the right around child
child->right = node;
node->left = grandChild;
// Update node height
updateHeight(node);
updateHeight(child);
// Return the root of the subtree after rotation
return child;
}
8 months ago
```
=== "Java"
```java title="avl_tree.java"
7 months ago
/* Right rotation operation */
8 months ago
TreeNode rightRotate(TreeNode node) {
TreeNode child = node.left;
TreeNode grandChild = child.right;
7 months ago
// Rotate node to the right around child
8 months ago
child.right = node;
node.left = grandChild;
7 months ago
// Update node height
8 months ago
updateHeight(node);
updateHeight(child);
7 months ago
// Return the root of the subtree after rotation
8 months ago
return child;
}
```
=== "C#"
```csharp title="avl_tree.cs"
7 months ago
[class]{AVLTree}-[func]{RightRotate}
8 months ago
```
=== "Go"
```go title="avl_tree.go"
7 months ago
[class]{aVLTree}-[func]{rightRotate}
8 months ago
```
=== "Swift"
```swift title="avl_tree.swift"
7 months ago
[class]{AVLTree}-[func]{rightRotate}
8 months ago
```
=== "JS"
```javascript title="avl_tree.js"
7 months ago
[class]{AVLTree}-[func]{rightRotate}
8 months ago
```
=== "TS"
```typescript title="avl_tree.ts"
7 months ago
[class]{AVLTree}-[func]{rightRotate}
8 months ago
```
=== "Dart"
```dart title="avl_tree.dart"
7 months ago
[class]{AVLTree}-[func]{rightRotate}
8 months ago
```
=== "Rust"
```rust title="avl_tree.rs"
7 months ago
[class]{AVLTree}-[func]{right_rotate}
8 months ago
```
=== "C"
```c title="avl_tree.c"
7 months ago
[class]{}-[func]{rightRotate}
8 months ago
```
=== "Kotlin"
```kotlin title="avl_tree.kt"
7 months ago
[class]{AVLTree}-[func]{rightRotate}
8 months ago
```
=== "Ruby"
```ruby title="avl_tree.rb"
7 months ago
[class]{AVLTree}-[func]{right_rotate}
8 months ago
```
=== "Zig"
```zig title="avl_tree.zig"
7 months ago
[class]{AVLTree}-[func]{rightRotate}
8 months ago
```
### 2. &nbsp; Left rotation
7 months ago
Correspondingly, if considering the "mirror" of the above unbalanced binary tree, the "left rotation" operation shown in Figure 7-28 needs to be performed.
8 months ago
![Left rotation operation](avl_tree.assets/avltree_left_rotate.png){ class="animation-figure" }
<p align="center"> Figure 7-28 &nbsp; Left rotation operation </p>
7 months ago
Similarly, as shown in Figure 7-29, 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`.
8 months ago
![Left rotation with grand_child](avl_tree.assets/avltree_left_rotate_with_grandchild.png){ class="animation-figure" }
<p align="center"> Figure 7-29 &nbsp; Left rotation with grand_child </p>
It can be observed that **the right and left rotation operations are logically symmetrical, and they solve two symmetrical types of imbalance**. Based on symmetry, by replacing all `left` with `right`, and all `right` with `left` in the implementation code of right rotation, we can get the implementation code for left rotation:
=== "Python"
```python title="avl_tree.py"
def left_rotate(self, node: TreeNode | None) -> TreeNode | None:
7 months ago
"""Left rotation operation"""
8 months ago
child = node.right
grand_child = child.left
7 months ago
# Rotate node to the left around child
8 months ago
child.left = node
node.right = grand_child
7 months ago
# Update node height
8 months ago
self.update_height(node)
self.update_height(child)
7 months ago
# Return the root of the subtree after rotation
8 months ago
return child
```
=== "C++"
```cpp title="avl_tree.cpp"
6 months ago
/* Left rotation operation */
TreeNode *leftRotate(TreeNode *node) {
TreeNode *child = node->right;
TreeNode *grandChild = child->left;
// Rotate node to the left around child
child->left = node;
node->right = grandChild;
// Update node height
updateHeight(node);
updateHeight(child);
// Return the root of the subtree after rotation
return child;
}
8 months ago
```
=== "Java"
```java title="avl_tree.java"
7 months ago
/* Left rotation operation */
8 months ago
TreeNode leftRotate(TreeNode node) {
TreeNode child = node.right;
TreeNode grandChild = child.left;
7 months ago
// Rotate node to the left around child
8 months ago
child.left = node;
node.right = grandChild;
7 months ago
// Update node height
8 months ago
updateHeight(node);
updateHeight(child);
7 months ago
// Return the root of the subtree after rotation
8 months ago
return child;
}
```
=== "C#"
```csharp title="avl_tree.cs"
7 months ago
[class]{AVLTree}-[func]{LeftRotate}
8 months ago
```
=== "Go"
```go title="avl_tree.go"
7 months ago
[class]{aVLTree}-[func]{leftRotate}
8 months ago
```
=== "Swift"
```swift title="avl_tree.swift"
7 months ago
[class]{AVLTree}-[func]{leftRotate}
8 months ago
```
=== "JS"
```javascript title="avl_tree.js"
7 months ago
[class]{AVLTree}-[func]{leftRotate}
8 months ago
```
=== "TS"
```typescript title="avl_tree.ts"
7 months ago
[class]{AVLTree}-[func]{leftRotate}
8 months ago
```
=== "Dart"
```dart title="avl_tree.dart"
7 months ago
[class]{AVLTree}-[func]{leftRotate}
8 months ago
```
=== "Rust"
```rust title="avl_tree.rs"
7 months ago
[class]{AVLTree}-[func]{left_rotate}
8 months ago
```
=== "C"
```c title="avl_tree.c"
7 months ago
[class]{}-[func]{leftRotate}
8 months ago
```
=== "Kotlin"
```kotlin title="avl_tree.kt"
7 months ago
[class]{AVLTree}-[func]{leftRotate}
8 months ago
```
=== "Ruby"
```ruby title="avl_tree.rb"
7 months ago
[class]{AVLTree}-[func]{left_rotate}
8 months ago
```
=== "Zig"
```zig title="avl_tree.zig"
7 months ago
[class]{AVLTree}-[func]{leftRotate}
8 months ago
```
### 3. &nbsp; Right-left rotation
7 months ago
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`.
8 months ago
![Right-left rotation](avl_tree.assets/avltree_left_right_rotate.png){ class="animation-figure" }
<p align="center"> Figure 7-30 &nbsp; Right-left rotation </p>
### 4. &nbsp; Left-right rotation
7 months ago
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`.
8 months ago
![Left-right rotation](avl_tree.assets/avltree_right_left_rotate.png){ class="animation-figure" }
<p align="center"> Figure 7-31 &nbsp; Left-right rotation </p>
### 5. &nbsp; Choice of rotation
7 months ago
The four kinds of imbalances shown in Figure 7-32 correspond to the cases described above, respectively requiring right rotation, left-right rotation, right-left rotation, and left rotation.
8 months ago
![The four rotation cases of AVL tree](avl_tree.assets/avltree_rotation_cases.png){ class="animation-figure" }
<p align="center"> Figure 7-32 &nbsp; The four rotation cases of AVL tree </p>
7 months ago
As shown in Table 7-3, we determine which of the above cases an unbalanced node belongs to by judging the sign of the balance factor of the unbalanced node and its higher-side child's balance factor.
8 months ago
<p align="center"> Table 7-3 &nbsp; Conditions for Choosing Among the Four Rotation Cases </p>
<div class="center-table" markdown>
| Balance factor of unbalanced node | Balance factor of child node | Rotation method to use |
| --------------------------------- | ---------------------------- | --------------------------------- |
| $> 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 |
</div>
For convenience, we encapsulate the rotation operations into a function. **With this function, we can perform rotations on various kinds of imbalances, restoring balance to unbalanced nodes**. The code is as follows:
=== "Python"
```python title="avl_tree.py"
def rotate(self, node: TreeNode | None) -> TreeNode | None:
7 months ago
"""Perform rotation operation to restore balance to the subtree"""
# Get the balance factor of node
8 months ago
balance_factor = self.balance_factor(node)
7 months ago
# Left-leaning tree
8 months ago
if balance_factor > 1:
if self.balance_factor(node.left) >= 0:
7 months ago
# Right rotation
8 months ago
return self.right_rotate(node)
else:
7 months ago
# First left rotation then right rotation
8 months ago
node.left = self.left_rotate(node.left)
return self.right_rotate(node)
7 months ago
# Right-leaning tree
8 months ago
elif balance_factor < -1:
if self.balance_factor(node.right) <= 0:
7 months ago
# Left rotation
8 months ago
return self.left_rotate(node)
else:
7 months ago
# First right rotation then left rotation
8 months ago
node.right = self.right_rotate(node.right)
return self.left_rotate(node)
7 months ago
# Balanced tree, no rotation needed, return
8 months ago
return node
```
=== "C++"
```cpp title="avl_tree.cpp"
6 months ago
/* Perform rotation operation to restore balance to the subtree */
TreeNode *rotate(TreeNode *node) {
// Get the balance factor of node
int _balanceFactor = balanceFactor(node);
// Left-leaning tree
if (_balanceFactor > 1) {
if (balanceFactor(node->left) >= 0) {
// Right rotation
return rightRotate(node);
} else {
// First left rotation then right rotation
node->left = leftRotate(node->left);
return rightRotate(node);
}
}
// Right-leaning tree
if (_balanceFactor < -1) {
if (balanceFactor(node->right) <= 0) {
// Left rotation
return leftRotate(node);
} else {
// First right rotation then left rotation
node->right = rightRotate(node->right);
return leftRotate(node);
}
}
// Balanced tree, no rotation needed, return
return node;
}
8 months ago
```
=== "Java"
```java title="avl_tree.java"
7 months ago
/* Perform rotation operation to restore balance to the subtree */
8 months ago
TreeNode rotate(TreeNode node) {
7 months ago
// Get the balance factor of node
8 months ago
int balanceFactor = balanceFactor(node);
7 months ago
// Left-leaning tree
8 months ago
if (balanceFactor > 1) {
if (balanceFactor(node.left) >= 0) {
7 months ago
// Right rotation
8 months ago
return rightRotate(node);
} else {
7 months ago
// First left rotation then right rotation
8 months ago
node.left = leftRotate(node.left);
return rightRotate(node);
}
}
7 months ago
// Right-leaning tree
8 months ago
if (balanceFactor < -1) {
if (balanceFactor(node.right) <= 0) {
7 months ago
// Left rotation
8 months ago
return leftRotate(node);
} else {
7 months ago
// First right rotation then left rotation
8 months ago
node.right = rightRotate(node.right);
return leftRotate(node);
}
}
7 months ago
// Balanced tree, no rotation needed, return
8 months ago
return node;
}
```
=== "C#"
```csharp title="avl_tree.cs"
7 months ago
[class]{AVLTree}-[func]{Rotate}
8 months ago
```
=== "Go"
```go title="avl_tree.go"
7 months ago
[class]{aVLTree}-[func]{rotate}
8 months ago
```
=== "Swift"
```swift title="avl_tree.swift"
7 months ago
[class]{AVLTree}-[func]{rotate}
8 months ago
```
=== "JS"
```javascript title="avl_tree.js"
7 months ago
[class]{AVLTree}-[func]{rotate}
8 months ago
```
=== "TS"
```typescript title="avl_tree.ts"
7 months ago
[class]{AVLTree}-[func]{rotate}
8 months ago
```
=== "Dart"
```dart title="avl_tree.dart"
7 months ago
[class]{AVLTree}-[func]{rotate}
8 months ago
```
=== "Rust"
```rust title="avl_tree.rs"
7 months ago
[class]{AVLTree}-[func]{rotate}
8 months ago
```
=== "C"
```c title="avl_tree.c"
7 months ago
[class]{}-[func]{rotate}
8 months ago
```
=== "Kotlin"
```kotlin title="avl_tree.kt"
7 months ago
[class]{AVLTree}-[func]{rotate}
8 months ago
```
=== "Ruby"
```ruby title="avl_tree.rb"
7 months ago
[class]{AVLTree}-[func]{rotate}
8 months ago
```
=== "Zig"
```zig title="avl_tree.zig"
7 months ago
[class]{AVLTree}-[func]{rotate}
8 months ago
```
## 7.5.3 &nbsp; Common operations in AVL trees
### 1. &nbsp; Node insertion
The node insertion operation in AVL trees is similar 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 along the path from that node to the root node. Therefore, **we need to start from this node and perform rotation operations upwards to restore balance to all unbalanced nodes**. The code is as follows:
=== "Python"
```python title="avl_tree.py"
def insert(self, val):
7 months ago
"""Insert node"""
8 months ago
self._root = self.insert_helper(self._root, val)
def insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:
7 months ago
"""Recursively insert node (helper method)"""
8 months ago
if node is None:
return TreeNode(val)
7 months ago
# 1. Find insertion position and insert node
8 months ago
if val < node.val:
node.left = self.insert_helper(node.left, val)
elif val > node.val:
node.right = self.insert_helper(node.right, val)
else:
7 months ago
# Do not insert duplicate nodes, return
8 months ago
return node
7 months ago
# Update node height
8 months ago
self.update_height(node)
7 months ago
# 2. Perform rotation operation to restore balance to the subtree
8 months ago
return self.rotate(node)
```
=== "C++"
```cpp title="avl_tree.cpp"
6 months ago
/* Insert node */
void insert(int val) {
root = insertHelper(root, val);
}
8 months ago
6 months ago
/* Recursively insert node (helper method) */
TreeNode *insertHelper(TreeNode *node, int val) {
if (node == nullptr)
return new TreeNode(val);
/* 1. Find insertion position and insert node */
if (val < node->val)
node->left = insertHelper(node->left, val);
else if (val > node->val)
node->right = insertHelper(node->right, val);
else
return node; // Do not insert duplicate nodes, return
updateHeight(node); // Update node height
/* 2. Perform rotation operation to restore balance to the subtree */
node = rotate(node);
// Return the root node of the subtree
return node;
}
8 months ago
```
=== "Java"
```java title="avl_tree.java"
7 months ago
/* Insert node */
8 months ago
void insert(int val) {
root = insertHelper(root, val);
}
7 months ago
/* Recursively insert node (helper method) */
8 months ago
TreeNode insertHelper(TreeNode node, int val) {
if (node == null)
return new TreeNode(val);
7 months ago
/* 1. Find insertion position and insert node */
8 months ago
if (val < node.val)
node.left = insertHelper(node.left, val);
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
7 months ago
return node; // Do not insert duplicate nodes, return
updateHeight(node); // Update node height
/* 2. Perform rotation operation to restore balance to the subtree */
8 months ago
node = rotate(node);
7 months ago
// Return the root node of the subtree
8 months ago
return node;
}
```
=== "C#"
```csharp title="avl_tree.cs"
7 months ago
[class]{AVLTree}-[func]{Insert}
8 months ago
7 months ago
[class]{AVLTree}-[func]{InsertHelper}
8 months ago
```
=== "Go"
```go title="avl_tree.go"
7 months ago
[class]{aVLTree}-[func]{insert}
8 months ago
7 months ago
[class]{aVLTree}-[func]{insertHelper}
8 months ago
```
=== "Swift"
```swift title="avl_tree.swift"
7 months ago
[class]{AVLTree}-[func]{insert}
8 months ago
7 months ago
[class]{AVLTree}-[func]{insertHelper}
8 months ago
```
=== "JS"
```javascript title="avl_tree.js"
7 months ago
[class]{AVLTree}-[func]{insert}
8 months ago
7 months ago
[class]{AVLTree}-[func]{insertHelper}
8 months ago
```
=== "TS"
```typescript title="avl_tree.ts"
7 months ago
[class]{AVLTree}-[func]{insert}
8 months ago
7 months ago
[class]{AVLTree}-[func]{insertHelper}
8 months ago
```
=== "Dart"
```dart title="avl_tree.dart"
7 months ago
[class]{AVLTree}-[func]{insert}
8 months ago
7 months ago
[class]{AVLTree}-[func]{insertHelper}
8 months ago
```
=== "Rust"
```rust title="avl_tree.rs"
7 months ago
[class]{AVLTree}-[func]{insert}
8 months ago
7 months ago
[class]{AVLTree}-[func]{insert_helper}
8 months ago
```
=== "C"
```c title="avl_tree.c"
7 months ago
[class]{AVLTree}-[func]{insert}
8 months ago
7 months ago
[class]{}-[func]{insertHelper}
8 months ago
```
=== "Kotlin"
```kotlin title="avl_tree.kt"
7 months ago
[class]{AVLTree}-[func]{insert}
8 months ago
7 months ago
[class]{AVLTree}-[func]{insertHelper}
8 months ago
```
=== "Ruby"
```ruby title="avl_tree.rb"
7 months ago
[class]{AVLTree}-[func]{insert}
[class]{AVLTree}-[func]{insert_helper}
8 months ago
```
=== "Zig"
```zig title="avl_tree.zig"
7 months ago
[class]{AVLTree}-[func]{insert}
8 months ago
7 months ago
[class]{AVLTree}-[func]{insertHelper}
8 months ago
```
### 2. &nbsp; Node removal
Similarly, based on the method of removing nodes in binary search trees, rotation operations need to be performed from the bottom up to restore balance to all unbalanced nodes. The code is as follows:
=== "Python"
```python title="avl_tree.py"
def remove(self, val: int):
7 months ago
"""Remove node"""
8 months ago
self._root = self.remove_helper(self._root, val)
def remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:
7 months ago
"""Recursively remove node (helper method)"""
8 months ago
if node is None:
return None
7 months ago
# 1. Find and remove the node
8 months ago
if val < node.val:
node.left = self.remove_helper(node.left, val)
elif val > node.val:
node.right = self.remove_helper(node.right, val)
else:
if node.left is None or node.right is None:
child = node.left or node.right
7 months ago
# Number of child nodes = 0, remove node and return
8 months ago
if child is None:
return None
7 months ago
# Number of child nodes = 1, remove node
8 months ago
else:
node = child
else:
7 months ago
# Number of child nodes = 2, remove the next node in in-order traversal and replace the current node with it
8 months ago
temp = node.right
while temp.left is not None:
temp = temp.left
node.right = self.remove_helper(node.right, temp.val)
node.val = temp.val
7 months ago
# Update node height
8 months ago
self.update_height(node)
7 months ago
# 2. Perform rotation operation to restore balance to the subtree
8 months ago
return self.rotate(node)
```
=== "C++"
```cpp title="avl_tree.cpp"
6 months ago
/* Remove node */
void remove(int val) {
root = removeHelper(root, val);
}
8 months ago
6 months ago
/* Recursively remove node (helper method) */
TreeNode *removeHelper(TreeNode *node, int val) {
if (node == nullptr)
return nullptr;
/* 1. Find and remove the node */
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 == nullptr || node->right == nullptr) {
TreeNode *child = node->left != nullptr ? node->left : node->right;
// Number of child nodes = 0, remove node and return
if (child == nullptr) {
delete node;
return nullptr;
}
// Number of child nodes = 1, remove node
else {
delete node;
node = child;
}
} else {
// Number of child nodes = 2, remove the next node in in-order traversal and replace the current node with it
TreeNode *temp = node->right;
while (temp->left != nullptr) {
temp = temp->left;
}
int tempVal = temp->val;
node->right = removeHelper(node->right, temp->val);
node->val = tempVal;
}
}
updateHeight(node); // Update node height
/* 2. Perform rotation operation to restore balance to the subtree */
node = rotate(node);
// Return the root node of the subtree
return node;
}
8 months ago
```
=== "Java"
```java title="avl_tree.java"
7 months ago
/* Remove node */
8 months ago
void remove(int val) {
root = removeHelper(root, val);
}
7 months ago
/* Recursively remove node (helper method) */
8 months ago
TreeNode removeHelper(TreeNode node, int val) {
if (node == null)
return null;
7 months ago
/* 1. Find and remove the node */
8 months ago
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) {
TreeNode child = node.left != null ? node.left : node.right;
7 months ago
// Number of child nodes = 0, remove node and return
8 months ago
if (child == null)
return null;
7 months ago
// Number of child nodes = 1, remove node
8 months ago
else
node = child;
} else {
7 months ago
// Number of child nodes = 2, remove the next node in in-order traversal and replace the current node with it
8 months ago
TreeNode temp = node.right;
while (temp.left != null) {
temp = temp.left;
}
node.right = removeHelper(node.right, temp.val);
node.val = temp.val;
}
}
7 months ago
updateHeight(node); // Update node height
/* 2. Perform rotation operation to restore balance to the subtree */
8 months ago
node = rotate(node);
7 months ago
// Return the root node of the subtree
8 months ago
return node;
}
```
=== "C#"
```csharp title="avl_tree.cs"
7 months ago
[class]{AVLTree}-[func]{Remove}
8 months ago
7 months ago
[class]{AVLTree}-[func]{RemoveHelper}
8 months ago
```
=== "Go"
```go title="avl_tree.go"
7 months ago
[class]{aVLTree}-[func]{remove}
8 months ago
7 months ago
[class]{aVLTree}-[func]{removeHelper}
8 months ago
```
=== "Swift"
```swift title="avl_tree.swift"
7 months ago
[class]{AVLTree}-[func]{remove}
8 months ago
7 months ago
[class]{AVLTree}-[func]{removeHelper}
8 months ago
```
=== "JS"
```javascript title="avl_tree.js"
7 months ago
[class]{AVLTree}-[func]{remove}
8 months ago
7 months ago
[class]{AVLTree}-[func]{removeHelper}
8 months ago
```
=== "TS"
```typescript title="avl_tree.ts"
7 months ago
[class]{AVLTree}-[func]{remove}
8 months ago
7 months ago
[class]{AVLTree}-[func]{removeHelper}
8 months ago
```
=== "Dart"
```dart title="avl_tree.dart"
7 months ago
[class]{AVLTree}-[func]{remove}
8 months ago
7 months ago
[class]{AVLTree}-[func]{removeHelper}
8 months ago
```
=== "Rust"
```rust title="avl_tree.rs"
7 months ago
[class]{AVLTree}-[func]{remove}
8 months ago
7 months ago
[class]{AVLTree}-[func]{remove_helper}
8 months ago
```
=== "C"
```c title="avl_tree.c"
7 months ago
[class]{AVLTree}-[func]{removeItem}
8 months ago
7 months ago
[class]{}-[func]{removeHelper}
8 months ago
```
=== "Kotlin"
```kotlin title="avl_tree.kt"
7 months ago
[class]{AVLTree}-[func]{remove}
8 months ago
7 months ago
[class]{AVLTree}-[func]{removeHelper}
8 months ago
```
=== "Ruby"
```ruby title="avl_tree.rb"
7 months ago
[class]{AVLTree}-[func]{remove}
[class]{AVLTree}-[func]{remove_helper}
8 months ago
```
=== "Zig"
```zig title="avl_tree.zig"
7 months ago
[class]{AVLTree}-[func]{remove}
8 months ago
7 months ago
[class]{AVLTree}-[func]{removeHelper}
8 months ago
```
### 3. &nbsp; Node search
The node search operation in AVL trees is consistent with that in binary search trees and will not be detailed here.
## 7.5.4 &nbsp; Typical applications of AVL trees
- Organizing and storing large amounts of data, suitable for scenarios with high-frequency searches and low-frequency intertions and removals.
- 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 balancing conditions, require fewer rotations for node insertion and removal, and have a higher average efficiency for node addition and removal operations.