AVL Tree Rotations Visualizer
See Exactly When and Why a Tree Rebalances
A free AVL tree visualizer for CS students: insert and delete keys, press Step, and watch every balance factor, every imbalance and every rotation.
A left-left imbalance at node 50 is fixed with one right rotation.
Home → CS Student Hub → AVL Tree Rotations Visualizer
This AVL tree rotations visualizer lets you insert and delete keys and watch an AVL tree keep itself balanced. After every change you can see each node’s height and balance factor, the first node that goes out of balance, and the exact rotation (left-left, right-right, left-right or right-left) that fixes it. Pick an example below, press Step, and then try your own keys.
AVL Tree Rotations Visualizer: Insert, Delete and Rebalance
Unlock Your Potential
Turn your highest ambitions into everyday reality. You set the standard and aim for the goal; when you’re ready for what comes next, we help you cross the finish line.
Own Your Future →How to Use This AVL Tree Visualizer
The AVL tree rotations visualizer above works like a lab bench: a few short experiments teach the idea faster than reading about it. Try these five, in order:
- Choose Left-left case, press Step three times, and read the text under the tree after each press. Node 30 turns red when its balance factor reaches +2, and one right rotation fixes it.
- Choose Left-right case and compare. The same three keys need two rotations because the new key sits on the inside of the zig-zag.
- Choose Insert keys 1 to 7, press Finish, and tick Compare with a plain BST. The plain tree becomes a straight chain of height 7, while the AVL tree stays at height 3.
- Type your own keys into the list box, for example 10, 20, 30, 40, 50, 25, and press Insert list. Use Back and Step to retrace any moment you missed.
- Choose Delete forces a rotation and press Step to see that removing a key can unbalance a tree too.
Each node shows its balance factor above it and its height below it. A good AVL tree visualizer lets you check every rule in the next sections with your own eyes, and this one does it for every step.
AVL Tree Rotations Step by Step
An AVL tree is a binary search tree that keeps itself balanced: for every node, the heights of its left and right subtrees differ by at most 1. It was invented in 1962 by Georgy Adelson-Velsky and Evgenii Landis, and the Wikipedia article on AVL trees gives the history and the formal proofs. Without this rule, inserting sorted keys turns an ordinary binary search tree into a long chain where every search takes O(n) time.
A rotation is the repair tool. It rearranges two or three nodes locally, changes the shape of that part of the tree, and keeps the in-order order of the keys unchanged, so the search-tree property survives. Every AVL tree rotation follows the same five moves:
- Insert or delete the key exactly as you would in an ordinary binary search tree.
- Walk back up from that spot towards the root and recompute each node’s height.
- At each node on the way, work out its balance factor.
- At the lowest node whose balance factor is +2 or −2, look at its taller child to decide which of the four cases you are in.
- Apply the matching rotation (one or two), then keep walking up in case another node is now out of balance.
Press Step in the AVL tree rotations visualizer and the text panel names each of these moves as it happens, with the real keys from your tree.
Height convention used on this page
Here an empty tree has height 0 and a single node has height 1. Some textbooks count a single node as height 0 instead. The numbers shift by one, but the balance factors, the cases and the rotations are identical, so the AVL tree rotations visualizer is safe to use with either convention.
AVL Tree Balance Factor Explained
The AVL tree balance factor of a node is the height of its left subtree minus the height of its right subtree. Some books subtract the other way round, which only flips every sign. This page and the visualizer always use left minus right.
| Balance factor | What it means | What to do |
|---|---|---|
| 0 | Both sides are equally tall | Nothing |
| +1 | Left side is one level taller | Nothing, still allowed |
| −1 | Right side is one level taller | Nothing, still allowed |
| +2 | Left side is two levels taller | Rotate: left-left or left-right case |
| −2 | Right side is two levels taller | Rotate: right-right or right-left case |
A single insertion or deletion changes a height by at most 1, so a balance factor can reach +2 or −2 but never +3. That is why a small, fixed set of rotations is enough to repair any imbalance. In the AVL tree rotations visualizer the balance factor above each node turns red the moment it reaches +2 or −2.
The Four AVL Tree Rotation Cases
Name the case by looking at the unbalanced node and its taller child. The first word says which side of the node is too tall; the second says which side of that child holds the extra weight. Each picture below shows the unbalanced node in red, the nodes involved in gold, and the balance factors above the nodes. You can reproduce all four in the AVL tree rotations visualizer with the example buttons.
When it happens: The unbalanced node has balance factor +2 and its left child has balance factor +1 or 0. The new key went into the left subtree of the left child.
The fix: Rotate right at the unbalanced node. Its left child moves up and the node becomes that child’s right child.
When it happens: The unbalanced node has balance factor −2 and its right child has balance factor −1 or 0. The new key went into the right subtree of the right child.
The fix: Rotate left at the unbalanced node. Its right child moves up and the node becomes that child’s left child.
When it happens: The unbalanced node has balance factor +2 but its left child leans the other way, with balance factor −1. The new key went into the right subtree of the left child.
The fix: First rotate left at the left child, which turns the zig-zag into a straight line. Then rotate right at the unbalanced node.
When it happens: The unbalanced node has balance factor −2 but its right child leans the other way, with balance factor +1. The new key went into the left subtree of the right child.
The fix: First rotate right at the right child. Then rotate left at the unbalanced node.
Left-right rotation in an AVL tree
The left right rotation AVL tree students struggle with most is simply two ordinary rotations in a row. A single right rotation cannot fix the zig-zag, because it would only move the heavy part of the tree to the other side. So the first rotation straightens the zig-zag into a line, and the second is the familiar left-left fix. Load Left-right case in the AVL tree rotations visualizer and press Step: the text panel calls the two moves the first and second rotation, and the rotation log counts both.
AVL Tree Insertion Example, Step by Step
Insert the keys 10, 20, 30, 40, 50 and 25 into an empty tree, one at a time. This sequence is a classic because it produces three of the four situations. The pre-order column lists the tree as root, then left subtree, then right subtree.
| Insert | What happens | Tree after (pre-order) |
|---|---|---|
| 10 | The tree was empty, so 10 becomes the root. | 10 |
| 20 | 20 goes to the right of 10. Node 10 now has balance factor −1, which is allowed. | 10, 20 |
| 30 | Node 10 has balance factor −2 and its right child 20 has −1: right-right case. Rotate left at 10. | 20, 10, 30 |
| 40 | 40 goes to the right of 30. All balance factors stay within −1 to +1. | 20, 10, 30, 40 |
| 50 | Node 30 has balance factor −2 and its right child 40 has −1: right-right case. Rotate left at 30. | 20, 10, 40, 30, 50 |
| 25 | 25 goes down 20, 40, 30 and lands left of 30. Node 20 has balance factor −2 but its right child 40 has +1: right-left case. Rotate right at 40, then left at 20. | 30, 20, 10, 25, 40, 50 |
The final tree has 30 at the root, 20 and 40 as its children, and a height of 3, after four rotations in total. Type 10, 20, 30, 40, 50, 25 into the Insert list box of the AVL tree rotations visualizer and press Finish to see exactly this tree, then press Back to walk through each step.
AVL Tree Deletion and Rebalancing
Deletion starts like an ordinary binary search tree. A leaf is simply removed. A node with one child is replaced by that child. A node with two children is replaced by its in-order successor, the smallest key in its right subtree. After that, walk back up and rebalance exactly as you did after an insertion.
Three things are different from insertion:
- Removing a key can unbalance the other side. Taking a key out of one subtree makes it shorter, so the sibling subtree may now be two levels taller.
- The taller child can have balance factor 0. This cannot happen after an insertion. When it does, one single rotation is enough, as in the left-left or right-right case.
- One rotation may not be the end. A rotation can shorten the subtree, which can unbalance a node higher up, so the check continues all the way to the root. Deletion may need O(log n) rotations.
To watch this, choose Delete forces a rotation in the visualizer. The tree holds 50, 30, 70, 20, 40, 60, 80 and 10. Deleting the leaf 40 leaves node 30 with a left subtree of height 2 and nothing on its right, so its balance factor is +2. Its left child 20 has balance factor +1, so this is the left-left case and one right rotation at 30 puts 20 in charge of that subtree.
AVL Tree vs Binary Search Tree vs Red-Black Tree
All three store keys in sorted order and answer the same questions. They differ in how hard they work to stay balanced, and that choice decides which one suits your program.
| Feature | Plain BST | AVL tree | Red-black tree |
|---|---|---|---|
| Balance rule | None | Subtree heights differ by at most 1 | Colour rules keep the longest path at most twice the shortest |
| Worst-case height | n (a chain) | About 1.44 log2 n | About 2 log2 n |
| Search, insert, delete (worst case) | O(n) | O(log n) | O(log n) |
| Rotations after an insert | None | At most 1 (single or double) | At most 2 |
| Rotations after a delete | None | Up to O(log n) | At most 3 |
| Best when | Data arrives in random order and the tree is small | Many lookups and fewer changes | Frequent inserts and deletes; used by many library maps such as Java’s TreeMap |
An AVL tree is more strictly balanced, so searches are slightly faster. A red-black tree does less work per update, so it often wins when the data changes constantly. To see the difference yourself, tick Compare with a plain BST in the AVL tree rotations visualizer and insert a sorted list. For exams and interviews, remember the AVL rule (balance factor of −1, 0 or +1) and the four rotation cases.
AVL Tree Code: Python and C++
These two programs implement the same logic the AVL tree rotations visualizer uses: insert, delete and rebalance with the four cases. Both were tested against the tool on 400 random sequences of inserts and deletes and produced identical trees every time. Inserting 10, 20, 30, 40, 50 and 25 prints the pre-order 30, 20, 10, 25, 40, 50 from the worked example above.
Python
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.height = 1 # a single node has height 1
def height(n):
return n.height if n else 0
def update(n):
n.height = 1 + max(height(n.left), height(n.right))
def balance(n):
return height(n.left) - height(n.right) if n else 0
def rotate_right(z):
y = z.left
z.left = y.right # the middle subtree moves across
y.right = z
update(z) # z is lower now, so update it first
update(y)
return y # y is the new root of this subtree
def rotate_left(z):
y = z.right
z.right = y.left
y.left = z
update(z)
update(y)
return y
def rebalance(n):
update(n)
b = balance(n)
if b > 1: # left side is too tall
if balance(n.left) < 0: # left-right case: rotate the child first
n.left = rotate_left(n.left)
return rotate_right(n) # left-left case (or second half of left-right)
if b < -1: # right side is too tall
if balance(n.right) > 0: # right-left case: rotate the child first
n.right = rotate_right(n.right)
return rotate_left(n) # right-right case (or second half of right-left)
return n
def insert(n, key):
if n is None:
return Node(key)
if key < n.key:
n.left = insert(n.left, key)
elif key > n.key:
n.right = insert(n.right, key)
else:
return n # duplicates are ignored
return rebalance(n)
def delete(n, key):
if n is None:
return None
if key < n.key:
n.left = delete(n.left, key)
elif key > n.key:
n.right = delete(n.right, key)
else:
if n.left is None:
return n.right
if n.right is None:
return n.left
s = n.right # in-order successor: smallest key on the right
while s.left:
s = s.left
n.key = s.key
n.right = delete(n.right, s.key)
return rebalance(n)
def preorder(n):
return [n.key] + preorder(n.left) + preorder(n.right) if n else []
if __name__ == "__main__":
root = None
for k in [10, 20, 30, 40, 50, 25]:
root = insert(root, k)
print(preorder(root)) # [30, 20, 10, 25, 40, 50]
C++
#include <algorithm>
#include <iostream>
#include <vector>
struct Node {
int key;
int height = 1; // a single node has height 1
Node* left = nullptr;
Node* right = nullptr;
explicit Node(int k) : key(k) {}
};
int height(Node* n) { return n ? n->height : 0; }
void update(Node* n) { n->height = 1 + std::max(height(n->left), height(n->right)); }
int balance(Node* n) { return n ? height(n->left) - height(n->right) : 0; }
Node* rotateRight(Node* z) {
Node* y = z->left;
z->left = y->right; // the middle subtree moves across
y->right = z;
update(z); // z is lower now, so update it first
update(y);
return y; // y is the new root of this subtree
}
Node* rotateLeft(Node* z) {
Node* y = z->right;
z->right = y->left;
y->left = z;
update(z);
update(y);
return y;
}
Node* rebalance(Node* n) {
update(n);
int b = balance(n);
if (b > 1) { // left side is too tall
if (balance(n->left) < 0) // left-right case
n->left = rotateLeft(n->left);
return rotateRight(n); // left-left case
}
if (b < -1) { // right side is too tall
if (balance(n->right) > 0) // right-left case
n->right = rotateRight(n->right);
return rotateLeft(n); // right-right case
}
return n;
}
Node* insert(Node* n, int key) {
if (!n) return new Node(key);
if (key < n->key) n->left = insert(n->left, key);
else if (key > n->key) n->right = insert(n->right, key);
else return n; // duplicates are ignored
return rebalance(n);
}
Node* erase(Node* n, int key) {
if (!n) return nullptr;
if (key < n->key) n->left = erase(n->left, key);
else if (key > n->key) n->right = erase(n->right, key);
else {
if (!n->left) { Node* r = n->right; delete n; return r; }
if (!n->right) { Node* l = n->left; delete n; return l; }
Node* s = n->right; // in-order successor
while (s->left) s = s->left;
n->key = s->key;
n->right = erase(n->right, s->key);
}
return rebalance(n);
}
void preorder(Node* n, std::vector<int>& out) {
if (!n) return;
out.push_back(n->key);
preorder(n->left, out);
preorder(n->right, out);
}
int main() {
Node* root = nullptr;
for (int k : {10, 20, 30, 40, 50, 25}) root = insert(root, k);
std::vector<int> v;
preorder(root, v);
for (int k : v) std::cout << k << ' '; // 30 20 10 25 40 50
std::cout << '\n';
}
Complexity and uses
Search, insert and delete all take O(log n) time in the worst case, because the height never exceeds about 1.44 log2 n. A tree of 1,000,000 keys is at most about 28 levels tall. Insert the keys 1 to 31 in sorted order into the AVL tree rotations visualizer and the height stays at 5, while a plain binary search tree would be 31 levels deep. Each rotation takes constant time, and the tree needs O(n) space plus one stored height per node. AVL trees suit read-heavy sorted data such as in-memory indexes and dictionaries, and they are a favourite exam topic because the rotation rules are short and exact.
AVL Tree Rotations Practice Problems
Try each problem on paper first, then check it in the AVL tree rotations visualizer. Click a question to reveal the answer.
Problem 1: Insert 14, 17, 11, 7, 53, 4 and 13 into an empty AVL tree, in that order. When is a rotation needed, and what is the final pre-order?
Nothing needs fixing until 4 is inserted. Then node 11 has balance factor +2 and its left child 7 has +1, so this is the left-left case: rotate right at 11. Inserting 13 afterwards goes to the right of 11 and needs no rotation. The final pre-order is 14, 7, 4, 11, 13, 17, 53, with one rotation in total.
Problem 2: What is the smallest number of nodes an AVL tree of height 4 can have? (A single node has height 1.)
The thinnest AVL tree of height h has one subtree of height h−1 and one of height h−2. So N(h) = N(h−1) + N(h−2) + 1, with N(1) = 1 and N(2) = 2. That gives N(3) = 4 and N(4) = 7. This recurrence is why the height of an AVL tree is never more than about 1.44 log2 n.
Problem 3: A node has balance factor +2 and its left child has balance factor −1. Which case is this, and which rotations fix it?
This is the left-right case. First rotate left at the left child, then rotate right at the unbalanced node. Load Left-right case in the visualizer to watch both rotations.
Problem 4: Build the tree 50, 30, 70, 20, 40, 60, 80, 10 and then delete 40. What happens, and what is the pre-order afterwards?
Deleting the leaf 40 leaves node 30 with balance factor +2. Its left child 20 has balance factor +1, so it is the left-left case: rotate right at 30. The pre-order after the deletion is 50, 20, 10, 30, 70, 60, 80.
AVL Tree Rotations FAQ
What is an AVL tree rotation?
A rotation is a small local change that moves one node above another to rebalance a tree. It changes the shape of the subtree but not the sorted order of the keys. The four cases are left-left, right-right, left-right and right-left, and you can try all four in the AVL tree rotations visualizer above.
What is the balance factor in an AVL tree?
It is the height of a node’s left subtree minus the height of its right subtree. In a valid AVL tree every node has a balance factor of −1, 0 or +1. A value of +2 or −2 means the node needs a rotation.
How many rotations does an AVL tree need after an insertion?
At most one rotation step: either a single rotation or a double rotation (two rotations that count as one repair). After that, the height of the subtree returns to what it was before the insertion, so no node higher up is affected. Deletion is different and may need a rotation at several levels.
Why does the left-right case need two rotations?
In the left-right case the extra weight sits on the inside of a zig-zag. One right rotation would only swing that weight to the other side and leave the tree just as unbalanced. Rotating left at the child first straightens the zig-zag, and then one right rotation finishes the job. The AVL tree rotations visualizer shows the two rotations as separate steps.
What is the time complexity of AVL tree operations?
Search, insert and delete all run in O(log n) in the worst case, because the height of an AVL tree is at most about 1.44 log2 n. A plain binary search tree can degrade to O(n) when keys arrive in sorted order.
When should I use an AVL tree instead of a red-black tree?
Choose an AVL tree when lookups are much more common than updates, because its stricter balance gives slightly shorter paths. Choose a red-black tree when inserts and deletes are frequent, because it rotates less often per update.
More Free Tools for CS Students
After the AVL tree rotations visualizer, keep practising with the other interactive tools on CS Student Hub:
Stuck on an AVL Tree Problem? Ask Us
Not sure why your rotations do not match the AVL tree rotations visualizer, or want help tracing your own insert and delete sequence? Send us your question and we will reply by email.
Submit Your Question — Get a Step-by-Step Explanation
We will review your keys and your question and email you a clear walk-through, usually within 24-48 hours. Free. No strings attached.
📋 Before you submit:
- List the keys in the order you insert or delete them
- Tell us which rotation you expected, and which one the tool showed
- Tell us where you are getting confused
Fill out the form below
🔒 Your email is safe with us. We will never share your information.
⚠️ Important Disclaimer
We aim to respond within 24-48 hours, but response times may vary. Our guidance is meant to help you learn, not to replace your own work or your professor’s instruction.
Get More CS Resources
Subscribe to get new visualizers, practice problems and CS study guides delivered to your inbox.
No spam. Unsubscribe anytime.
