Documentation

CslibFrontier.DataStructure.BPlusTree.Insert

Insertion into B+-trees #

Insert-or-replace for the B+-trees of Cslib.DataStructure.BPlusTree, bottom-up: the entry is inserted into a leaf, and a leaf or node that overflows splits, handing a separator to its parent, which may overflow in turn; an overflow at the root grows a new root.

The two splits differ. A leaf with more than 2 * t - 1 entries keeps its first t entries, and the rest form a new leaf whose first key is copied up as the separator: every entry stays in a leaf. A node with more than 2 * t - 1 separators keeps its first t, the next separator moves up, and its child becomes the first child of the new node.

Each operation is defined once over an arbitrary monad m, with a comparator le : α → α → m Bool and a node-visit action visit : m Unit, and no order instance in scope, so that every comparison of keys goes through le. The pure operations are the Id instantiations with the comparator Cslib.SortedAssoc.leI of a linear order and visit := pure (). visit fires once at each node on the path from the root to the leaf. At an interior node the separators are scanned as in slotM, one comparison per separator passed and one more where the scan stops, and a separator equal to the key does not end the search: the key goes into the child that the separator introduces. In the leaf the entry is placed by Cslib.SortedAssoc.insertM. The recursion is structural, so the definitions are @[expose]d and decide evaluates insertion on concrete trees. Costs are not stated here.

Main definitions #

Main results #

The model equation needs more than sorted entries: the search trusts each separator to bound the later children from below, which holds through a key of the child the separator introduces, so the children must not be empty. Well-formedness provides that. The separator convention is preserved because, when no child is empty, it is a condition between neighbouring children, and an insertion changes one child or puts two in its place.

References #

The statements here are not taken from a source.

The result of inserting into a subtree: a tree of the same height, or an overflow split into two trees of the same height with the separator that goes between them.

The entries of a result in order. A separator is not an entry.

Put a result in the place of a child followed by the branches rest.

The tree of a result at the root: an overflow grows a new root.

def Cslib.DataStructure.BPlusTree.InsertResult.ofLeaf {α β : Type} (t : ℕ) (entries : List (α × β)) :

A leaf with the given entries, split when it has more than 2 * t - 1 of them: the first t entries stay on the left, the rest go right, and the first key on the right is copied up as the separator. The [] branch is unreachable.

def Cslib.DataStructure.BPlusTree.InsertResult.ofNode {α β : Type} (t : ℕ) (first : BPlusTree α β) (rest : List (Branch α β)) :

A node with contents first and rest, split when it has more than 2 * t - 1 separators: the first t branches stay on the left, the separator of the next one moves up, and its child starts the right node. The [] branch is unreachable.

@[simp]

The entries of a tree result are the tree's.

@[simp]

The entries of a split are the left entries, then the right entries.

@[simp]

Growing a new root keeps the entries.

theorem Cslib.DataStructure.BPlusTree.InsertResult.toList_withRest {α β : Type} (x : InsertResult α β) (rest : List (Branch α β)) :
(x.withRest rest).1.toList ++ List.flatMap (fun (e : Branch α β) => e.child.toList) (x.withRest rest).2 = x.toList ++ List.flatMap (fun (e : Branch α β) => e.child.toList) rest

Placing a result in the place of a child keeps the entries, followed by those of rest.

@[simp]
theorem Cslib.DataStructure.BPlusTree.InsertResult.toList_ofLeaf {α β : Type} {t : ℕ} (entries : List (α × β)) :
(ofLeaf t entries).toList = entries

Splitting an overflowing leaf keeps its entries.

@[simp]
theorem Cslib.DataStructure.BPlusTree.InsertResult.toList_ofNode {α β : Type} {t : ℕ} (first : BPlusTree α β) (rest : List (Branch α β)) :
(ofNode t first rest).toList = (first.node rest).toList

Splitting an overflowing node keeps its entries.

Shape #

structure Cslib.DataStructure.BPlusTree.Good {α β : Type} (t h : ℕ) (c : BPlusTree α β) :

A subtree of height h that is balanced and occupied: what every child of a well-formed node is.

  • balanced : c.Balanced

    The subtree is balanced.

  • height_eq : c.height = h

    The subtree has height h.

  • occupied : Occupied t c

    Every node of the subtree is occupied.

theorem Cslib.DataStructure.BPlusTree.good_node {α β : Type} {t : ℕ} {first : BPlusTree α β} {rest : List (Branch α β)} {h : ℕ} (hf : Good t h first) (hr : ∀ e ∈ rest, Good t h e.child) (h₁ : t - 1 ≤ rest.length) (h₂ : rest.length ≤ 2 * t - 1) :
Good t (h + 1) (first.node rest)

