Documentation

CslibFrontier.DataStructure.SplayTree.Basic

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 #

Main definitions #

Main results #

References #

Key stored at the root of a BinaryTree α, or none if empty.

@[simp]
theorem Cslib.DataStructure.SplayTree.rootKey_node {α : Type u_1} (a : α) (l r : BinaryTree α) :

Output of a tree-restructuring splay or update operation (splay, splayMin, splayMax, join, insert, erase).

  • tree : BinaryTree α

    Restructured binary search tree.

  • rotations : ℕ

    Exact number of edge rotations executed (0 if empty or already at the root).

Instances For
    theorem Cslib.DataStructure.SplayTree.SplayResult.ext {α : Type u_3} {x y : SplayResult α} (tree : x.tree = y.tree) (rotations : x.rotations = y.rotations) :
    x = y
    @[instance_reducible]

    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
      theorem Cslib.DataStructure.SplayTree.SplitResult.ext {α : Type u_3} {x y : SplitResult α} (left : x.left = y.left) (right : x.right = y.right) (rotations : x.rotations = y.rotations) :
      x = y
      @[instance_reducible]
      structure Cslib.DataStructure.SplayTree.QueryResult (α : Type u_3) (β : Type u_4) :
      Type (max u_3 u_4)

      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
        theorem Cslib.DataStructure.SplayTree.QueryResult.ext {α : Type u_3} {β : Type u_4} {x y : QueryResult α β} (answer : x.answer = y.answer) (tree : x.tree = y.tree) (rotations : x.rotations = y.rotations) :
        x = y
        def Cslib.DataStructure.SplayTree.instDecidableEqQueryResult.decEq {α✝ : Type u_3} {β✝ : Type u_4} [DecidableEq α✝] [DecidableEq β✝] (x✝ x✝¹ : QueryResult α✝ β✝) :
        Decidable (x✝ = x✝¹)
        @[instance_reducible]
        instance Cslib.DataStructure.SplayTree.instDecidableEqQueryResult {α✝ : Type u_3} {β✝ : Type u_4} [DecidableEq α✝] [DecidableEq β✝] :
        DecidableEq (QueryResult α✝ β✝)
        def Cslib.DataStructure.SplayTree.instReprQueryResult.repr {α✝ : Type u_3} {β✝ : Type u_4} [Repr α✝] [Repr β✝] :
        QueryResult α✝ β✝ → ℕ → Std.Format
        @[instance_reducible]
        instance Cslib.DataStructure.SplayTree.instReprQueryResult {α✝ : Type u_3} {β✝ : Type u_4} [Repr α✝] [Repr β✝] :
        Repr (QueryResult α✝ β✝)

        One ancestor frame along a downward search path in a BinaryTree α.

        Instances For
          def Cslib.DataStructure.SplayTree.instDecidableEqFrame.decEq {α✝ : Type u_3} [DecidableEq α✝] (x✝ x✝¹ : Frame α✝) :
          Decidable (x✝ = x✝¹)
          @[instance_reducible]
          @[instance_reducible]
          instance Cslib.DataStructure.SplayTree.instReprFrame {α✝ : Type u_3} [Repr α✝] :
          Repr (Frame α✝)

          Reconstruct a BinaryTree α by plugging t upward through an ancestor stack ctx (ordered from parent to original root).

          @[simp]
          @[simp]
          theorem Cslib.DataStructure.SplayTree.plug_left {α : Type u_1} (t : BinaryTree α) (p : α) (r : BinaryTree α) (ctx : List (Frame α)) :
          plug t (Frame.left p r :: ctx) = plug (BinaryTree.node p t r) ctx
          @[simp]
          theorem Cslib.DataStructure.SplayTree.plug_right {α : Type u_1} (t l : BinaryTree α) (p : α) (ctx : List (Frame α)) :
          plug t (Frame.right l p :: ctx) = plug (BinaryTree.node p l t) ctx
          @[simp]
          theorem Cslib.DataStructure.SplayTree.plug_append {α : Type u_1} (t : BinaryTree α) (ctx₁ ctx₂ : List (Frame α)) :
          plug t (ctx₁ ++ ctx₂) = plug (plug t ctx₁) ctx₂

          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.

          • ctx : List (Frame α)

            Ancestor stack from parent to root.

          Instances For
            @[instance_reducible]
            def Cslib.DataStructure.SplayTree.instDecidableEqFocus.decEq {α✝ : Type u_3} [DecidableEq α✝] (x✝ x✝¹ : Focus α✝) :
            Decidable (x✝ = x✝¹)
            @[instance_reducible]
            instance Cslib.DataStructure.SplayTree.instReprFocus {α✝ : Type u_3} [Repr α✝] :
            Repr (Focus α✝)

            The subtree rooted at the focused node, ignoring its ancestors.

            Reconstruct the full tree represented by f.

            def Cslib.DataStructure.SplayTree.splayUp {α : Type u_1} (x : α) (l r : BinaryTree α) :
            List (Frame α) → SplayResult α

            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.

            @[simp]
            theorem Cslib.DataStructure.SplayTree.rotations_splayUp {α : Type u_1} (x : α) (l r : BinaryTree α) (ctx : List (Frame α)) :
            (splayUp x l r ctx).rotations = ctx.length

            Bottom-up splaying performs exactly one edge rotation per ancestor in ctx.

            theorem Cslib.DataStructure.SplayTree.exists_node_splayUp {α : Type u_1} (x : α) (l r : BinaryTree α) (ctx : List (Frame α)) :
            ∃ (l' : BinaryTree α) (r' : BinaryTree α), (splayUp x l r ctx).tree = BinaryTree.node x l' r'

            splayUp x l r ctx always roots the resulting tree at x.

            @[simp]
            theorem Cslib.DataStructure.SplayTree.rootKey_splayUp {α : Type u_1} (x : α) (l r : BinaryTree α) (ctx : List (Frame α)) :
            rootKey (splayUp x l r ctx).tree = some x
            theorem Cslib.DataStructure.SplayTree.tree_splayUp_ne_nil {α : Type u_1} (x : α) (l r : BinaryTree α) (ctx : List (Frame α)) :

            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.

            theorem Cslib.DataStructure.SplayTree.focusMin_spec {α : Type u_1} {t : BinaryTree α} {ctx : List (Frame α)} {f : Focus α} (hf : focusMin t ctx = some f) :
            theorem Cslib.DataStructure.SplayTree.focusMax_spec {α : Type u_1} {t : BinaryTree α} {ctx : List (Frame α)} {f : Focus α} (hf : focusMax t ctx = some f) :

            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.

            theorem Cslib.DataStructure.SplayTree.ordered_splayUp {α : Type u_1} [LinearOrder α] {x : α} {l r : BinaryTree α} {ctx : List (Frame α)} (h : SearchTree.Ordered (plug (BinaryTree.node x l r) ctx)) :

            splayUp preserves Ordered whenever the reconstructed initial tree is Ordered.

            def Cslib.DataStructure.SplayTree.focusSearch {α : Type u_1} [LinearOrder α] (x : α) :
            BinaryTree α → List (Frame α) → Option (Focus α)

            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).

            theorem Cslib.DataStructure.SplayTree.focusSearch_toTree_and_length {α : Type u_1} [LinearOrder α] {x : α} {t : BinaryTree α} {ctx : List (Frame α)} {f : Focus α} (hf : focusSearch x t ctx = some f) :
            theorem Cslib.DataStructure.SplayTree.focusSearch_ordered_spec {α : Type u_1} [LinearOrder α] {x : α} {t : BinaryTree α} {ctx : List (Frame α)} {f : Focus α} (ht : SearchTree.Ordered t) (hf : focusSearch x t ctx = some f) :
            f.key ∈ SearchTree.elements t ∧ (x ∈ SearchTree.elements t → f.key = x) ∧ ∀ y ∈ SearchTree.elements t, ¬(x < y ∧ y < f.key) ∧ ¬(f.key < y ∧ y < x)

            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).

            @[simp]
            theorem Cslib.DataStructure.SplayTree.splay_nil {α : Type u_1} [LinearOrder α] (x : α) :
            splay x BinaryTree.nil = { tree := BinaryTree.nil, rotations := 0 }
            @[simp]

            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.

            theorem Cslib.DataStructure.SplayTree.splay_split_lt {α : Type u_1} [LinearOrder α] {t L R : BinaryTree α} {x r : α} (ht : SearchTree.Ordered t) (hs : (splay x t).tree = BinaryTree.node r L R) (hxr : x < r) (y : α) :

            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.

            theorem Cslib.DataStructure.SplayTree.splay_split_ge {α : Type u_1} [LinearOrder α] {t L R : BinaryTree α} {x r : α} (ht : SearchTree.Ordered t) (hs : (splay x t).tree = BinaryTree.node r L R) (hrx : r ≤ x) (y : α) :

            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.

            Correctness and Invariant Preservation of Operations #

            theorem Cslib.DataStructure.SplayTree.ordered_join {α : Type u_1} [LinearOrder α] {t₁ t₂ : BinaryTree α} (h₁ : SearchTree.Ordered t₁) (h₂ : SearchTree.Ordered t₂) (hsep : ∀ x ∈ SearchTree.elements t₁, ∀ y ∈ SearchTree.elements t₂, x < y) :
            @[simp]