Documentation

Complexity.Algorithms.Lean.SearchTree.Basic

Unbalanced binary search trees #

An ordinary binary search tree reuses Mathlib's BinaryTree. Keys strictly smaller than the root occur on the left, larger keys on the right. Inserting a duplicate leaves the tree unchanged. These are the textbook search and insertion algorithms of Cormen et al., Chapter 12.

Main definitions #

Main results #

Trees are not balanced. Cost bounds in the companion module depend on their actual height.

References #

The finite set of keys stored at internal nodes.

Instances For

    Strict subtree separation rules out duplicate keys.

    Instances For

      Follow a single search path; no subtree is scanned.

      Instances For

        Insert at the first empty child on the search path; equality preserves the existing node.

        Instances For

          Follow left children to find the least key.

          Instances For

            The least stored key strictly greater than x, including when x is absent.

            Instances For
              @[simp]

              Membership in a node's element set splits across the root key and the two subtrees.

              Search is correct for any shape satisfying the invariant.

              Insertion adds exactly the requested key, even when it was already present.

              theorem Cslib.Algorithms.Lean.SearchTree.ordered_insert {α : Type u_1} [LinearOrder α] {t : BinaryTree α} (ht : Ordered t) (x : α) :

              The binary-search-tree invariant survives insertion.

              theorem Cslib.Algorithms.Lean.SearchTree.insert_eq_self {α : Type u_1} [LinearOrder α] {t : BinaryTree α} (ht : Ordered t) {x : α} (hx : x ∈ elements t) :
              insert x t = t

              Re-inserting a present key preserves the entire tree, not just its represented set.

              A strict search tree stores each represented key in exactly one node.

              Left descent returns the least element, with the empty case accounted for.

              theorem Cslib.Algorithms.Lean.SearchTree.successor_spec {α : Type u_1} [LinearOrder α] {t : BinaryTree α} (ht : Ordered t) (x : α) :
              OrderedSet.LeastResult ({x_1 ∈ elements t | x < x_1}) (successor x t)

              Successor is strict and does not require the query itself to belong to the tree.