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)\).
As shown in Figure 7-24, after two node removal operations, this binary search tree will degrade into a linked list.
Figure 7-24 Degradation of an AVL tree after removing nodes
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.
Figure 7-25 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 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.
7.5.1 Common terminology in AVL trees¶
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 balanced binary search tree.
1. Node height¶
Since the operations related to AVL trees require obtaining node heights, we need to add a height
variable to the node class:
/* 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;
}
}
/* 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;
}
}
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
}))
}
}
/* 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;
}
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:
def height(self, node: TreeNode | None) -> int:
"""Get node height"""
# Empty node height is -1, leaf node height is 0
if node is not None:
return node.height
return -1
def update_height(self, node: TreeNode | None):
"""Update node height"""
# Node height equals the height of the tallest subtree + 1
node.height = max([self.height(node.left), self.height(node.right)]) + 1
/* Get node height */
int height(TreeNode *node) {
// Empty node height is -1, leaf node height is 0
return node == nullptr ? -1 : node->height;
}
/* 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;
}
/* Get node height */
int height(TreeNode node) {
// Empty node height is -1, leaf node height is 0
return node == null ? -1 : node.height;
}
/* Update node height */
void updateHeight(TreeNode node) {
// Node height equals the height of the tallest subtree + 1
node.height = Math.max(height(node.left), height(node.right)) + 1;
}
2. 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, 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:
Tip
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 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. Right rotation¶
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.
Figure 7-26 Steps of right rotation
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
.
Figure 7-27 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:
def right_rotate(self, node: TreeNode | None) -> TreeNode | None:
"""Right rotation operation"""
child = node.left
grand_child = child.right
# Rotate node to the right around child
child.right = node
node.left = grand_child
# Update node height
self.update_height(node)
self.update_height(child)
# Return the root of the subtree after rotation
return child
/* 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;
}
/* 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;
}
2. Left rotation¶
Correspondingly, if considering the "mirror" of the above unbalanced binary tree, the "left rotation" operation shown in Figure 7-28 needs to be performed.
Figure 7-28 Left rotation operation
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
.
Figure 7-29 Left rotation with grand_child
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:
def left_rotate(self, node: TreeNode | None) -> TreeNode | None:
"""Left rotation operation"""
child = node.right
grand_child = child.left
# Rotate node to the left around child
child.left = node
node.right = grand_child
# Update node height
self.update_height(node)
self.update_height(child)
# Return the root of the subtree after rotation
return child
/* 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;
}
/* 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;
}
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
.
Figure 7-30 Left-right rotation
4. 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
.
Figure 7-31 Right-left rotation
5. Choice of rotation¶
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.
Figure 7-32 The four rotation cases of AVL tree
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.
Table 7-3 Conditions for Choosing Among the Four Rotation Cases
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 |
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:
def rotate(self, node: TreeNode | None) -> TreeNode | None:
"""Perform rotation operation to restore balance to the subtree"""
# Get the balance factor of node
balance_factor = self.balance_factor(node)
# Left-leaning tree
if balance_factor > 1:
if self.balance_factor(node.left) >= 0:
# Right rotation
return self.right_rotate(node)
else:
# First left rotation then right rotation
node.left = self.left_rotate(node.left)
return self.right_rotate(node)
# Right-leaning tree
elif balance_factor < -1:
if self.balance_factor(node.right) <= 0:
# Left rotation
return self.left_rotate(node)
else:
# First right rotation then left rotation
node.right = self.right_rotate(node.right)
return self.left_rotate(node)
# Balanced tree, no rotation needed, return
return node
/* 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;
}
/* 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;
}
7.5.3 Common operations in AVL trees¶
1. 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:
def insert(self, val):
"""Insert node"""
self._root = self.insert_helper(self._root, val)
def insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:
"""Recursively insert node (helper method)"""
if node is None:
return TreeNode(val)
# 1. Find insertion position and insert node
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:
# Do not insert duplicate nodes, return
return node
# Update node height
self.update_height(node)
# 2. Perform rotation operation to restore balance to the subtree
return self.rotate(node)
/* Insert node */
void insert(int val) {
root = insertHelper(root, val);
}
/* 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;
}
/* Insert node */
void insert(int val) {
root = insertHelper(root, val);
}
/* Recursively insert node (helper method) */
TreeNode insertHelper(TreeNode node, int val) {
if (node == null)
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;
}
2. 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:
def remove(self, val: int):
"""Remove node"""
self._root = self.remove_helper(self._root, val)
def remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:
"""Recursively remove node (helper method)"""
if node is None:
return None
# 1. Find and remove the node
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
# Number of child nodes = 0, remove node and return
if child is None:
return None
# Number of child nodes = 1, remove node
else:
node = child
else:
# Number of child nodes = 2, remove the next node in in-order traversal and replace the current node with it
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
# Update node height
self.update_height(node)
# 2. Perform rotation operation to restore balance to the subtree
return self.rotate(node)
/* Remove node */
void remove(int val) {
root = removeHelper(root, val);
}
/* 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;
}
/* Remove node */
void remove(int val) {
root = removeHelper(root, val);
}
/* Recursively remove node (helper method) */
TreeNode removeHelper(TreeNode node, int val) {
if (node == null)
return null;
/* 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 == null || node.right == null) {
TreeNode child = node.left != null ? node.left : node.right;
// Number of child nodes = 0, remove node and return
if (child == null)
return null;
// Number of child nodes = 1, remove node
else
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 != null) {
temp = temp.left;
}
node.right = removeHelper(node.right, temp.val);
node.val = temp.val;
}
}
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;
}
3. 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 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.