How to use the AVL Tree Visualizer
- 1. Enter keysType the keys to insert in order, and any to delete afterwards.
- 2. BuildThe tree is built one key at a time.
- 3. Step throughUse Prev and Next to see each insert and the rotation it caused.
How an AVL tree stays balanced
An AVL tree is a binary search tree that keeps itself balanced. Every node stores a balance factor: the height of its left subtree minus the height of its right subtree. AVL trees allow only −1, 0 or 1. When an insert or delete pushes a node to +2 or −2, the tree fixes it with a rotation:
- LL case (too heavy on the left-left): one right rotation.
- RR case (right-right): one left rotation.
- LR case: a left rotation on the child, then a right rotation.
- RL case: a right rotation on the child, then a left rotation.
Because the height always stays O(log n), search, insert and delete are guaranteed O(log n). A plain BST can become a straight line and degrade to O(n). Each node in the drawing is labelled with its bf; any node that briefly goes out of balance is marked in red.
Worked example
Insert 10, 20, 30, 40, 50, 25:
- 10 and 20 go in with no change.
- 30 makes 10's balance −2 (right-right), so a left rotation at 10 puts 20 at the root.
- 40 goes in fine. 50 unbalances 30 (right-right), so a left rotation at 30 lifts 40.
- 25 makes the root 20 right-heavy with a left-leaning right child: an RL rotation at 20, after which 30 becomes the root.
The final tree has root 30, height 3 and 3 rotations in total, with 20 (children 10 and 25) on the left and 40 (child 50) on the right. Its inorder walk is 10, 20, 25, 30, 40, 50. That's exactly the textbook answer for this classic exam question, so you can use the tool to check your working before a DSA test.
Frequently asked questions
Is an AVL tree better than a red-black tree?
AVL trees are more strictly balanced, so lookups are slightly faster; red-black trees do fewer rotations on insert and delete. Libraries like Java's TreeMap and C++'s std::map use red-black trees.
How does deletion work here?
The key is removed as in a BST (using the inorder successor when it has two children), then every ancestor is checked and rotated if needed.
What happens with duplicate keys?
They are skipped, the usual rule for AVL trees in exams.
Is the avl tree visualizer free to use?
Yes, the avl tree visualizer is free and costs nothing. Building your first tree shows a short form for your name and WhatsApp number one time; once that's done, this tool and every other Uncodemy tool open with no form.