theory BTree imports Main "HOL-Data_Structures.Sorted_Less""HOL-Data_Structures.Cmp" begin
(* some setup to cover up the redefinition of sorted in Sorted_Less
but keep the lemmas *)
hide_const (open) Sorted_Less.sorted abbreviation"sorted_less ≡ Sorted_Less.sorted"
section"Definition of the B-Tree"
subsection"Datatype definition"
text"B-trees can be considered to have all data stored interleaved as child nodes and separating elements (also keys or indices). We define them to either be a Node that holds a list of pairs of children and indices or be a completely empty Leaf."
text"The order of a B-tree is defined just as in the original paper by Bayer."
(* alt1: following knuths definition to allow for any
natural number as order and resolve ambiguity *) (* alt2: use range [k,2*k] allowing for valid btrees
from k=1 onwards NOTE this is what I ended up implementing *)
fun order:: "nat ==> 'a btree ==> bool"where "order k Leaf = True" | "order k (Node ts t) = ( (length ts ≥ k) ∧ (length ts ≤ 2*k) ∧ (∀sub ∈ set (subtrees ts). order k sub) ∧ order k t )"
text‹The special condition for the root is called \textit{root\_order}›
(* the invariant for the root of the btree *) fun root_order:: "nat ==> 'a btree ==> bool"where "root_order k Leaf = True" | "root_order k (Node ts t) = ( (length ts > 0) ∧ (length ts ≤ 2*k) ∧ (∀s ∈ set (subtrees ts). order k s) ∧ order k t )"
subsection"Auxiliary Lemmas"
(* auxiliary lemmas when handling sets *) lemma separators_split: "set (separators (l@(a,b)#r)) = set (separators l) ∪ set (separators r) ∪ {b}" by simp
lemma subtrees_split: "set (subtrees (l@(a,b)#r)) = set (subtrees l) ∪ set (subtrees r) ∪ {a}" by simp
(* height and set lemmas *)
lemma finite_set_ins_swap: assumes"finite A" shows"max a (Max (Set.insert b A)) = max b (Max (Set.insert a A))" using Max_insert assms max.commute max.left_commute by fastforce
lemma finite_set_in_idem: assumes"finite A" shows"max a (Max (Set.insert a A)) = Max (Set.insert a A)" using Max_insert assms max.commute max.left_commute by fastforce
lemma height_Leaf: "height t = 0 ⟷ t = Leaf" by (induction t) (auto)
corollary sorted_inorder_subtrees: "sorted_less (inorder (Node ts t)) ==>∀ sub ∈set (subtrees ts). sorted_less (inorder sub)" using sorted_inorder_list_subtrees sorted_wrt_append by auto
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.