AVL Tree
A self-balancing Binary Search Tree where the heights of the left and right subtrees of any node differ by at most 1. Named after inventors Adelson-Velsky and Landis.Overview
AVL trees automatically maintain balance through rotations, guaranteeing O(log n) time complexity for search, insertion, and deletion operations. This makes them ideal when you need predictable performance.Implementation
AVL Tree extends Binary Search Tree with self-balancing logic:Balance Factor
The balance factor determines if a tree is balanced:-2: Unbalanced right (left-heavy)-1: Slightly unbalanced right0: Balanced1: Slightly unbalanced left2: Unbalanced left (right-heavy)
AVL Property: For every node, the balance factor must be -1, 0, or 1.
If it becomes -2 or 2, the tree is rebalanced through rotations.
API Reference
insert(key: T): boolean
Inserts a value and automatically balances the tree.remove(key: T): boolean
Removes a value from the tree.Rotation Operations
AVL trees use four types of rotations to maintain balance:Left-Left (LL) Rotation
Used when the left subtree of the left child is too deep.Right-Right (RR) Rotation
Used when the right subtree of the right child is too deep.Left-Right (LR) Rotation
Used when the right subtree of the left child is too deep.Right-Left (RL) Rotation
Used when the left subtree of the right child is too deep.Helper Methods
getNodeHeight(node)
Calculates the height of a node.- Null node: -1
- Leaf node: 0
- Other nodes: max(left height, right height) + 1
getBalanceFactor(node)
Calculates the balance factor of a node.Balancing Logic
After each insertion, the tree checks balance and applies rotations:Complexity Summary
| Operation | AVL Tree | BST (Average) | BST (Worst) |
|---|---|---|---|
| insert | O(log n) | O(log n) | O(n) |
| remove | O(log n) | O(log n) | O(n) |
| search | O(log n) | O(log n) | O(n) |
| min/max | O(log n) | O(log n) | O(n) |
Guaranteed Performance: AVL trees provide O(log n) in all cases, making them superior to unbalanced BSTs for performance-critical applications.
Example: Building a Balanced Tree
Use Cases
AVL trees excel when:
- Search operations significantly outnumber insertions/deletions
- Guaranteed O(log n) performance is required
- Dealing with worst-case input patterns (sorted data)
- Building databases and file systems
- Implementing associative arrays/maps
AVL vs Other Balanced Trees
| Feature | AVL Tree | Red-Black Tree | B-Tree |
|---|---|---|---|
| Balance | Strictly balanced | Loosely balanced | Multi-way balanced |
| Height | ~1.44 log n | ~2 log n | Variable |
| Rotations on insert | Up to 2 | Up to 2 | Rare |
| Search speed | Fastest | Slower | Slower |
| Insert/Delete speed | Slower | Faster | Fastest |
| Use case | Search-heavy | Balanced ops | Databases |
Recommendation:
- Use AVL for search-heavy workloads
- Use Red-Black for insert/delete-heavy workloads
- Use B-Tree for disk-based storage
Rotation Decision Tree
Visualization Example
Inserting 1, 2, 3 into AVL Tree
Implementation Details
Why AVL Trees Stay Balanced
- After every insertion, calculate balance factors from inserted node to root
- If balance factor is ±2, tree is unbalanced
- Determine rotation type based on insertion path
- Perform rotation(s) to restore balance
- Height reduced by 1 at rotation point
Space Overhead
AVL trees don’t store height/balance factors in nodes (in this implementation). Instead, they calculate them on-demand during balancing:- Memory: Same as BST (no extra fields)
- Computation: Additional O(log n) for height calculations during insertion
Key Characteristics
Advantages:
- Guaranteed O(log n) for all operations
- More strictly balanced than Red-Black trees
- Faster search operations
- Predictable performance
- Height never exceeds 1.44 log n
When to Use AVL Trees
Frequent Lookups
Frequent Lookups
When search operations far outnumber modifications, AVL’s strict balance pays off.
Predictable Performance
Predictable Performance
When you need guaranteed O(log n) without worst-case degradation.
Sorted Input
Sorted Input
When data arrives in sorted or nearly-sorted order, AVL prevents skewing.
Real-time Systems
Real-time Systems
When worst-case latency must be bounded.
Related Data Structures
Binary Search Tree
Base class without self-balancing
Binary Heap
Different balance property for priority queues