No tree yet — build one from the controls.
AlgoPlus//structures / avl
Read the theory

AVL Tree

A self-balancing BST — rotations keep it height-balanced.

Nodes
0
Legend
Inserted node
Node
AI Tutor Workspace
In a nutshell
An AVL tree is a binary search tree that refuses to get lopsided. After every insert it checks each node's balance factor — the height difference between its two branches — and the moment that gap exceeds one, it performs a rotation: a small, local rearrangement that pivots a few nodes to even the sides out again. Staying height-balanced this way keeps search and insert fast (O(log n)) even when data arrives already sorted, which would turn a plain BST into a slow chain.
Ready
Press play to begin the cinematic walkthrough.
A BST that rebalances itself: after each insert, if a node's two sides differ in height by more than one, a rotation restores balance — keeping operations O(log n).
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for AVL Tree.
Code Simulator
1
def bst(root, val):
2
    # insert / search / traverse
3
    if root is None:
4
        return Node(val)  # Inserted/Found
5
    if val < root.val:
6
        root.left = bst(root.left, val)
7
    else:
8
        root.right = bst(root.right, val)
9
    # traversal order
10
    visit(root.val)  # Visit
Why Python? · Readable first, fast second

Dynamically typed and interpreted — every comparison and swap is dispatched by the interpreter at run time, so tight loops run roughly 10–100× slower than compiled C/C++. Unbeatable for learning the idea with the least code; not what you reach for when the inner loop is the bottleneck.