Documentation

Examples.Language.Splay.Search

The original subtree selected by a binary-search-tree access #

searchFocus is ordinary mathematical BST search. It returns the original subtree rooted at the matching node, or at the last node visited on an unsuccessful search. It does not rotate a tree, model memory or assign costs. The splay implementation's correctness theorem can therefore specify its new root without maintaining a second executable splay algorithm.

Node labels are stable natural-number IDs; keys gives their mathematical keys. Orderedness is exactly mathlib's List.Pairwise on the inorder key sequence.

The original subtree at the matching or last visited node of ordinary BST search.

Equations
Instances For

    The number of edges to nonempty children traversed by ordinary BST search. This is a structural path length, not a price assigned to executable operations.

    Equations
    Instances For
      theorem Complexity.Language.Examples.Splay.searchFocus_of_eq (keys : ℕ → ℕ) (query root : ℕ) (left right : Tree ℕ) (h : keys root = query) :
      searchFocus keys query (Tree.node root left right) = Tree.node root left right
      theorem Complexity.Language.Examples.Splay.searchFocus_left_nil (keys : ℕ → ℕ) (query root : ℕ) (right : Tree ℕ) (h : query < keys root) :
      searchFocus keys query (Tree.node root Tree.nil right) = Tree.node root Tree.nil right
      theorem Complexity.Language.Examples.Splay.searchFocus_right_nil (keys : ℕ → ℕ) (query root : ℕ) (left : Tree ℕ) (h : keys root < query) :
      searchFocus keys query (Tree.node root left Tree.nil) = Tree.node root left Tree.nil
      theorem Complexity.Language.Examples.Splay.searchFocus_left (keys : ℕ → ℕ) (query root : ℕ) (left right : Tree ℕ) (h : query < keys root) (hne : left ≠ Tree.nil) :
      searchFocus keys query (Tree.node root left right) = searchFocus keys query left
      theorem Complexity.Language.Examples.Splay.searchFocus_right (keys : ℕ → ℕ) (query root : ℕ) (left right : Tree ℕ) (h : keys root < query) (hne : right ≠ Tree.nil) :
      searchFocus keys query (Tree.node root left right) = searchFocus keys query right
      @[simp]
      theorem Complexity.Language.Examples.Splay.searchDepth_of_eq (keys : ℕ → ℕ) (query root : ℕ) (left right : Tree ℕ) (h : keys root = query) :
      searchDepth keys query (Tree.node root left right) = 0
      theorem Complexity.Language.Examples.Splay.searchDepth_left_nil (keys : ℕ → ℕ) (query root : ℕ) (right : Tree ℕ) (h : query < keys root) :
      searchDepth keys query (Tree.node root Tree.nil right) = 0
      theorem Complexity.Language.Examples.Splay.searchDepth_right_nil (keys : ℕ → ℕ) (query root : ℕ) (left : Tree ℕ) (h : keys root < query) :
      searchDepth keys query (Tree.node root left Tree.nil) = 0
      theorem Complexity.Language.Examples.Splay.searchDepth_left (keys : ℕ → ℕ) (query root : ℕ) (left right : Tree ℕ) (h : query < keys root) (hne : left ≠ Tree.nil) :
      searchDepth keys query (Tree.node root left right) = searchDepth keys query left + 1
      theorem Complexity.Language.Examples.Splay.searchDepth_right (keys : ℕ → ℕ) (query root : ℕ) (left right : Tree ℕ) (h : keys root < query) (hne : right ≠ Tree.nil) :
      searchDepth keys query (Tree.node root left right) = searchDepth keys query right + 1
      theorem Complexity.Language.Examples.Splay.searchDepth_le_numNodes (keys : ℕ → ℕ) (query : ℕ) (tree : Tree ℕ) :
      searchDepth keys query tree ≤ tree.numNodes

      Ordinary search traverses no more edges than there are original internal nodes.

      An original subtree, reached by zero or more child edges without any rotation.

      Instances For

        At the last visited node, the key matches or the requested child is empty.

        Equations
        Instances For
          theorem Complexity.Language.Examples.Splay.searchFocus_spec (keys : ℕ → ℕ) (query : ℕ) (tree : Tree ℕ) :
          IsSubtree (searchFocus keys query tree) tree ∧ (searchFocus keys query tree = Tree.nil ↔ tree = Tree.nil) ∧ SearchTerminal keys query (searchFocus keys query tree)

          Ordinary search selects an original subtree and stops at precisely a terminal node.

          theorem Complexity.Language.Examples.Splay.searchFocus_subtree (keys : ℕ → ℕ) (query : ℕ) (tree : Tree ℕ) :
          IsSubtree (searchFocus keys query tree) tree
          theorem Complexity.Language.Examples.Splay.searchFocus_ne_nil (keys : ℕ → ℕ) (query : ℕ) {tree : Tree ℕ} (hne : tree ≠ Tree.nil) :
          searchFocus keys query tree ≠ Tree.nil
          theorem Complexity.Language.Examples.Splay.searchFocus_terminal (keys : ℕ → ℕ) (query : ℕ) (tree : Tree ℕ) :
          SearchTerminal keys query (searchFocus keys query tree)
          theorem Complexity.Language.Examples.Splay.IsSubtree.root_mem {focus tree : Tree ℕ} (sub : IsSubtree focus tree) {root : ℕ} (hroot : focus.root? = some root) :
          root ∈ tree.inorder

          A selected nonempty subtree retains an actual node of the original input.

          theorem Complexity.Language.Examples.Splay.searchFocus_root_mem (keys : ℕ → ℕ) (query : ℕ) (tree : Tree ℕ) {root : ℕ} (hroot : (searchFocus keys query tree).root? = some root) :
          root ∈ tree.inorder

          The root returned by mathematical search belongs to the original inorder list.

          theorem Complexity.Language.Examples.Splay.searchFocus_root_of_mem (keys : ℕ → ℕ) (query : ℕ) (tree : Tree ℕ) (ordered : List.Pairwise (fun (x1 x2 : ℕ) => x1 < x2) (List.map keys tree.inorder)) (found : query ∈ List.map keys tree.inorder) :
          Option.map keys (searchFocus keys query tree).root? = some query

          On an ordered tree, any existing query is the key at the selected root.

          theorem Complexity.Language.Examples.Splay.searchFocus_missing_child (keys : ℕ → ℕ) (query : ℕ) (tree : Tree ℕ) (missing : ¬query ∈ List.map keys tree.inorder) {root : ℕ} {left right : Tree ℕ} (hfocus : searchFocus keys query tree = Tree.node root left right) :
          query < keys root ∧ left = Tree.nil ∨ keys root < query ∧ right = Tree.nil

          An unsuccessful search ends at a node whose requested child is empty.