A node with t - 1 to 2 * t - 1 separators over good children of height h is good, of height h + 1.

theorem Cslib.DataStructure.BPlusTree.good_of_balanced_node {α β : Type} {t : ℕ} {first : BPlusTree α β} {rest : List (Branch α β)} (hb : (first.node rest).Balanced) (ho : ∀ c ∈ (first.node rest).children, Occupied t c) :
Good t first.height first ∧ ∀ e ∈ rest, Good t first.height e.child

The children of a balanced node whose children are occupied are good, of the height of its first child.

theorem Cslib.DataStructure.BPlusTree.InsertResult.exists_good_split_ofLeaf {α β : Type} {t : ℕ} {entries : List (α × β)} (hlo : 2 * t - 1 < entries.length) (hhi : entries.length ≤ 2 * t - 1 + t) :
∃ (l : BPlusTree α β) (s : α) (r : BPlusTree α β), ofLeaf t entries = split l s r ∧ Good t 1 l ∧ Good t 1 r

A leaf with more than 2 * t - 1 entries, and at most t more, splits into two good leaves: the first t entries go left and the rest go right.

theorem Cslib.DataStructure.BPlusTree.InsertResult.exists_good_split_ofNode {α β : Type} {t : ℕ} {first : BPlusTree α β} {rest : List (Branch α β)} {h : ℕ} (hf : Good t h first) (hr : ∀ e ∈ rest, Good t h e.child) (hlo : 2 * t - 1 < rest.length) (hhi : rest.length ≤ 2 * t - 1 + t + 1) :
∃ (l : BPlusTree α β) (s : α) (r : BPlusTree α β), ofNode t first rest = split l s r ∧ Good t (h + 1) l ∧ Good t (h + 1) r

A node over good children of height h with more than 2 * t - 1 separators, and at most t + 1 more, splits into two good trees of height h + 1: the first t branches go left, the separator of the next one moves up, and the rest go right.

