Documentation

CslibFrontier.DataStructure.BTree.Basic

Classic B-trees #

The B-trees of [BMcC72], in the minimum-degree form of [CLRS09], Chapter 18. A node is a first child followed by a list of (key, value, child) triples (BTree.Triple), so a node with m keys has m + 1 children by construction; leaf is the empty tree. The tree is a map from keys to values, specified by toList against the sorted association list model List.IsSortedAssoc. toList traverses any tree in structural in-order (the first child, then each key-value pair followed by its child); under IsWF (its sorted field) the keys are strictly increasing, which gives map semantics.

height counts the nodes on a root-to-leaf path: the leaf has height 0 and a node has height one more than its tallest child. Well-formedness at minimum degree t is the hypothesis IsWF t tr, never part of the type: t ≥ 2, every non-root node has between t - 1 and 2 * t - 1 keys (Occupied), a non-leaf root has between 1 and 2 * t - 1 keys, all leaves are at the same depth (Balanced), and toList is key-sorted. Taking t = 2 gives 2-3-4 trees.

induction is the induction principle for the nested type; proofs use it rather than the raw nested recursor. The Boolean checkers occupied, balanced and isWF decide the predicates, so that well-formedness of concrete trees is checked by decide. The operations and checkers are @[expose]d so that decide and rfl evaluate them from other modules.

Main definitions #

Main results #

References #

Ported from cslib-community/EpsilonEridani at commit 1966ab476: EpsilonEridani/Algorithms/Lean/BTree/Basic.lean.

inductive Cslib.DataStructure.BTree (α : Type u) (β : Type v) :
Type (max u v)

A classic B-tree. A node is a first child followed by (key, value, child) triples, so a node with m keys has m + 1 children by construction. leaf is the empty tree.

  • leaf {α : Type u} {β : Type v} : BTree α β

    The empty tree.

  • node {α : Type u} {β : Type v} (first : BTree α β) (rest : List (α × β × BTree α β)) : BTree α β

    A node: its first child, then the keys, values and remaining children in order.

