Self-Adjusting Splay Trees #
This module formalizes annotation-free bottom-up splay trees over Mathlib's BinaryTree α,
reusing the finite-set abstraction Cslib.DataStructure.SearchTree.elements and strict
binary-search-tree invariant Cslib.DataStructure.SearchTree.Ordered ([ST85b],
Sections 2–3, pp. 654–662).
Unlike height- or weight-balanced search trees, splay nodes store no auxiliary balance metadata.
Each operation descends a search path, saves the ancestor chain in an explicit stack (Frame),
and restructures the tree bottom-up from the target node (or the last non-empty node visited on an
unsuccessful search) to the root using zig (single terminal rotation at the root), zig-zig
(parent-grandparent rotation followed by node-parent rotation), and zig-zag (double rotation)
steps ([ST85b], Section 2, pp. 655–656, Figure 3; Section 3, pp. 660–661, Figure 6;
Section 4, p. 665).
Design notes and cost conventions #
- Set-idempotent updates: Sleator and Tarjan ([ST85b], Section 3, p. 661) specify
insert(i, t)under the preconditioni ∉ tanddelete(i, t)underi ∈ t. To realizeCslib.DataStructure.OrderedSet.spec αdirectly,insertanderaseare total and set-idempotent: inserting a present key splays that key to the root without duplicating it, and erasing an absent key splays the last visited node along the failed search path while leavingelementsunchanged. - Rotation counts vs. node visits: Each
SplayResult,SplitResult, andQueryResultrecords the exact number of binary-tree edge rotations executed (0on.nilor when the target is already at the root,1forzig, and2perzig-zigorzig-zagstep). On any non-empty tree,(splay x t).rotations + 1 = SearchTree.searchVisits x tand(splayMin t).rotations + 1 = SearchTree.minimumVisits t. Ancestor-stack allocation, tree-node allocation, and key-comparison costs are not charged separately.
Main definitions #
SplayResult,SplitResult,QueryResult: named output records bundling restructured trees, query answers, and exact edge-rotation counts.Frame,plug,Focus,splayUp: upward ancestor zipper and bottom-up splaying loop.splay,splayMin,splayMax: splay at a searched key (or last visited node), leftmost key, or rightmost key.contains,split,join,insert,erase,minimum,successor: self-adjusting dictionary and split/join operations.
Main results #
elements_splay,ordered_splay,rootKey_splay_of_mem,splay_split_lt,splay_split_ge:splaypreserveselementsandOrdered, moves present keys to the root, and partitions keys across the splayed root whenxis absent.rotations_splay_add_one_eq_searchVisits,rotations_splayMin_add_one_eq_minimumVisits: exact correspondence between rotation counts andSearchTreedownward path visits.contains_answer_eq,elements_split_left,elements_split_right,elements_join,ordered_join,elements_insert,ordered_insert,elements_erase,ordered_erase,minimum_spec,successor_spec: semantic correctness andOrderedpreservation for all operations.
References #
- [ST85b] Daniel Dominic Sleator and Robert Endre Tarjan, Self-Adjusting Binary Search Trees, Journal of the ACM 32(3), Sections 2–4, pp. 654–667, 1985.
Key stored at the root of a BinaryTree α, or none if empty.
Output of split: left tree (≤ x), right tree (> x), and edge-rotation count.
- left : BinaryTree α
Subtree containing all keys
≤ x. - right : BinaryTree α
Subtree containing all keys
> x. - rotations : ℕ
Exact number of edge rotations executed during
split.
Instances For
Output of a self-adjusting query (contains, minimum, successor).
- answer : β
Query answer.
- tree : BinaryTree α
Restructured binary search tree after splaying.
- rotations : ℕ
Exact number of edge rotations executed.
Instances For
One ancestor frame along a downward search path in a BinaryTree α.
- left {α : Type u_3} (key : α) (rightSub : BinaryTree α) : Frame α
- right {α : Type u_3} (leftSub : BinaryTree α) (key : α) : Frame α
Instances For
Reconstruct a BinaryTree α by plugging t upward through an ancestor stack ctx
(ordered from parent to original root).
Focused non-empty node .node key leftSub rightSub together with its upward ancestor stack.
- key : α
Key at the focused node.
- leftSub : BinaryTree α
Left subtree of the focused node.
- rightSub : BinaryTree α
Right subtree of the focused node.
Ancestor stack from parent to root.
Instances For
The subtree rooted at the focused node, ignoring its ancestors.
Reconstruct the full tree represented by f.
Bottom-up splaying loop ([ST85b], Section 2, pp. 655–656, Figure 3):
rotates the focused node (x, l, r) to the root through its upward ancestor stack ctx via
zig-zig and zig-zag pairs and at most one terminal zig step.
Focus on the leftmost node of t, accumulating upward left-child frames onto ctx.
Focus on the rightmost node of t, accumulating upward right-child frames onto ctx.
Splay the leftmost (minimum) node of t to the root.
Splay the rightmost (maximum) node of t to the root.
On any non-empty tree t, (splayMin t).tree has .nil as its left child.
On any non-empty tree t, (splayMax t).tree has .nil as its right child.
Every rotation step in splayUp preserves the represented finite set of keys.
splayUp preserves Ordered whenever the reconstructed initial tree is Ordered.
Descend the search path for x in t, returning the node containing x if present or the
last non-empty node visited before an empty child ([ST85b], Section 3, pp. 660–661,
Figure 6).
Key separation invariant of focusSearch: in an Ordered tree t, f.key ∈ elements t,
f.key = x whenever x ∈ elements t, and no key of t lies strictly between x and f.key.
Core Splaying Operations #
Splay t at x: if t is empty, return (.nil, 0); otherwise splay the node reached by
focusSearch x t [] to the root ([ST85b], Section 2, pp. 655–656, and Section 3,
pp. 660–661, Figure 6).
splay preserves the represented finite set of keys on any tree.
splay preserves the strict binary-search-tree invariant.
On any non-empty tree, the rotation count of splay x t plus one equals searchVisits x t.
If x ∈ elements t in an Ordered tree t, splay x t places x at the root.
Split separation on the left: if (splay x t).tree = .node r L R and x < r, then every key
in L is strictly less than x.
Split separation on the right: if (splay x t).tree = .node r L R and r ≤ x, then every key
in R is strictly greater than x.
Self-Adjusting Dictionary and Split/Join Operations #
Self-adjusting membership test (access(x, t) in [ST85b], Section 3,
pp. 660–661): splays t at x and checks whether the new root equals x.
Split t at x into (left, right) with keys ≤ x and > x ([ST85b],
Section 3, pp. 661–662, Figure 7(b)).
Concatenate two trees t₁ and t₂ whose keys satisfy ∀ x ∈ t₁, ∀ y ∈ t₂, x < y, by
splaying the maximum element of t₁ to its root and attaching t₂ as its right subtree
([ST85b], Section 3, pp. 661–662, Figure 7(a)).
Self-adjusting set-idempotent insertion ([ST85b], Section 3, p. 662,
Figure 8(a)): splays t at x; if x is already at the root, leaves the splayed tree unchanged,
and otherwise inserts x as the new root above the split subtrees.
Self-adjusting set-idempotent deletion ([ST85b], Section 3, p. 662,
Figure 8(b)): splays t at x, and if x is at the new root replaces the tree by join of its
left and right subtrees.
Self-adjusting minimum query: splays the leftmost node to the root and returns its key.
Self-adjusting strict successor query: splays t at x; if the splayed root r satisfies
x < r, r is already the least key > x; otherwise splays the minimum of the right subtree R
and rotates it to the root.