author | haftmann |
Thu, 19 Jun 2025 17:15:40 +0200 | |
changeset 82734 | 89347c0cc6a3 |
parent 82308 | 3529946fca19 |
permissions | -rw-r--r-- |
69133 | 1 |
(* Author: Tobias Nipkow *) |
2 |
||
3 |
section \<open>Braun Trees\<close> |
|
4 |
||
5 |
theory Braun_Tree |
|
82308 | 6 |
imports "HOL-Library.Tree_Real" |
69133 | 7 |
begin |
8 |
||
76987 | 9 |
text \<open>Braun Trees were studied by Braun and Rem~\<^cite>\<open>"BraunRem"\<close> |
10 |
and later Hoogerwoord~\<^cite>\<open>"Hoogerwoord"\<close>.\<close> |
|
69133 | 11 |
|
12 |
fun braun :: "'a tree \<Rightarrow> bool" where |
|
82308 | 13 |
"braun Leaf = True" | |
14 |
"braun (Node l x r) = ((size l = size r \<or> size l = size r + 1) \<and> braun l \<and> braun r)" |
|
69143 | 15 |
|
16 |
lemma braun_Node': |
|
17 |
"braun (Node l x r) = (size r \<le> size l \<and> size l \<le> size r + 1 \<and> braun l \<and> braun r)" |
|
82308 | 18 |
by auto |
69133 | 19 |
|
20 |
text \<open>The shape of a Braun-tree is uniquely determined by its size:\<close> |
|
21 |
||
22 |
lemma braun_unique: "\<lbrakk> braun (t1::unit tree); braun t2; size t1 = size t2 \<rbrakk> \<Longrightarrow> t1 = t2" |
|
23 |
proof (induction t1 arbitrary: t2) |
|
24 |
case Leaf thus ?case by simp |
|
25 |
next |
|
26 |
case (Node l1 _ r1) |
|
27 |
from Node.prems(3) have "t2 \<noteq> Leaf" by auto |
|
28 |
then obtain l2 x2 r2 where [simp]: "t2 = Node l2 x2 r2" by (meson neq_Leaf_iff) |
|
69143 | 29 |
with Node.prems have "size l1 = size l2 \<and> size r1 = size r2" by auto |
69133 | 30 |
thus ?case using Node.prems(1,2) Node.IH by auto |
31 |
qed |
|
32 |
||
72566
831f17da1aab
renamed "balanced" -> "acomplete" because balanced has other meanings in the literature
nipkow
parents:
71294
diff
changeset
|
33 |
text \<open>Braun trees are almost complete:\<close> |
69133 | 34 |
|
72566
831f17da1aab
renamed "balanced" -> "acomplete" because balanced has other meanings in the literature
nipkow
parents:
71294
diff
changeset
|
35 |
lemma acomplete_if_braun: "braun t \<Longrightarrow> acomplete t" |
69133 | 36 |
proof(induction t) |
72566
831f17da1aab
renamed "balanced" -> "acomplete" because balanced has other meanings in the literature
nipkow
parents:
71294
diff
changeset
|
37 |
case Leaf show ?case by (simp add: acomplete_def) |
69133 | 38 |
next |
72566
831f17da1aab
renamed "balanced" -> "acomplete" because balanced has other meanings in the literature
nipkow
parents:
71294
diff
changeset
|
39 |
case (Node l x r) thus ?case using acomplete_Node_if_wbal2 by force |
69133 | 40 |
qed |
41 |
||
69192 | 42 |
subsection \<open>Numbering Nodes\<close> |
43 |
||
44 |
text \<open>We show that a tree is a Braun tree iff a parity-based |
|
45 |
numbering (\<open>braun_indices\<close>) of nodes yields an interval of numbers.\<close> |
|
46 |
||
47 |
fun braun_indices :: "'a tree \<Rightarrow> nat set" where |
|
82308 | 48 |
"braun_indices Leaf = {}" | |
49 |
"braun_indices (Node l _ r) = {1} \<union> (*) 2 ` braun_indices l \<union> Suc ` (*) 2 ` braun_indices r" |
|
69192 | 50 |
|
69196 | 51 |
lemma braun_indices1: "0 \<notin> braun_indices t" |
82308 | 52 |
by (induction t) auto |
69196 | 53 |
|
54 |
lemma finite_braun_indices: "finite(braun_indices t)" |
|
82308 | 55 |
by (induction t) auto |
69196 | 56 |
|
71294 | 57 |
text "One direction:" |
58 |
||
69192 | 59 |
lemma braun_indices_if_braun: "braun t \<Longrightarrow> braun_indices t = {1..size t}" |
60 |
proof(induction t) |
|
61 |
case Leaf thus ?case by simp |
|
62 |
next |
|
69195 | 63 |
have *: "(*) 2 ` {a..b} \<union> Suc ` (*) 2 ` {a..b} = {2*a..2*b+1}" (is "?l = ?r") for a b |
69192 | 64 |
proof |
65 |
show "?l \<subseteq> ?r" by auto |
|
66 |
next |
|
67 |
have "\<exists>x2\<in>{a..b}. x \<in> {Suc (2*x2), 2*x2}" if *: "x \<in> {2*a .. 2*b+1}" for x |
|
68 |
proof - |
|
69 |
have "x div 2 \<in> {a..b}" using * by auto |
|
70 |
moreover have "x \<in> {2 * (x div 2), Suc(2 * (x div 2))}" by auto |
|
71 |
ultimately show ?thesis by blast |
|
72 |
qed |
|
73 |
thus "?r \<subseteq> ?l" by fastforce |
|
74 |
qed |
|
75 |
case (Node l x r) |
|
76 |
hence "size l = size r \<or> size l = size r + 1" (is "?A \<or> ?B") by auto |
|
77 |
thus ?case |
|
78 |
proof |
|
79 |
assume ?A |
|
80 |
with Node show ?thesis by (auto simp: *) |
|
81 |
next |
|
82 |
assume ?B |
|
83 |
with Node show ?thesis by (auto simp: * atLeastAtMostSuc_conv) |
|
84 |
qed |
|
85 |
qed |
|
86 |
||
71294 | 87 |
text "The other direction is more complicated. The following proof is due to Thomas Sewell." |
88 |
||
69196 | 89 |
lemma disj_evens_odds: "(*) 2 ` A \<inter> Suc ` (*) 2 ` B = {}" |
82308 | 90 |
using double_not_eq_Suc_double by auto |
69196 | 91 |
|
92 |
lemma card_braun_indices: "card (braun_indices t) = size t" |
|
93 |
proof (induction t) |
|
94 |
case Leaf thus ?case by simp |
|
95 |
next |
|
96 |
case Node |
|
97 |
thus ?case |
|
98 |
by(auto simp: UNION_singleton_eq_range finite_braun_indices card_Un_disjoint |
|
82308 | 99 |
card_insert_if disj_evens_odds card_image inj_on_def braun_indices1) |
69196 | 100 |
qed |
101 |
||
71294 | 102 |
lemma braun_indices_intvl_base_1: |
103 |
assumes bi: "braun_indices t = {m..n}" |
|
104 |
shows "{m..n} = {1..size t}" |
|
105 |
proof (cases "t = Leaf") |
|
106 |
case True then show ?thesis using bi by simp |
|
107 |
next |
|
108 |
case False |
|
109 |
note eqs = eqset_imp_iff[OF bi] |
|
110 |
from eqs[of 0] have 0: "0 < m" |
|
111 |
by (simp add: braun_indices1) |
|
112 |
from eqs[of 1] have 1: "m \<le> 1" |
|
113 |
by (cases t; simp add: False) |
|
114 |
from 0 1 have eq1: "m = 1" by simp |
|
115 |
from card_braun_indices[of t] show ?thesis |
|
116 |
by (simp add: bi eq1) |
|
117 |
qed |
|
118 |
||
119 |
lemma even_of_intvl_intvl: |
|
120 |
fixes S :: "nat set" |
|
121 |
assumes "S = {m..n} \<inter> {i. even i}" |
|
122 |
shows "\<exists>m' n'. S = (\<lambda>i. i * 2) ` {m'..n'}" |
|
82308 | 123 |
proof - |
124 |
have "S = (\<lambda>i. i * 2) ` {Suc m div 2..n div 2}" |
|
125 |
by (fastforce simp add: assms mult.commute) |
|
126 |
then show ?thesis |
|
127 |
by blast |
|
128 |
qed |
|
71294 | 129 |
|
130 |
lemma odd_of_intvl_intvl: |
|
131 |
fixes S :: "nat set" |
|
132 |
assumes "S = {m..n} \<inter> {i. odd i}" |
|
133 |
shows "\<exists>m' n'. S = Suc ` (\<lambda>i. i * 2) ` {m'..n'}" |
|
134 |
proof - |
|
82308 | 135 |
have "S = Suc ` ({if n = 0 then 1 else m - 1..n - 1} \<inter> Collect even)" |
136 |
by (auto simp: assms image_def elim!: oddE) |
|
71294 | 137 |
thus ?thesis |
138 |
by (metis even_of_intvl_intvl) |
|
139 |
qed |
|
140 |
||
141 |
lemma image_int_eq_image: |
|
142 |
"(\<forall>i \<in> S. f i \<in> T) \<Longrightarrow> (f ` S) \<inter> T = f ` S" |
|
143 |
"(\<forall>i \<in> S. f i \<notin> T) \<Longrightarrow> (f ` S) \<inter> T = {}" |
|
144 |
by auto |
|
145 |
||
146 |
lemma braun_indices1_le: |
|
147 |
"i \<in> braun_indices t \<Longrightarrow> Suc 0 \<le> i" |
|
148 |
using braun_indices1 not_less_eq_eq by blast |
|
149 |
||
150 |
lemma braun_if_braun_indices: "braun_indices t = {1..size t} \<Longrightarrow> braun t" |
|
151 |
proof(induction t) |
|
82308 | 152 |
case Leaf |
71294 | 153 |
then show ?case by simp |
154 |
next |
|
155 |
case (Node l x r) |
|
156 |
obtain t where t: "t = Node l x r" by simp |
|
82308 | 157 |
then have "insert (Suc 0) ((*) 2 ` braun_indices l \<union> Suc ` (*) 2 ` braun_indices r) \<inter> {2..} |
158 |
= {Suc 0..Suc (size l + size r)} \<inter> {2..}" |
|
159 |
by (metis Node.prems One_nat_def Suc_eq_plus1 Un_insert_left braun_indices.simps(2) |
|
160 |
sup_bot_left tree.size(4)) |
|
161 |
then have eq: "{2 .. size t} = (\<lambda>i. i * 2) ` braun_indices l \<union> Suc ` (\<lambda>i. i * 2) ` braun_indices r" |
|
71294 | 162 |
(is "?R = ?S \<union> ?T") |
82308 | 163 |
by (simp add: t mult.commute Int_Un_distrib2 image_int_eq_image braun_indices1_le) |
71294 | 164 |
then have ST: "?S = ?R \<inter> {i. even i}" "?T = ?R \<inter> {i. odd i}" |
165 |
by (simp_all add: Int_Un_distrib2 image_int_eq_image) |
|
166 |
from ST have l: "braun_indices l = {1 .. size l}" |
|
167 |
by (fastforce dest: braun_indices_intvl_base_1 dest!: even_of_intvl_intvl |
|
82308 | 168 |
simp: mult.commute inj_image_eq_iff[OF inj_onI]) |
71294 | 169 |
from ST have r: "braun_indices r = {1 .. size r}" |
170 |
by (fastforce dest: braun_indices_intvl_base_1 dest!: odd_of_intvl_intvl |
|
82308 | 171 |
simp: mult.commute inj_image_eq_iff[OF inj_onI]) |
71294 | 172 |
note STa = ST[THEN eqset_imp_iff, THEN iffD2] |
173 |
note STb = STa[of "size t"] STa[of "size t - 1"] |
|
82308 | 174 |
then have "size l = size r \<or> size l = size r + 1" |
175 |
using t l r by atomize auto |
|
176 |
with l r show ?case |
|
71294 | 177 |
by (clarsimp simp: Node.IH) |
178 |
qed |
|
179 |
||
180 |
lemma braun_iff_braun_indices: "braun t \<longleftrightarrow> braun_indices t = {1..size t}" |
|
82308 | 181 |
using braun_if_braun_indices braun_indices_if_braun by blast |
69192 | 182 |
|
69133 | 183 |
end |