AVL Tree
Idea Like You Are 10
An AVL tree is a BST that keeps itself balanced.
It does not allow one side to become much taller than the other.
Balance Rule
For every node:
Allowed values:
-101
If the balance factor goes outside that range, the tree rotates to fix itself.
Rotations
LL Rotation
Right rotation
RR Rotation
Left rotation
LR Rotation
Left then right rotation
RL Rotation
Right then left rotation
Insert
- Insert like BST
- Update heights
- Check balance factor
- Rotate if needed
Delete
- Delete like BST
- Update heights
- Rebalance using rotations
Complexity Analysis
| Operation | Time |
|---|---|
| Search | O(log n) |
| Insert | O(log n) |
| Delete | O(log n) |
Why?
AVL keeps height near log n, so operations travel only along a short root-to-leaf path.
Main Advantage
Faster worst-case searching than a plain BST.
Tradeoff
Insert and delete are more complex because rotations may be needed.