visual-cs

Splay Tree - self-adjusting binary search tree

← All visualizations

Splay Tree
Rotation Studio

Insert, search, and delete integer keys. Every access splays the touched node toward the root with zig, zig-zig, or zig-zag rotations, making recently used values cheaper to reach next time.

Root
Empty
current root after the latest splay
Nodes
0
keys currently stored
Height
0
longest root-to-leaf path
Rotations
0
performed during the latest operation
Speed 700ms
Preset Sequences
Tree Canvas
Frame 0 / 0
Blue active node, amber access path, violet rotating trio
Ready for the next access.
accessed / inserted / found node
search path or successor path
zig / zig-zig / zig-zag rotation focus
current root
removed node snapshot
How splaying works. A splay tree is a regular BST plus a rule: after every successful insert/search/delete, the touched node is moved to the root. Single rotation zig happens when the parent is the root. Two-step zig-zig rotates parent then node in the same direction. zig-zag rotates node twice in opposite directions.

This keeps hot keys near the top without storing explicit balance metadata. Worst-case single operation time is still O(n), but the amortized bound for search, insert, and delete is O(log n).
Deletion strategy. When deleting key x, the visualization first splays x to the root. Then it joins the remaining left and right subtrees by splaying the maximum node of the left subtree to the top and attaching the old right subtree as its right child.

Operation Stats

Ready
Last Op
-
Target
0
Frames
0
Visited

Access Path

Timeline

Recent Log

Why Use Splay Trees?

They are simple to implement, adapt to skewed workloads, and satisfy useful bounds like the working-set and static-finger properties. This makes them attractive when recent accesses strongly predict future accesses.