AVL Tree Visualizer
Follow comparisons and rotations as an AVL tree keeps its balance. Insert, delete or search integer keys, pause the operation trace, and inspect each node's height and balance factor. Export the committed tree or restore an earlier JSON export.
Height: empty = 0, leaf = 1.
Balance: left height − right height.
Safe integers from −9007199254740991 through 9007199254740991. Up to 512 unique keys and 1,000 operations per batch keep the diagram and trace responsive. Ctrl or ⌘ + Enter inserts.
is a valid beginning.
Height 0. Insert keys to place the first node and follow the path to a balanced tree.
Node labels show key, height (h) and balance (b). Exports always use the committed tree.
Committed root empty; 0 keys; height 0.
Committed result
Start with an empty tree. Insert the example keys to see the comparisons and right rotation.
Committed tree as accessible text
Empty tree. Height 0; no keys.
Worked rotation examples
These examples replace the committed tree with the stated fixture. Export your current tree first if you want to keep it.
Accuracy. Educational implementation with explicit duplicate-key policy; no production database performance claims.
Common questions
- What makes an AVL tree balanced?
- Every node's left and right subtree heights differ by at most one. This tool defines an empty subtree's height as 0 and a leaf's height as 1. The balance factor is left height minus right height. Insertions and deletions update heights and rotate imbalanced subtrees before the new tree is committed.
- How can I see left-left and left-right rotations?
- The worked examples start from an empty tree. Inserting 30, 20, 10 creates a left-left imbalance and a right rotation, leaving 20 as root with children 10 and 30. Inserting 30, 10, 20 creates a left-right case: rotate left at 10 and then right at 30. Previous step, Next step, Play trace and Pause show the same saved trace.
- How are duplicate keys and missing values handled?
- Duplicate insertion is a visible no-op: the existing key stays in the tree. Deleting an absent key also leaves the tree unchanged. Search reports whether the key was found and never changes the tree. All three operations include an explanatory trace.
- How does deletion choose a replacement?
- A leaf is removed directly; a node with one child is replaced by that child. A node with two children uses its inorder successor, the smallest key in its right subtree. Heights are then updated and any affected ancestors are rebalanced. Deleting 20 from the tree [20, 10, 30] leaves root 30 with left child 10 and height 2.
- Why can a trace frame show a balance of 2 or -2?
- Trace frames include temporary states immediately before rotations. These are explicitly labelled as previews with temporary imbalance. The committed tree has already passed ordering, uniqueness, cached-height and AVL-balance checks. The Committed tree control shows that final tree; SVG and JSON exports always use it.
- What key values and batch sizes are supported?
- Keys must be safe integers from -9007199254740991 through 9007199254740991, with no decimal points or exponents. Commas or whitespace separate keys. The interactive diagram supports 512 unique keys, 1,000 operations per batch and 50,000 input characters to bound diagram and trace memory. Long batches yield progress and can be cancelled; cancellation leaves the previous committed tree unchanged.
- Can I save the tree and continue later?
- The committed tree is saved locally when browser storage is available. JSON export includes the exact tree shape, sorted canonical keys, height convention, balance convention and duplicate policy. Import checks all of these before replacing the tree. Restoring a tree does not replay an old operation. SVG includes node labels and accessible parent-child descriptions.
- Does animation speed change the algorithm?
- No. Speed changes only when the saved trace advances. Pause or step through it at your own pace. New operations invalidate older animation callbacks, and playback pauses when the page is hidden. With reduced motion enabled, a new trace starts paused; manual step controls remain available.
Educational implementation with explicit duplicate-key policy; no production database performance claims.