Tree Visualiser — BST · AVL · Red-Black
Type up to 15 numbers, choose a tree type, and press Play. Each value is inserted one at a time: watch the search compare its way down, the new node attach, then the tree rebalance with rotations (AVL & Red-Black) and recolouring (Red-Black). The stats track every operation and the tree's height — the key to its complexity.
How to Play
Enter numbers in order (e.g. 10, 20, 30, 40, 50, 60, 70) and run a
Binary Search Tree. It degenerates into a straight line (height = n) — search
becomes O(n)! Now switch to AVL or Red-Black with the same input and
watch rotations keep it short and bushy. That's exactly why self-balancing trees exist.
How the Three Trees Compare
| Tree | Balanced? | Search / Insert | Worst height | Rebalancing cost |
|---|---|---|---|---|
| Binary Search Tree | No | O(log n) avg, O(n) worst | n (a line) | None |
| AVL Tree | Strictly | O(log n) | ≈ 1.44 log₂n | Rotations (more) |
| Red-Black Tree | Loosely | O(log n) | ≤ 2 log₂(n+1) | Recolours + fewer rotations |
A plain BST is simplest but can degrade to a linked list. AVL keeps
the tightest balance — fastest lookups, but more rotations on insert. Red-Black
balances more loosely using cheap recolouring, so it does fewer rotations — which is why it
backs many standard libraries (like C++ std::map and Java TreeMap).