def Cslib.DataStructure.BPlusTree.insertOrSplitM {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (v : β) :
BPlusTree α β → m (InsertResult α β)

Bottom-up insertion into a subtree: the tree with the entry inserted, or its split when it overflows. A node is visited once, on entry.

def Cslib.DataStructure.BPlusTree.insertRestM {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (v : β) :
List (Branch α β) → m (Option (List (Branch α β)))

Insertion among the branches of a node, scanning the separators in order with the comparisons of slotM: none defers to the child before the branches, because the key is below the first separator; some rest' is the new list of branches, with the result of the descent placed in the child after the last separator that is at most the key.

theorem Cslib.DataStructure.BPlusTree.insertOrSplitM_leaf {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (v : β) (entries : List (α × β)) :
insertOrSplitM t le visit k v (leaf entries) = do visit let __do_lift ← SortedAssoc.insertM le k v entries pure (InsertResult.ofLeaf t __do_lift)

A leaf is visited once; the entry is placed by the scan of its entries, and the leaf is split when it overflows.

theorem Cslib.DataStructure.BPlusTree.insertOrSplitM_node {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (v : β) (first : BPlusTree α β) (rest : List (Branch α β)) :
insertOrSplitM t le visit k v (first.node rest) = do visit let __do_lift ← insertRestM t le visit k v rest match __do_lift with | some rest' => pure (InsertResult.ofNode t first rest') | none => do let __do_lift ← insertOrSplitM t le visit k v first have p : BPlusTree α β × List (Branch α β) := __do_lift.withRest rest pure (InsertResult.ofNode t p.1 p.2)

A node is visited once, before its separators are searched; the result is split when it overflows.

@[simp]
theorem Cslib.DataStructure.BPlusTree.insertRestM_nil {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (v : β) :
insertRestM t le visit k v [] = pure none

The search past the last branch defers to the child before it.

theorem Cslib.DataStructure.BPlusTree.insertRestM_cons {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (v : β) (s : α) (c : BPlusTree α β) (rest : List (Branch α β)) :
insertRestM t le visit k v ((s, c) :: rest) = do let __do_lift ← le s k if __do_lift = true then do let __do_lift ← insertRestM t le visit k v rest match __do_lift with | some rest' => pure (some ((s, c) :: rest')) | none => do let __do_lift ← insertOrSplitM t le visit k v c have p : BPlusTree α β × List (Branch α β) := __do_lift.withRest rest pure (some ((s, p.1) :: p.2)) else pure none

At a branch: defer to the preceding child when its separator is above the key, and otherwise move on, descending into this branch's child when the later branches defer.

def Cslib.DataStructure.BPlusTree.insertM {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (v : β) (tr : BPlusTree α β) :
m (BPlusTree α β)

Insert-or-replace: the bottom-up insertion, growing a new root on overflow.

theorem Cslib.DataStructure.BPlusTree.insertM_def {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (v : β) (tr : BPlusTree α β) :
insertM t le visit k v tr = do let __do_lift ← insertOrSplitM t le visit k v tr pure __do_lift.toTree

Insertion is the bottom-up insertion with an overflow at the root grown into a new root.

theorem Cslib.DataStructure.BPlusTree.map_insertM {α β : Type} {m : Type → Type u_1} {n : Type → Type u_2} [Monad m] [Monad n] {f : {γ : Type} → m γ → n γ} (hf : IsMonadHom m n fun {α : Type} => f) (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (v : β) (tr : BPlusTree α β) :
f (insertM t le visit k v tr) = insertM t (fun (a b : α) => f (le a b)) (f visit) k v tr

A monad morphism commutes with insertion.

def Cslib.DataStructure.BPlusTree.insertOrSplit {α β : Type} [LinearOrder α] (t : ℕ) (k : α) (v : β) (tr : BPlusTree α β) :

Pure bottom-up insertion into a subtree.

def Cslib.DataStructure.BPlusTree.insertRest {α β : Type} [LinearOrder α] (t : ℕ) (k : α) (v : β) (rest : List (Branch α β)) :
Option (List (Branch α β))

Pure insertion among the branches of a node.

def Cslib.DataStructure.BPlusTree.insert {α β : Type} [LinearOrder α] (t : ℕ) (k : α) (v : β) (tr : BPlusTree α β) :
BPlusTree α β

Pure insertion.

theorem Cslib.DataStructure.BPlusTree.insertOrSplit_def {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} (tr : BPlusTree α β) :

insertOrSplit is the Id instantiation of insertOrSplitM.

theorem Cslib.DataStructure.BPlusTree.insertRest_def {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} (rest : List (Branch α β)) :
insertRest t k v rest = (insertRestM t SortedAssoc.leI (pure ()) k v rest).run

insertRest is the Id instantiation of insertRestM.

theorem Cslib.DataStructure.BPlusTree.insert_def {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} (tr : BPlusTree α β) :
insert t k v tr = (insertM t SortedAssoc.leI (pure ()) k v tr).run

insert is the Id instantiation of insertM.

theorem Cslib.DataStructure.BPlusTree.insert_eq_toTree {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} (tr : BPlusTree α β) :
insert t k v tr = (insertOrSplit t k v tr).toTree

Insertion is the bottom-up insertion with an overflow grown into a new root.

theorem Cslib.DataStructure.BPlusTree.insertOrSplit_leaf {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} (entries : List (α × β)) :

Insertion into a leaf places the entry in its entry list and splits on overflow.

theorem Cslib.DataStructure.BPlusTree.insertOrSplit_node {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} (first : BPlusTree α β) (rest : List (Branch α β)) :
insertOrSplit t k v (first.node rest) = match insertRest t k v rest with | some rest' => InsertResult.ofNode t first rest' | none => have p := (insertOrSplit t k v first).withRest rest; InsertResult.ofNode t p.1 p.2

Insertion into a node: search its separators, descending into the first child when the key is below them, then split on overflow.

@[simp]
theorem Cslib.DataStructure.BPlusTree.insertRest_nil {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} :

The search past the last branch defers to the child before it.

theorem Cslib.DataStructure.BPlusTree.insertRest_cons {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} (s : α) (c : BPlusTree α β) (rest : List (Branch α β)) :
insertRest t k v ((s, c) :: rest) = if s ≤ k then match insertRest t k v rest with | some rest' => some ((s, c) :: rest') | none => have p := (insertOrSplit t k v c).withRest rest; some ((s, p.1) :: p.2) else none

The search at a branch: defer when its separator is above the key, and otherwise move on, descending into this branch's child when the later branches defer.

The model equation #

The shape of the result #

The separator convention of the result #

The separator convention between neighbours: each separator is above the keys of the child before it and at most the keys of its own child. When no child is empty this is the whole convention at a node (separated_node_of_linked), and insertion changes it at one place.

theorem Cslib.DataStructure.BPlusTree.Separated.linked {α β : Type} [LinearOrder α] {first : BPlusTree α β} {rest : List (Branch α β)} (h : (first.node rest).Separated) :
first.Linked rest

The separator convention at a node holds between its neighbouring children.

theorem Cslib.DataStructure.BPlusTree.separated_node_of_linked {α β : Type} [LinearOrder α] {first : BPlusTree α β} {rest : List (Branch α β)} (hl : first.Linked rest) (hf : first.Separated) (hr : ∀ e ∈ rest, e.child.Separated) (hne : ∀ e ∈ rest, e.child.toList ≠ []) :
(first.node rest).Separated

When no child among the branches is empty, the separator convention between neighbouring children, over separated children, is the convention at the node.

The separator convention of a result: a separated tree, or two separated trees with the keys of the left one below the separator and those of the right one at least it.

theorem Cslib.DataStructure.BPlusTree.sep_ofLeaf {α β : Type} [LinearOrder α] {t : ℕ} {entries : List (α × β)} (hs : entries.IsSortedAssoc) :

The split of a leaf with sorted entries has the separator convention.

theorem Cslib.DataStructure.BPlusTree.sep_ofNode {α β : Type} [LinearOrder α] {t : ℕ} {first : BPlusTree α β} {rest : List (Branch α β)} (h : (first.node rest).Separated) (hne : ∀ e ∈ rest, e.child.toList ≠ []) :
Sep (InsertResult.ofNode t first rest)

The split of a separated node without empty children among its branches has the separator convention.

theorem Cslib.DataStructure.BPlusTree.mem_toList_of_mem_withRest_fst {α β : Type} {x : InsertResult α β} {rest : List (Branch α β)} {p : α × β} (hp : p ∈ (x.withRest rest).1.toList) :

The entries of the tree that takes the place of a child are entries of the result.

theorem Cslib.DataStructure.BPlusTree.Linked.of_forall_lt {α β : Type} [LinearOrder α] {a b : BPlusTree α β} {rest : List (Branch α β)} (hl : a.Linked rest) (hb : ∀ (s : α) (c : BPlusTree α β) (rest' : List (α × BPlusTree α β)), rest = (s, c) :: rest' → ∀ p ∈ b.toList, p.1 < s) :
b.Linked rest

The convention between neighbours looks at a child only through the bound of the next separator on its keys.

theorem Cslib.DataStructure.BPlusTree.linked_withRest_of_sep {α β : Type} [LinearOrder α] {x : InsertResult α β} {c : BPlusTree α β} {rest : List (Branch α β)} (hx : Sep x) (hl : c.Linked rest) (hr : ∀ e ∈ rest, e.child.Separated) (hb : ∀ (s : α) (c' : BPlusTree α β) (rest' : List (α × BPlusTree α β)), rest = (s, c') :: rest' → ∀ p ∈ x.toList, p.1 < s) :
(x.withRest rest).1.Linked (x.withRest rest).2 ∧ (x.withRest rest).1.Separated ∧ ∀ e ∈ (x.withRest rest).2, e.child.Separated

Placing a result with the separator convention in the place of a child linked to rest keeps the convention between neighbours, when the keys of the result are below the next separator.

Insertion on well-formed trees #

theorem Cslib.DataStructure.BPlusTree.toList_insert {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (h : IsWF t tr) (k : α) (v : β) :

Model equation of insertion on well-formed trees.

theorem Cslib.DataStructure.BPlusTree.IsWF.insert {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (h : IsWF t tr) (k : α) (v : β) :
IsWF t (BPlusTree.insert t k v tr)

Insertion preserves well-formedness.

theorem Cslib.DataStructure.BPlusTree.height_insert {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (h : IsWF t tr) (k : α) (v : β) :
(insert t k v tr).height = match insertOrSplit t k v tr with | InsertResult.tree tr_1 => tr.height | InsertResult.split left sep right => tr.height + 1

On a well-formed tree, insertion keeps the height unless the root splits, and then raises it by one.

theorem Cslib.DataStructure.BPlusTree.height_insert_le {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (h : IsWF t tr) (k : α) (v : β) :
(insert t k v tr).height ≤ tr.height + 1

Insertion raises the height by at most one.

theorem Cslib.DataStructure.BPlusTree.height_le_height_insert {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (h : IsWF t tr) (k : α) (v : β) :
tr.height ≤ (insert t k v tr).height

Insertion never lowers the height.

theorem Cslib.DataStructure.BPlusTree.size_insert {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (h : IsWF t tr) (k : α) (v : β) :
(insert t k v tr).size = if (lookup k tr).isSome = true then tr.size else tr.size + 1

Insertion keeps the size when the key is present and adds one otherwise.