Instances For
    @[reducible, inline]
    abbrev Cslib.DataStructure.BTree.Triple (α : Type u) (β : Type v) :
    Type (max u v)

    A (key, value, child) triple of a node: a key, its value, and the child holding the keys between this key and the next. The key is the first component e.1; Triple.val and Triple.child name the other two.

    @[reducible, inline]
    abbrev Cslib.DataStructure.BTree.Triple.val {α : Type u} {β : Type v} (e : Triple α β) :
    β

    The value of a triple.

    @[reducible, inline]
    abbrev Cslib.DataStructure.BTree.Triple.child {α : Type u} {β : Type v} (e : Triple α β) :
    BTree α β

    The child of a triple, which follows its key.

    @[simp]
    theorem Cslib.DataStructure.BTree.Triple.val_mk {α : Type u} {β : Type v} (k : α) (v : β) (c : BTree α β) :
    val (k, v, c) = v

    The value of a triple built from its components.

    @[simp]
    theorem Cslib.DataStructure.BTree.Triple.child_mk {α : Type u} {β : Type v} (k : α) (v : β) (c : BTree α β) :
    child (k, v, c) = c

    The child of a triple built from its components.

    def Cslib.DataStructure.BTree.toList {α : Type u} {β : Type v} :
    BTree α β → List (α × β)

    Entries in structural in-order: the first child's, then each key-value pair followed by its child's. The keys are strictly increasing under IsWF (its sorted field).

    def Cslib.DataStructure.BTree.toListRest {α : Type u} {β : Type v} :
    List (Triple α β) → List (α × β)

    Entries of the triples following a first child, each key before its child's entries.

    def Cslib.DataStructure.BTree.height {α : Type u} {β : Type v} :
    BTree α β → ℕ

    Nodes on a root-to-leaf path: the leaf has height 0.

    def Cslib.DataStructure.BTree.heightRest {α : Type u} {β : Type v} :
    List (Triple α β) → ℕ

    Height contribution of the children in a list of triples.

    def Cslib.DataStructure.BTree.bimap {α : Type u} {α' : Type u'} {β : Type v} {γ : Type w} (g : α → α') (f : β → γ) :
    BTree α β → BTree α' γ

    Map over keys and values, keeping shape.

    def Cslib.DataStructure.BTree.bimapRest {α : Type u} {α' : Type u'} {β : Type v} {γ : Type w} (g : α → α') (f : β → γ) :
    List (Triple α β) → List (Triple α' γ)

    Map over the keys and values of a list of triples.

    def Cslib.DataStructure.BTree.map {α : Type u} {β : Type v} {γ : Type w} (f : β → γ) :
    BTree α β → BTree α γ

    Map over values, keeping keys and shape.

    def Cslib.DataStructure.BTree.mapRest {α : Type u} {β : Type v} {γ : Type w} (f : β → γ) :
    List (Triple α β) → List (Triple α γ)

    Map over the values of a list of triples.

    def Cslib.DataStructure.BTree.mapKeys {α : Type u} {α' : Type u'} {β : Type v} (g : α → α') :
    BTree α β → BTree α' β

    Map over keys, keeping values and shape.

    def Cslib.DataStructure.BTree.mapKeysRest {α : Type u} {α' : Type u'} {β : Type v} (g : α → α') :
    List (Triple α β) → List (Triple α' β)

    Map over the keys of a list of triples.

    def Cslib.DataStructure.BTree.size {α : Type u} {β : Type v} (tr : BTree α β) :

    Number of entries.

    def Cslib.DataStructure.BTree.keys {α : Type u} {β : Type v} (tr : BTree α β) :
    List α

    Keys in structural in-order, as in toList; strictly increasing under IsWF.

    def Cslib.DataStructure.BTree.foldl {α : Type u} {β : Type v} {γ : Type w} (f : γ → α × β → γ) (init : γ) (tr : BTree α β) :
    γ

    Fold over entries in structural in-order, as in toList, which is key order under IsWF; the specification of any direct recursive fold.

    @[instance_reducible]
    instance Cslib.DataStructure.BTree.instMembershipProd {α : Type u} {β : Type v} :
    Membership (α × β) (BTree α β)

    An entry belongs to a tree when it belongs to its toList.

    def Cslib.DataStructure.BTree.rootKeys {α : Type u} {β : Type v} :
    BTree α β → ℕ

    Number of keys at the root node.

    def Cslib.DataStructure.BTree.children {α : Type u} {β : Type v} :
    BTree α β → List (BTree α β)

    Children of the root node.

    inductive Cslib.DataStructure.BTree.Occupied {α : Type u} {β : Type v} (t : ℕ) :
    BTree α β → Prop

    Every node satisfies the occupancy bounds of a non-root node of minimum degree t: between t - 1 and 2 * t - 1 keys.

    Instances For
      inductive Cslib.DataStructure.BTree.Balanced {α : Type u} {β : Type v} :
      BTree α β → Prop

      All leaves at the same depth: every child of a node has the height of the first child.

      Instances For
        structure Cslib.DataStructure.BTree.IsWF {α : Type u} {β : Type v} [LinearOrder α] (t : ℕ) (tr : BTree α β) :

        Well-formed B-tree of minimum degree t: root occupancy, non-root occupancy, equal leaf depth, and key-sortedness of toList.

        • two_le : 2 ≤ t

          The minimum degree is at least 2.

        • rootKeys_le : tr.rootKeys ≤ 2 * t - 1

          The root has at most 2 * t - 1 keys.

        • rootKeys_pos : tr ≠ leaf → 1 ≤ tr.rootKeys

          A non-empty root has at least one key.

        • occupied_of_mem_children (c : BTree α β) : c ∈ tr.children → Occupied t c

          Every child of the root is occupied.

        • balanced : tr.Balanced

          All leaves are at the same depth.

        • sorted : tr.toList.IsSortedAssoc

          The entries are sorted by strictly increasing key.

        Instances For
          def Cslib.DataStructure.BTree.occupied {α : Type u} {β : Type v} (t : ℕ) :
          BTree α β → Bool

          Boolean check of Occupied t.

          def Cslib.DataStructure.BTree.occupiedRest {α : Type u} {β : Type v} (t : ℕ) :
          List (Triple α β) → Bool

          Boolean check that every child in a list of triples is occupied.

          def Cslib.DataStructure.BTree.balanced {α : Type u} {β : Type v} :
          BTree α β → Bool

          Boolean check of Balanced.

          def Cslib.DataStructure.BTree.balancedRest {α : Type u} {β : Type v} (h : ℕ) :
          List (Triple α β) → Bool

          Boolean check that every child in a list of triples is balanced and of height h.

          def Cslib.DataStructure.BTree.isWF {α : Type u} {β : Type v} [LinearOrder α] (t : ℕ) (tr : BTree α β) :

          Boolean check of IsWF t.

          Equation lemmas #

          @[simp]

          The empty tree has no entries.

          @[simp]
          theorem Cslib.DataStructure.BTree.toList_node {α : Type u} {β : Type v} {first : BTree α β} {rest : List (Triple α β)} :
          (first.node rest).toList = first.toList ++ toListRest rest

          The entries of a node: those of its first child, then those of the triples.

          An empty list of triples has no entries.

          theorem Cslib.DataStructure.BTree.toListRest_cons {α : Type u} {β : Type v} {rest : List (Triple α β)} {k : α} {v : β} {c : BTree α β} :
          toListRest ((k, v, c) :: rest) = (k, v) :: c.toList ++ toListRest rest

          The entries of a list of triples: the key, its child's entries, then the rest.

          @[simp]
          theorem Cslib.DataStructure.BTree.toListRest_eq_flatMap {α : Type u} {β : Type v} {rest : List (Triple α β)} :
          toListRest rest = List.flatMap (fun (e : Triple α β) => (e.1, e.val) :: e.child.toList) rest

          The entries of a list of triples: each key-value pair followed by its child's entries.

          theorem Cslib.DataStructure.BTree.toListRest_map_leaf {α : Type u} {β : Type v} (l : List (α × β)) :
          toListRest (List.map (fun (p : α × β) => (p.1, p.2, leaf)) l) = l

          A list of entries, each over an empty child, has those entries. Not @[simp]: the simp normal form of toListRest is the flatMap of toListRest_eq_flatMap.

          @[simp]

          The empty tree has size 0.

          @[simp]

          The empty tree has height 0.

          @[simp]
          theorem Cslib.DataStructure.BTree.height_node {α : Type u} {β : Type v} {first : BTree α β} {rest : List (Triple α β)} :
          (first.node rest).height = max (first.height + 1) (heightRest rest)

          The height of a node is one more than that of its tallest child.

          An empty list of triples contributes height 0.

          theorem Cslib.DataStructure.BTree.heightRest_cons {α : Type u} {β : Type v} {rest : List (Triple α β)} {k : α} {v : β} {c : BTree α β} :
          heightRest ((k, v, c) :: rest) = max (c.height + 1) (heightRest rest)

          The height contribution of a list of triples is that of its tallest child.

          @[simp]
          theorem Cslib.DataStructure.BTree.heightRest_eq_foldr {α : Type u} {β : Type v} {rest : List (Triple α β)} :
          heightRest rest = List.foldr max 0 (List.map (fun (e : Triple α β) => e.child.height + 1) rest)

          The height contribution of a list of triples is the maximum of its children's heights plus one, or 0 for the empty list.

          theorem Cslib.DataStructure.BTree.heightRest_le_iff {α : Type u} {β : Type v} {rest : List (Triple α β)} {n : ℕ} :
          heightRest rest ≤ n ↔ ∀ e ∈ rest, e.child.height + 1 ≤ n

          The children among a list of triples are all shorter than n exactly when the list's height contribution is at most n.

          @[simp]
          theorem Cslib.DataStructure.BTree.bimap_leaf {α : Type u} {α' : Type u'} {β : Type v} {γ : Type w} (g : α → α') (f : β → γ) :

          Mapping keys and values of the empty tree gives the empty tree.

          @[simp]
          theorem Cslib.DataStructure.BTree.bimap_node {α : Type u} {α' : Type u'} {β : Type v} {γ : Type w} {first : BTree α β} {rest : List (Triple α β)} (g : α → α') (f : β → γ) :
          bimap g f (first.node rest) = (bimap g f first).node (bimapRest g f rest)

          Mapping keys and values of a node maps its first child and its triples.

          @[simp]
          theorem Cslib.DataStructure.BTree.bimapRest_eq_map {α : Type u} {α' : Type u'} {β : Type v} {γ : Type w} {rest : List (Triple α β)} (g : α → α') (f : β → γ) :
          bimapRest g f rest = List.map (fun (e : Triple α β) => (g e.1, f e.val, bimap g f e.child)) rest

          Mapping keys and values of a list of triples maps the key, the value and the child of each triple.

          @[simp]
          theorem Cslib.DataStructure.BTree.map_leaf {α : Type u} {β : Type v} {γ : Type w} (f : β → γ) :

          Mapping the empty tree gives the empty tree.

          @[simp]
          theorem Cslib.DataStructure.BTree.map_node {α : Type u} {β : Type v} {γ : Type w} {first : BTree α β} {rest : List (Triple α β)} (f : β → γ) :
          map f (first.node rest) = (map f first).node (mapRest f rest)

          Mapping a node maps its first child and its triples.

          @[simp]
          theorem Cslib.DataStructure.BTree.mapRest_eq_map {α : Type u} {β : Type v} {γ : Type w} {rest : List (Triple α β)} (f : β → γ) :
          mapRest f rest = List.map (fun (e : Triple α β) => (e.1, f e.val, map f e.child)) rest

          Mapping a list of triples maps the value and the child of each triple, keeping its key.

          @[simp]
          theorem Cslib.DataStructure.BTree.mapKeys_leaf {α : Type u} {α' : Type u'} {β : Type v} (g : α → α') :

          Mapping keys over the empty tree gives the empty tree.

          @[simp]
          theorem Cslib.DataStructure.BTree.mapKeys_node {α : Type u} {α' : Type u'} {β : Type v} (g : α → α') (first : BTree α β) (rest : List (Triple α β)) :
          mapKeys g (first.node rest) = (mapKeys g first).node (mapKeysRest g rest)

          Mapping keys over a node maps its first child and its triples.

          @[simp]
          theorem Cslib.DataStructure.BTree.mapKeysRest_eq_map {α : Type u} {α' : Type u'} {β : Type v} (g : α → α') (rest : List (Triple α β)) :
          mapKeysRest g rest = List.map (fun (e : Triple α β) => (g e.1, e.val, mapKeys g e.child)) rest

          Mapping the keys of a list of triples maps the key and the child of each triple, keeping its value.

          @[simp]

          The empty tree has no children.

          @[simp]
          theorem Cslib.DataStructure.BTree.children_node {α : Type u} {β : Type v} {first : BTree α β} {rest : List (Triple α β)} :
          (first.node rest).children = first :: List.map Triple.child rest

          The children of a node: its first child, then the children of its triples.

          @[simp]

          The empty tree has no keys at its root.

          @[simp]
          theorem Cslib.DataStructure.BTree.rootKeys_node {α : Type u} {β : Type v} {first : BTree α β} {rest : List (Triple α β)} :
          (first.node rest).rootKeys = rest.length

          A node has one root key per triple.

          theorem Cslib.DataStructure.BTree.toList_node_cons {α : Type u} {β : Type v} {first : BTree α β} {rest : List (Triple α β)} {k : α} {v : β} {c : BTree α β} :
          (first.node ((k, v, c) :: rest)).toList = first.toList ++ (k, v) :: (c.node rest).toList

          The first separator splits a node's entries into those of its first child, the separator, and those of the node without its first child and separator.

          theorem Cslib.DataStructure.BTree.mem_toList_node_cons {α : Type u} {β : Type v} {first : BTree α β} {rest : List (Triple α β)} {k : α} {v : β} {c : BTree α β} {p : α × β} :
          p ∈ (first.node ((k, v, c) :: rest)).toList ↔ p ∈ first.toList ∨ p = (k, v) ∨ p ∈ (c.node rest).toList

          Membership in a node with keys: in its first child, equal to its first separator, or in the node without its first child and separator.

          theorem Cslib.DataStructure.BTree.children_node_subset_children_node_cons {α : Type u} {β : Type v} {first : BTree α β} {rest : List (Triple α β)} {k : α} {v : β} {c : BTree α β} :
          (c.node rest).children ⊆ (first.node ((k, v, c) :: rest)).children

          Dropping a node's first child and first separator keeps the remaining children.

          Induction #

          theorem Cslib.DataStructure.BTree.sizeOf_lt_of_mem_children {α : Type u} {β : Type v} {first c : BTree α β} {rest : List (Triple α β)} (hc : c ∈ (first.node rest).children) :
          sizeOf c < sizeOf (first.node rest)

          A child of a node is smaller than the node: the measure for recursion into a child.

          theorem Cslib.DataStructure.BTree.induction {α : Type u} {β : Type v} {motive : BTree α β → Prop} (leaf : motive leaf) (node : ∀ (first : BTree α β) (rest : List (Triple α β)), motive first → (∀ e ∈ rest, motive e.child) → motive (first.node rest)) (tr : BTree α β) :
          motive tr

          Induction on B-trees, with a hypothesis for the first child and for every child among the triples. This replaces the raw nested recursor in proofs.

          theorem Cslib.DataStructure.BTree.induction_children {α : Type u} {β : Type v} {motive : BTree α β → Prop} (leaf : motive leaf) (node : ∀ (first : BTree α β) (rest : List (Triple α β)), (∀ c ∈ (first.node rest).children, motive c) → motive (first.node rest)) (tr : BTree α β) :
          motive tr

          Induction on B-trees with one hypothesis for all children of a node.

          Basic API #

          @[simp]
          theorem Cslib.DataStructure.BTree.mem_def {α : Type u} {β : Type v} {tr : BTree α β} {p : α × β} :
          p ∈ tr ↔ p ∈ tr.toList

          Membership in a tree is membership in its entries.

          @[simp]
          theorem Cslib.DataStructure.BTree.length_toList {α : Type u} {β : Type v} {tr : BTree α β} :

          The length of the entries is the size.

          theorem Cslib.DataStructure.BTree.length_toListRest {α : Type u} {β : Type v} {rest : List (Triple α β)} :
          (toListRest rest).length = rest.length + (List.map (fun (e : Triple α β) => e.child.size) rest).sum

          The entries of a list of triples: one per key, plus those of the children.

          theorem Cslib.DataStructure.BTree.size_node {α : Type u} {β : Type v} {first : BTree α β} {rest : List (Triple α β)} :
          (first.node rest).size = first.size + rest.length + (List.map (fun (e : Triple α β) => e.child.size) rest).sum

          The size of a node: its first child's, one per key, and its other children's.

          theorem Cslib.DataStructure.BTree.foldl_def {α : Type u} {β : Type v} {γ : Type w} {tr : BTree α β} (f : γ → α × β → γ) (init : γ) :
          foldl f init tr = List.foldl f init tr.toList

          foldl is the fold over toList.

          theorem Cslib.DataStructure.BTree.keys_def {α : Type u} {β : Type v} {tr : BTree α β} :

          keys is the list of first components of toList.

          @[simp]
          theorem Cslib.DataStructure.BTree.mem_keys {α : Type u} {β : Type v} {tr : BTree α β} {k : α} :
          k ∈ tr.keys ↔ ∃ (v : β), (k, v) ∈ tr

          A key belongs to a tree's keys when some entry of the tree carries it.

          @[simp]
          theorem Cslib.DataStructure.BTree.height_eq_zero_iff {α : Type u} {β : Type v} {tr : BTree α β} :
          tr.height = 0 ↔ tr = leaf

          Only the empty tree has height 0.

          @[simp]
          theorem Cslib.DataStructure.BTree.toList_bimap {α : Type u} {α' : Type u'} {β : Type v} {γ : Type w} {tr : BTree α β} (g : α → α') (f : β → γ) :
          (bimap g f tr).toList = List.map (fun (p : α × β) => (g p.1, f p.2)) tr.toList

          Mapping over keys and values maps the entries.

          @[simp]
          theorem Cslib.DataStructure.BTree.toList_map {α : Type u} {β : Type v} {γ : Type w} {tr : BTree α β} (f : β → γ) :
          (map f tr).toList = List.map (fun (p : α × β) => (p.1, f p.2)) tr.toList

          Mapping over values maps the entries, keeping keys.

          @[simp]
          theorem Cslib.DataStructure.BTree.toList_mapKeys {α : Type u} {α' : Type u'} {β : Type v} (g : α → α') (tr : BTree α β) :
          (mapKeys g tr).toList = List.map (fun (x : α × β) => (g x.1, x.2)) tr.toList

          Mapping keys maps the entries, keeping values.

          @[simp]
          theorem Cslib.DataStructure.BTree.height_map {α : Type u} {β : Type v} {γ : Type w} {tr : BTree α β} (f : β → γ) :
          (map f tr).height = tr.height

          Mapping over values keeps the height.

          @[simp]
          theorem Cslib.DataStructure.BTree.size_map {α : Type u} {β : Type v} {γ : Type w} {tr : BTree α β} (f : β → γ) :
          (map f tr).size = tr.size

          Mapping over values keeps the size.

          @[simp]
          theorem Cslib.DataStructure.BTree.keys_map {α : Type u} {β : Type v} {γ : Type w} {tr : BTree α β} (f : β → γ) :
          (map f tr).keys = tr.keys

          Mapping over values keeps the keys.

          @[simp]
          theorem Cslib.DataStructure.BTree.map_eq_leaf_iff {α : Type u} {β : Type v} {γ : Type w} {tr : BTree α β} (f : β → γ) :
          map f tr = leaf ↔ tr = leaf

          Mapping over values gives the empty tree exactly when the tree is empty.

          @[simp]
          theorem Cslib.DataStructure.BTree.rootKeys_map {α : Type u} {β : Type v} {γ : Type w} {tr : BTree α β} (f : β → γ) :

          Mapping over values keeps the number of root keys.

          @[simp]
          theorem Cslib.DataStructure.BTree.children_bimap {α : Type u} {α' : Type u'} {β : Type v} {γ : Type w} {tr : BTree α β} (g : α → α') (f : β → γ) :
          (bimap g f tr).children = List.map (fun (x : BTree α β) => bimap g f x) tr.children

          The children of a tree with mapped keys and values are the mapped children.

          @[simp]
          theorem Cslib.DataStructure.BTree.children_map {α : Type u} {β : Type v} {γ : Type w} {tr : BTree α β} (f : β → γ) :
          (map f tr).children = List.map (fun (x : BTree α β) => map f x) tr.children

          The children of a mapped tree are the mapped children.

          @[simp]
          theorem Cslib.DataStructure.BTree.children_mapKeys {α : Type u} {α' : Type u'} {β : Type v} {tr : BTree α β} (g : α → α') :
          (mapKeys g tr).children = List.map (fun (x : BTree α β) => mapKeys g x) tr.children

          The children of a tree with mapped keys are the mapped children.

          @[simp]
          theorem Cslib.DataStructure.BTree.map_id {α : Type u} {β : Type v} {tr : BTree α β} :
          map id tr = tr

          Mapping the identity leaves the tree unchanged.

          @[simp]
          theorem Cslib.DataStructure.BTree.map_map {α : Type u} {β : Type v} {γ : Type w} {tr : BTree α β} {δ : Type u_1} (g : γ → δ) (f : β → γ) :
          map g (map f tr) = map (g ∘ f) tr

          Mapping twice is mapping the composition.

          theorem Cslib.DataStructure.BTree.toListRest_append {α : Type u} {β : Type v} {l₁ l₂ : List (Triple α β)} :
          toListRest (l₁ ++ l₂) = toListRest l₁ ++ toListRest l₂

          The entries of a concatenation of triple lists are the concatenated entries.

          theorem Cslib.DataStructure.BTree.toList_sublist_toListRest_of_mem {α : Type u} {β : Type v} {rest : List (Triple α β)} {e : Triple α β} (he : e ∈ rest) :

          The entries of a child among the triples form a sublist of the triples' entries.

          theorem Cslib.DataStructure.BTree.toList_sublist_of_mem_children {α : Type u} {β : Type v} {tr c : BTree α β} (hc : c ∈ tr.children) :

          The entries of a child of the root form a sublist of the tree's entries.

          theorem Cslib.DataStructure.BTree.height_succ_le_of_mem_rest {α : Type u} {β : Type v} {first : BTree α β} {rest : List (Triple α β)} {e : Triple α β} (he : e ∈ rest) :
          e.child.height + 1 ≤ (first.node rest).height

          Every child among the triples of a node is shorter than the node.

          theorem Cslib.DataStructure.BTree.height_first_succ_le {α : Type u} {β : Type v} {first : BTree α β} {rest : List (Triple α β)} :
          first.height + 1 ≤ (first.node rest).height

          The first child of a node is shorter than the node.

          theorem Cslib.DataStructure.BTree.height_node_le_height_node_cons {α : Type u} {β : Type v} {first c : BTree α β} {k : α} {v : β} {rest : List (Triple α β)} :
          (c.node rest).height ≤ (first.node ((k, v, c) :: rest)).height

          Dropping a node's first child and first key leaves a tree no taller than the node.

          theorem Cslib.DataStructure.BTree.height_succ_le_of_mem_children {α : Type u} {β : Type v} {tr c : BTree α β} (hc : c ∈ tr.children) :

          Every child of a tree is shorter than the tree.

          Inversion lemmas #

          theorem Cslib.DataStructure.BTree.Balanced.height_of_mem_rest {α : Type u} {β : Type v} {fst : BTree α β} {rest : List (Triple α β)} (h : (fst.node rest).Balanced) {e : Triple α β} (he : e ∈ rest) :

          Every child among the triples of a balanced node has the height of the first child.

          theorem Cslib.DataStructure.BTree.Balanced.height_node {α : Type u} {β : Type v} {fst : BTree α β} {rest : List (Triple α β)} (h : (fst.node rest).Balanced) :
          (fst.node rest).height = fst.height + 1

          A balanced node is one taller than its first child.

          theorem Cslib.DataStructure.BTree.Balanced.first {α : Type u} {β : Type v} {fst : BTree α β} {rest : List (Triple α β)} (h : (fst.node rest).Balanced) :

          The first child of a balanced node is balanced.

          theorem Cslib.DataStructure.BTree.Balanced.of_mem_rest {α : Type u} {β : Type v} {fst : BTree α β} {rest : List (Triple α β)} (h : (fst.node rest).Balanced) {e : Triple α β} (he : e ∈ rest) :

          Every child among the triples of a balanced node is balanced.

          theorem Cslib.DataStructure.BTree.balanced_node_of_forall {α : Type u} {β : Type v} {first : BTree α β} {rest : List (Triple α β)} (hf : first.Balanced) (hr : ∀ e ∈ rest, e.child.Balanced ∧ e.child.height = first.height) :
          (first.node rest).Balanced ∧ (first.node rest).height = first.height + 1

          A node whose children are balanced and as tall as its first child is balanced, and one taller than its first child.

          theorem Cslib.DataStructure.BTree.Balanced.height_of_mem_children {α : Type u} {β : Type v} {tr c : BTree α β} (h : tr.Balanced) (hc : c ∈ tr.children) :
          c.height + 1 = tr.height

          Children of a balanced tree are one level shorter.

          theorem Cslib.DataStructure.BTree.Balanced.of_mem_children {α : Type u} {β : Type v} {tr c : BTree α β} (h : tr.Balanced) (hc : c ∈ tr.children) :

          Children of a balanced tree are balanced.

          theorem Cslib.DataStructure.BTree.Occupied.first {α : Type u} {β : Type v} {fst : BTree α β} {rest : List (Triple α β)} {t : ℕ} (h : Occupied t (fst.node rest)) :
          Occupied t fst

          The first child of an occupied node is occupied.

          theorem Cslib.DataStructure.BTree.Occupied.of_mem_rest {α : Type u} {β : Type v} {fst : BTree α β} {rest : List (Triple α β)} {t : ℕ} (h : Occupied t (fst.node rest)) {e : Triple α β} (he : e ∈ rest) :

          Every child among the triples of an occupied node is occupied.

          theorem Cslib.DataStructure.BTree.Occupied.length_le {α : Type u} {β : Type v} {fst : BTree α β} {rest : List (Triple α β)} {t : ℕ} (h : Occupied t (fst.node rest)) :
          rest.length ≤ 2 * t - 1

          An occupied node has at most 2 * t - 1 keys.

          theorem Cslib.DataStructure.BTree.Occupied.le_length {α : Type u} {β : Type v} {fst : BTree α β} {rest : List (Triple α β)} {t : ℕ} (h : Occupied t (fst.node rest)) :
          t - 1 ≤ rest.length

          An occupied node has at least t - 1 keys.

          theorem Cslib.DataStructure.BTree.Occupied.rootKeys_le {α : Type u} {β : Type v} {t : ℕ} {c : BTree α β} (h : Occupied t c) :
          c.rootKeys ≤ 2 * t - 1

          An occupied tree has at most 2 * t - 1 root keys.

          theorem Cslib.DataStructure.BTree.Occupied.le_rootKeys {α : Type u} {β : Type v} {t : ℕ} {c : BTree α β} (h : Occupied t c) (hne : c ≠ BTree.leaf) :
          t - 1 ≤ c.rootKeys

          An occupied node has at least t - 1 root keys.

          theorem Cslib.DataStructure.BTree.Occupied.one_le_rootKeys {α : Type u} {β : Type v} {t : ℕ} {c : BTree α β} (ht : 2 ≤ t) (h : Occupied t c) (hne : c ≠ BTree.leaf) :

          An occupied node has at least one key when 2 ≤ t.

          theorem Cslib.DataStructure.BTree.Occupied.of_mem_children {α : Type u} {β : Type v} {tr : BTree α β} {t : ℕ} {c : BTree α β} (h : Occupied t tr) (hc : c ∈ tr.children) :

          Children of an occupied tree are occupied.

          @[simp]
          theorem Cslib.DataStructure.BTree.occupied_map {α : Type u} {β : Type v} {γ : Type w} {tr : BTree α β} {t : ℕ} (f : β → γ) :
          Occupied t (map f tr) ↔ Occupied t tr

          Mapping over values preserves occupancy.

          @[simp]
          theorem Cslib.DataStructure.BTree.balanced_map {α : Type u} {β : Type v} {γ : Type w} {tr : BTree α β} (f : β → γ) :

          Mapping over values preserves balance.

          theorem Cslib.DataStructure.BTree.occupied_children_node_cons {α : Type u} {β : Type v} {t : ℕ} {first c : BTree α β} {k : α} {v : β} {rest : List (Triple α β)} (hc : ∀ c' ∈ (first.node ((k, v, c) :: rest)).children, Occupied t c') :
          Occupied t first ∧ (∀ c' ∈ first.children, Occupied t c') ∧ ∀ c' ∈ (c.node rest).children, Occupied t c'

          Splitting a node with occupied children at its first separator: the first child is occupied, and so are the children of the first child and of the node without its first child and separator.

          theorem Cslib.DataStructure.BTree.IsWF.occupied_first {α : Type u} {β : Type v} [LinearOrder α] {t : ℕ} {first : BTree α β} {rest : List (Triple α β)} (h : IsWF t (first.node rest)) :
          Occupied t first

          The first child of a well-formed node is occupied.

          theorem Cslib.DataStructure.BTree.IsWF.occupied_of_mem_rest {α : Type u} {β : Type v} [LinearOrder α] {t : ℕ} {first : BTree α β} {rest : List (Triple α β)} {e : Triple α β} (h : IsWF t (first.node rest)) (he : e ∈ rest) :

          Every child among the triples of a well-formed node is occupied.

          theorem Cslib.DataStructure.BTree.IsWF.sorted_of_mem_children {α : Type u} {β : Type v} {tr : BTree α β} [LinearOrder α] {t : ℕ} {c : BTree α β} (h : IsWF t tr) (hc : c ∈ tr.children) :

          Every child of a well-formed tree has sorted entries.

          theorem Cslib.DataStructure.BTree.Occupied.isWF {α : Type u} {β : Type v} [LinearOrder α] {t : ℕ} {c : BTree α β} (ht : 2 ≤ t) (ho : Occupied t c) (hb : c.Balanced) (hs : c.toList.IsSortedAssoc) :
          IsWF t c

          An occupied, balanced tree with sorted entries is well-formed when 2 ≤ t: the occupancy of a non-root node implies the root rule.

          theorem Cslib.DataStructure.BTree.IsWF.of_mem_children {α : Type u} {β : Type v} {tr : BTree α β} [LinearOrder α] {t : ℕ} {c : BTree α β} (h : IsWF t tr) (hc : c ∈ tr.children) :
          IsWF t c

          Every subtree of a well-formed tree is well-formed: non-root occupancy implies the root rule when 2 ≤ t.

          @[simp]
          theorem Cslib.DataStructure.BTree.isWF_map {α : Type u} {β : Type v} {γ : Type w} {tr : BTree α β} [LinearOrder α] {t : ℕ} (f : β → γ) :
          IsWF t (map f tr) ↔ IsWF t tr

          Mapping over values preserves well-formedness.

          theorem Cslib.DataStructure.BTree.isSortedAssoc_toList_node_cons_iff {α : Type u} {β : Type v} [LinearOrder α] {c first : BTree α β} {k : α} {v : β} {rest : List (Triple α β)} :
          (first.node ((k, v, c) :: rest)).toList.IsSortedAssoc ↔ first.toList.IsSortedAssoc ∧ (c.node rest).toList.IsSortedAssoc ∧ (∀ p ∈ first.toList, p.1 < k) ∧ ∀ p ∈ (c.node rest).toList, k < p.1

          A node's entries are sorted exactly when those of its first child and of the node without its first child and separator are, with the first separator strictly between them.

          theorem Cslib.DataStructure.BTree.induction_isSortedAssoc {α : Type u} {β : Type v} [LinearOrder α] {motive : (tr : BTree α β) → tr.toList.IsSortedAssoc → Prop} (leaf : motive leaf ⋯) (node_nil : ∀ (first : BTree α β) (hf : first.toList.IsSortedAssoc), motive first hf → ∀ {h : (first.node []).toList.IsSortedAssoc}, motive (first.node []) h) (node_cons : ∀ (first : BTree α β) (k : α) (v : β) (c : BTree α β) (rest : List (α × β × BTree α β)) (h₁ : first.toList.IsSortedAssoc) (h₂ : (c.node rest).toList.IsSortedAssoc), (∀ p ∈ first.toList, p.1 < k) → (∀ p ∈ (c.node rest).toList, k < p.1) → motive first h₁ → motive (c.node rest) h₂ → ∀ {h : (first.node ((k, v, c) :: rest)).toList.IsSortedAssoc}, motive (first.node ((k, v, c) :: rest)) h) {tr : BTree α β} (h : tr.toList.IsSortedAssoc) :
          motive tr h

          Induction on a B-tree with sorted entries, splitting a node with keys at its first separator into first and node c rest.

          Decidability #

          @[simp]
          theorem Cslib.DataStructure.BTree.occupiedRest_eq_all {α : Type u} {β : Type v} {rest : List (Triple α β)} {t : ℕ} :
          occupiedRest t rest = rest.all fun (e : Triple α β) => occupied t e.child

          occupiedRest checks every child among the triples with occupied.

          @[simp]
          theorem Cslib.DataStructure.BTree.balancedRest_eq_all {α : Type u} {β : Type v} {rest : List (Triple α β)} {n : ℕ} :
          balancedRest n rest = rest.all fun (e : Triple α β) => e.child.balanced && e.child.height == n

          balancedRest n checks that every child among the triples is balanced and of height n.

          @[simp]
          theorem Cslib.DataStructure.BTree.occupied_iff {α : Type u} {β : Type v} {tr : BTree α β} {t : ℕ} :

          occupied decides Occupied.

          @[simp]
          theorem Cslib.DataStructure.BTree.balanced_iff {α : Type u} {β : Type v} {tr : BTree α β} :

          balanced decides Balanced.

          @[simp]
          theorem Cslib.DataStructure.BTree.isWF_iff {α : Type u} {β : Type v} {tr : BTree α β} {t : ℕ} [LinearOrder α] :
          isWF t tr = true ↔ IsWF t tr

          isWF decides IsWF.

          @[instance_reducible]
          instance Cslib.DataStructure.BTree.instDecidableOccupied {α : Type u} {β : Type v} (t : ℕ) (tr : BTree α β) :
          @[instance_reducible]
          @[instance_reducible]
          instance Cslib.DataStructure.BTree.instDecidableIsWF {α : Type u} {β : Type v} [LinearOrder α] (t : ℕ) (tr : BTree α β) :