Self-balancing Tree Visualizer

LLRB Trees, Splay Trees & B-Trees

Left-Leaning Red-Black Tree

Step through the code line-by-line to see how unique_ptr ownership changes during rotations and insertions. Each step shows which pointer owns which node.

Use Next → to step through the insertion

      
Red node
Black node
null / moved-from
Step 0 / 6

      
Red node
Black node
null / moved-from
Step 0 / 7

      
Red node
Black node
Step 0 / 0

      
Red node
Black node
Step 0 / 4

Splay Trees

A splay tree moves accessed nodes to the root via rotations. This improves future access to the same or nearby nodes (locality of reference). Step through each splay operation to see how the tree restructures.

Or: Use Next → to step through the splay

Zig (Single Rotation)

When the target is a direct child of the current subtree root, a single rotation brings it to the top.


Left child: Rotate right

Right child: Rotate left


This is the final step of a splay when the target is one level away from the subtree root.

Click "Next" to step through the zig rotation.
Step 1 / 4

Zig-Zig (Same Side)

Target X and its parent P are both on the same side (both left children or both right children).


  1. Recursively splay in the grandchild subtree
  2. Rotate at grandparent (not parent!)
  3. Rotate again (final rotation brings X to top)

Key: Rotating at the grandparent first (instead of the parent) is what distinguishes splaying from naive rotate-to-root, and gives the amortized O(lg N) guarantee.

Click "Next" to step through the zig-zig operation.
Step 1 / 6

Zig-Zag (Opposite Sides)

Target X and its parent P are on opposite sides (e.g., P is a left child but X is P's right child).


  1. Recursively splay in the grandchild subtree
  2. Rotate at parent (brings X up one level)
  3. Rotate at grandparent (final rotation brings X to top)

Similar to an AVL double rotation. X moves up two levels: first past its parent, then past its grandparent.

Click "Next" to step through the zig-zag operation.
Step 1 / 6

Full Splay Operation

Watch a complete splay bring a deep node all the way to the root by repeatedly applying zig, zig-zig, or zig-zag.


Build a demo tree first, then access a node to see the full splay in action.

Insert nodes using the controls above, then access a node to see the splay operation step-by-step.
Step 0 / 0

B-Tree

A B-tree of order M is a self-balancing tree where each node can have multiple keys and children. This visualization shows insertion with node splitting when nodes exceed capacity.

B-Tree Invariants (Order M)

  • Node Invariant: Internal nodes have up to M pointers and M-1 sorted keys; Leaf nodes have up to L key-value pairs
  • Order Invariant: Keys to the left are less than the key; keys to the right are ≥ the key
  • Structure Invariant: Root has 2 to M children (if not leaf); internal nodes have ⌈M/2⌉ to M children; all leaves at same depth; leaves have ⌈L/2⌉ to L items
Key cell
Pointer cell
Child pointer
Ready to insert. The B-tree will automatically split nodes when they exceed capacity. Each node shows alternating pointer cells (white) and key cells (blue). Enable "Show values" to see how each key maps to a value.