Documentation

CslibFrontier.DataStructure.BTree.Height

Size and height of classic B-trees #

The three inequalities relating the size and height of a well-formed classic B-tree of minimum degree t, after [CLRS09], Chapter 18. They follow from the corresponding bounds for non-root subtrees, Occupied.pow_sub_one_le_size and Occupied.size_le. These are structural facts about the shape of a tree; they carry no cost content.

Main results #

References #

Ported from cslib-community/EpsilonEridani at commit 1966ab476: EpsilonEridani/Algorithms/Lean/BTree/Height.lean. le_mul_height_of_scan ported from cslib-community/EpsilonEridani PR #39 at commit 9425389c8.

Nodes #

theorem Cslib.DataStructure.BTree.le_size_node {α : Type u_1} {β : Type u_2} {first : BTree α β} {rest : List (Triple α β)} {n : ℕ} (h₁ : n - 1 ≤ first.size) (h₂ : ∀ e ∈ rest, n - 1 ≤ e.child.size) :
(rest.length + 1) * n - 1 ≤ (first.node rest).size

A node whose children each hold at least n - 1 entries holds at least (rest.length + 1) * n - 1.

theorem Cslib.DataStructure.BTree.size_node_le {α : Type u_1} {β : Type u_2} {first : BTree α β} {rest : List (Triple α β)} {n : ℕ} (hn : 0 < n) (h₁ : first.size ≤ n - 1) (h₂ : ∀ e ∈ rest, e.child.size ≤ n - 1) :
(first.node rest).size ≤ (rest.length + 1) * n - 1

A node whose children each hold at most n - 1 entries, for positive n, holds at most (rest.length + 1) * n - 1.

Bounds #

theorem Cslib.DataStructure.BTree.Occupied.pow_sub_one_le_size {α : Type u_1} {β : Type u_2} {t : ℕ} {tr : BTree α β} (ho : Occupied t tr) (hb : tr.Balanced) :
t ^ tr.height - 1 ≤ tr.size

A balanced occupied subtree of height h holds at least t ^ h - 1 entries.

theorem Cslib.DataStructure.BTree.Occupied.size_le {α : Type u_1} {β : Type u_2} {t : ℕ} {tr : BTree α β} (ho : Occupied t tr) :
tr.size ≤ (2 * t) ^ tr.height - 1

An occupied subtree of height h holds at most (2 * t) ^ h - 1 entries.

theorem Cslib.DataStructure.BTree.Balanced.le_size {α : Type u_1} {β : Type u_2} {t : ℕ} {tr : BTree α β} (hb : tr.Balanced) (hk : tr ≠ BTree.leaf → 1 ≤ tr.rootKeys) (ho : ∀ c ∈ tr.children, Occupied t c) (hne : tr ≠ BTree.leaf) :
2 * t ^ (tr.height - 1) - 1 ≤ tr.size

A non-empty balanced tree of height h with a key at the root and occupied children holds at least 2 * t ^ (h - 1) - 1 entries. Only the shape matters, not the order of the keys.

theorem Cslib.DataStructure.BTree.Balanced.height_le_log {α : Type u_1} {β : Type u_2} {t : ℕ} {tr : BTree α β} (ht : 2 ≤ t) (hb : tr.Balanced) (hk : tr ≠ BTree.leaf → 1 ≤ tr.rootKeys) (ho : ∀ c ∈ tr.children, Occupied t c) :
tr.height ≤ Nat.log t ((tr.size + 1) / 2) + 1

The height of a balanced tree with a key at a non-empty root and occupied children is logarithmic in its size, for 2 ≤ t. Only the shape matters, not the order of the keys.

Counts along a descent #

theorem Cslib.DataStructure.BTree.le_mul_height_of_node_le {α : Type u_1} {β : Type u_2} {t c : ℕ} {f : BTree α β → ℕ} (hleaf : f leaf = 0) (hnode : ∀ (first : BTree α β) (rest : List (Triple α β)) (B : ℕ), rest.length ≤ 2 * t - 1 → (∀ ch ∈ (first.node rest).children, f ch ≤ B) → f (first.node rest) ≤ c + B) {tr : BTree α β} (hk : tr.rootKeys ≤ 2 * t - 1) (hc : ∀ ch ∈ tr.children, Occupied t ch) :
f tr ≤ c * tr.height

A count on trees that spends at most c at a node with at most 2 * t - 1 keys beyond what it spends in one of the node's children, and nothing on the empty tree, is at most c * height on a tree whose root holds at most 2 * t - 1 keys and whose children are occupied.

theorem Cslib.DataStructure.BTree.le_mul_height_of_scan {α : Type u_1} {β : Type u_2} {t : ℕ} (ht : 0 < t) {f : BTree α β → ℕ} (hleaf : f leaf = 0) (hnode : ∀ (first : BTree α β) (rest : List (Triple α β)) (B : ℕ), (∀ c ∈ (first.node rest).children, f c ≤ B) → f (first.node rest) ≤ rest.length + 1 + B) {tr : BTree α β} (hk : tr.rootKeys ≤ 2 * t - 1) (hc : ∀ c ∈ tr.children, Occupied t c) :
f tr ≤ 2 * t * tr.height

A count on trees that spends at most rest.length + 1 at a node .node first rest beyond what it spends in one of the node's children, and nothing on the empty tree, is at most 2 * t * height, for positive t, on a tree whose root holds at most 2 * t - 1 keys and whose children are occupied.

theorem Cslib.DataStructure.BTree.IsWF.le_size {α : Type u_1} {β : Type u_2} {t : ℕ} {tr : BTree α β} [LinearOrder α] (h : IsWF t tr) (hne : tr ≠ leaf) :
2 * t ^ (tr.height - 1) - 1 ≤ tr.size

A non-empty well-formed tree of height h holds at least 2 * t ^ (h - 1) - 1 entries.

theorem Cslib.DataStructure.BTree.IsWF.size_le {α : Type u_1} {β : Type u_2} {t : ℕ} {tr : BTree α β} [LinearOrder α] (h : IsWF t tr) :
tr.size ≤ (2 * t) ^ tr.height - 1

A well-formed tree of height h holds at most (2 * t) ^ h - 1 entries.

theorem Cslib.DataStructure.BTree.IsWF.height_le_log {α : Type u_1} {β : Type u_2} {t : ℕ} {tr : BTree α β} [LinearOrder α] (h : IsWF t tr) :
tr.height ≤ Nat.log t ((tr.size + 1) / 2) + 1

The height of a well-formed tree is logarithmic in its size (Cormen et al., Theorem 18.1).