author | nipkow |
Thu, 06 Oct 2016 11:38:05 +0200 | |
changeset 64065 | 40d440b75b00 |
parent 64018 | c6eb691770d8 |
child 64444 | daae191c9344 |
permissions | -rw-r--r-- |
63829 | 1 |
(* Author: Tobias Nipkow *) |
63643 | 2 |
|
63829 | 3 |
section \<open>Creating Balanced Trees\<close> |
63643 | 4 |
|
63829 | 5 |
theory Balance |
63643 | 6 |
imports |
64018 | 7 |
Complex_Main |
63643 | 8 |
"~~/src/HOL/Library/Tree" |
9 |
begin |
|
10 |
||
64018 | 11 |
(* mv *) |
12 |
||
13 |
text \<open>The lemmas about \<open>floor\<close> and \<open>ceiling\<close> of \<open>log 2\<close> should be generalized |
|
64065 | 14 |
from 2 to \<open>n\<close> and should be made executable. In the end they should be moved |
15 |
to theory \<open>Log_Nat\<close> and \<open>floorlog\<close> should be replaced.\<close> |
|
64018 | 16 |
|
64065 | 17 |
lemma floor_log_nat_ivl: fixes b n k :: nat |
64018 | 18 |
assumes "b \<ge> 2" "b^n \<le> k" "k < b^(n+1)" |
19 |
shows "floor (log b (real k)) = int(n)" |
|
20 |
proof - |
|
21 |
have "k \<ge> 1" |
|
22 |
using assms(1,2) one_le_power[of b n] by linarith |
|
23 |
show ?thesis |
|
24 |
proof(rule floor_eq2) |
|
25 |
show "int n \<le> log b k" |
|
26 |
using assms(1,2) \<open>k \<ge> 1\<close> |
|
27 |
by(simp add: powr_realpow le_log_iff of_nat_power[symmetric] del: of_nat_power) |
|
28 |
next |
|
29 |
have "real k < b powr (real(n + 1))" using assms(1,3) |
|
30 |
by (simp only: powr_realpow) (metis of_nat_less_iff of_nat_power) |
|
31 |
thus "log b k < real_of_int (int n) + 1" |
|
32 |
using assms(1) \<open>k \<ge> 1\<close> by(simp add: log_less_iff add_ac) |
|
33 |
qed |
|
34 |
qed |
|
35 |
||
64065 | 36 |
lemma ceil_log_nat_ivl: fixes b n k :: nat |
64018 | 37 |
assumes "b \<ge> 2" "b^n < k" "k \<le> b^(n+1)" |
38 |
shows "ceiling (log b (real k)) = int(n)+1" |
|
39 |
proof(rule ceiling_eq) |
|
40 |
show "int n < log b k" |
|
41 |
using assms(1,2) |
|
42 |
by(simp add: powr_realpow less_log_iff of_nat_power[symmetric] del: of_nat_power) |
|
43 |
next |
|
44 |
have "real k \<le> b powr (real(n + 1))" |
|
45 |
using assms(1,3) |
|
46 |
by (simp only: powr_realpow) (metis of_nat_le_iff of_nat_power) |
|
47 |
thus "log b k \<le> real_of_int (int n) + 1" |
|
48 |
using assms(1,2) by(simp add: log_le_iff add_ac) |
|
49 |
qed |
|
50 |
||
51 |
lemma ceil_log2_div2: assumes "n \<ge> 2" |
|
52 |
shows "ceiling(log 2 (real n)) = ceiling(log 2 ((n-1) div 2 + 1)) + 1" |
|
53 |
proof cases |
|
54 |
assume "n=2" |
|
55 |
thus ?thesis by simp |
|
56 |
next |
|
57 |
let ?m = "(n-1) div 2 + 1" |
|
58 |
assume "n\<noteq>2" |
|
59 |
hence "2 \<le> ?m" |
|
60 |
using assms by arith |
|
61 |
then obtain i where i: "2 ^ i < ?m" "?m \<le> 2 ^ (i + 1)" |
|
62 |
using ex_power_ivl2[of 2 ?m] by auto |
|
63 |
have "n \<le> 2*?m" |
|
64 |
by arith |
|
65 |
also have "2*?m \<le> 2 ^ ((i+1)+1)" |
|
66 |
using i(2) by simp |
|
67 |
finally have *: "n \<le> \<dots>" . |
|
68 |
have "2^(i+1) < n" |
|
69 |
using i(1) by (auto simp add: less_Suc_eq_0_disj) |
|
64065 | 70 |
from ceil_log_nat_ivl[OF _ this *] ceil_log_nat_ivl[OF _ i] |
64018 | 71 |
show ?thesis by simp |
72 |
qed |
|
73 |
||
74 |
lemma floor_log2_div2: fixes n :: nat assumes "n \<ge> 2" |
|
75 |
shows "floor(log 2 n) = floor(log 2 (n div 2)) + 1" |
|
76 |
proof cases |
|
77 |
assume "n=2" |
|
78 |
thus ?thesis by simp |
|
79 |
next |
|
80 |
let ?m = "n div 2" |
|
81 |
assume "n\<noteq>2" |
|
82 |
hence "1 \<le> ?m" |
|
83 |
using assms by arith |
|
84 |
then obtain i where i: "2 ^ i \<le> ?m" "?m < 2 ^ (i + 1)" |
|
85 |
using ex_power_ivl1[of 2 ?m] by auto |
|
86 |
have "2^(i+1) \<le> 2*?m" |
|
87 |
using i(1) by simp |
|
88 |
also have "2*?m \<le> n" |
|
89 |
by arith |
|
90 |
finally have *: "2^(i+1) \<le> \<dots>" . |
|
91 |
have "n < 2^(i+1+1)" |
|
92 |
using i(2) by simp |
|
64065 | 93 |
from floor_log_nat_ivl[OF _ * this] floor_log_nat_ivl[OF _ i] |
64018 | 94 |
show ?thesis by simp |
95 |
qed |
|
96 |
||
97 |
(* end of mv *) |
|
98 |
||
63643 | 99 |
fun bal :: "'a list \<Rightarrow> nat \<Rightarrow> 'a tree * 'a list" where |
100 |
"bal xs n = (if n=0 then (Leaf,xs) else |
|
101 |
(let m = n div 2; |
|
102 |
(l, ys) = bal xs m; |
|
103 |
(r, zs) = bal (tl ys) (n-1-m) |
|
104 |
in (Node l (hd ys) r, zs)))" |
|
105 |
||
106 |
declare bal.simps[simp del] |
|
107 |
||
63829 | 108 |
definition balance_list :: "'a list \<Rightarrow> 'a tree" where |
109 |
"balance_list xs = fst (bal xs (length xs))" |
|
110 |
||
111 |
definition balance_tree :: "'a tree \<Rightarrow> 'a tree" where |
|
112 |
"balance_tree = balance_list o inorder" |
|
113 |
||
63843 | 114 |
lemma bal_simps: |
115 |
"bal xs 0 = (Leaf, xs)" |
|
116 |
"n > 0 \<Longrightarrow> |
|
117 |
bal xs n = |
|
118 |
(let m = n div 2; |
|
64018 | 119 |
(l, ys) = bal xs m; |
120 |
(r, zs) = bal (tl ys) (n-1-m) |
|
63843 | 121 |
in (Node l (hd ys) r, zs))" |
122 |
by(simp_all add: bal.simps) |
|
63643 | 123 |
|
63861 | 124 |
text\<open>The following lemmas take advantage of the fact |
125 |
that \<open>bal xs n\<close> yields a result even if \<open>n > length xs\<close>.\<close> |
|
126 |
||
127 |
lemma size_bal: "bal xs n = (t,ys) \<Longrightarrow> size t = n" |
|
128 |
proof(induction xs n arbitrary: t ys rule: bal.induct) |
|
129 |
case (1 xs n) |
|
130 |
thus ?case |
|
131 |
by(cases "n=0") |
|
132 |
(auto simp add: bal_simps Let_def split: prod.splits) |
|
133 |
qed |
|
134 |
||
63643 | 135 |
lemma bal_inorder: |
63755
182c111190e5
Renamed balanced to complete; added balanced; more about both
nipkow
parents:
63663
diff
changeset
|
136 |
"\<lbrakk> bal xs n = (t,ys); n \<le> length xs \<rbrakk> |
182c111190e5
Renamed balanced to complete; added balanced; more about both
nipkow
parents:
63663
diff
changeset
|
137 |
\<Longrightarrow> inorder t = take n xs \<and> ys = drop n xs" |
63643 | 138 |
proof(induction xs n arbitrary: t ys rule: bal.induct) |
139 |
case (1 xs n) show ?case |
|
140 |
proof cases |
|
63843 | 141 |
assume "n = 0" thus ?thesis using 1 by (simp add: bal_simps) |
63643 | 142 |
next |
143 |
assume [arith]: "n \<noteq> 0" |
|
144 |
let ?n1 = "n div 2" let ?n2 = "n - 1 - ?n1" |
|
145 |
from "1.prems" obtain l r xs' where |
|
146 |
b1: "bal xs ?n1 = (l,xs')" and |
|
147 |
b2: "bal (tl xs') ?n2 = (r,ys)" and |
|
148 |
t: "t = \<langle>l, hd xs', r\<rangle>" |
|
63843 | 149 |
by(auto simp: Let_def bal_simps split: prod.splits) |
63643 | 150 |
have IH1: "inorder l = take ?n1 xs \<and> xs' = drop ?n1 xs" |
151 |
using b1 "1.prems" by(intro "1.IH"(1)) auto |
|
152 |
have IH2: "inorder r = take ?n2 (tl xs') \<and> ys = drop ?n2 (tl xs')" |
|
153 |
using b1 b2 IH1 "1.prems" by(intro "1.IH"(2)) auto |
|
154 |
have "drop (n div 2) xs \<noteq> []" using "1.prems"(2) by simp |
|
155 |
hence "hd (drop ?n1 xs) # take ?n2 (tl (drop ?n1 xs)) = take (?n2 + 1) (drop ?n1 xs)" |
|
156 |
by (metis Suc_eq_plus1 take_Suc) |
|
157 |
hence *: "inorder t = take n xs" using t IH1 IH2 |
|
158 |
using take_add[of ?n1 "?n2+1" xs] by(simp) |
|
159 |
have "n - n div 2 + n div 2 = n" by simp |
|
160 |
hence "ys = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric]) |
|
161 |
thus ?thesis using * by blast |
|
162 |
qed |
|
163 |
qed |
|
164 |
||
63829 | 165 |
corollary inorder_balance_list: "inorder(balance_list xs) = xs" |
63643 | 166 |
using bal_inorder[of xs "length xs"] |
63829 | 167 |
by (metis balance_list_def order_refl prod.collapse take_all) |
63643 | 168 |
|
64018 | 169 |
corollary inorder_balance_tree[simp]: "inorder(balance_tree t) = inorder t" |
170 |
by(simp add: balance_tree_def inorder_balance_list) |
|
171 |
||
172 |
corollary size_balance_list[simp]: "size(balance_list xs) = length xs" |
|
173 |
by (metis inorder_balance_list length_inorder) |
|
174 |
||
175 |
corollary size_balance_tree[simp]: "size(balance_tree t) = size t" |
|
176 |
by(simp add: balance_tree_def inorder_balance_list) |
|
177 |
||
178 |
lemma min_height_bal: |
|
179 |
"bal xs n = (t,ys) \<Longrightarrow> min_height t = nat(floor(log 2 (n + 1)))" |
|
63643 | 180 |
proof(induction xs n arbitrary: t ys rule: bal.induct) |
181 |
case (1 xs n) show ?case |
|
182 |
proof cases |
|
183 |
assume "n = 0" thus ?thesis |
|
64018 | 184 |
using "1.prems" by (simp add: bal_simps) |
63643 | 185 |
next |
186 |
assume [arith]: "n \<noteq> 0" |
|
187 |
from "1.prems" obtain l r xs' where |
|
188 |
b1: "bal xs (n div 2) = (l,xs')" and |
|
189 |
b2: "bal (tl xs') (n - 1 - n div 2) = (r,ys)" and |
|
190 |
t: "t = \<langle>l, hd xs', r\<rangle>" |
|
63843 | 191 |
by(auto simp: bal_simps Let_def split: prod.splits) |
64018 | 192 |
let ?log1 = "nat (floor(log 2 (n div 2 + 1)))" |
193 |
let ?log2 = "nat (floor(log 2 (n - 1 - n div 2 + 1)))" |
|
63643 | 194 |
have IH1: "min_height l = ?log1" using "1.IH"(1) b1 by simp |
64018 | 195 |
have IH2: "min_height r = ?log2" using "1.IH"(2) b1 b2 by simp |
196 |
have "(n+1) div 2 \<ge> 1" by arith |
|
197 |
hence 0: "log 2 ((n+1) div 2) \<ge> 0" by simp |
|
198 |
have "n - 1 - n div 2 + 1 \<le> n div 2 + 1" by arith |
|
199 |
hence le: "?log2 \<le> ?log1" |
|
200 |
by(simp add: nat_mono floor_mono) |
|
201 |
have "min_height t = min ?log1 ?log2 + 1" by (simp add: t IH1 IH2) |
|
202 |
also have "\<dots> = ?log2 + 1" using le by (simp add: min_absorb2) |
|
203 |
also have "n - 1 - n div 2 + 1 = (n+1) div 2" by linarith |
|
204 |
also have "nat (floor(log 2 ((n+1) div 2))) + 1 |
|
205 |
= nat (floor(log 2 ((n+1) div 2) + 1))" |
|
206 |
using 0 by linarith |
|
207 |
also have "\<dots> = nat (floor(log 2 (n + 1)))" |
|
208 |
using floor_log2_div2[of "n+1"] by (simp add: log_mult) |
|
209 |
finally show ?thesis . |
|
210 |
qed |
|
211 |
qed |
|
212 |
||
213 |
lemma height_bal: |
|
214 |
"bal xs n = (t,ys) \<Longrightarrow> height t = nat \<lceil>log 2 (n + 1)\<rceil>" |
|
215 |
proof(induction xs n arbitrary: t ys rule: bal.induct) |
|
216 |
case (1 xs n) show ?case |
|
217 |
proof cases |
|
218 |
assume "n = 0" thus ?thesis |
|
219 |
using "1.prems" by (simp add: bal_simps) |
|
220 |
next |
|
221 |
assume [arith]: "n \<noteq> 0" |
|
222 |
from "1.prems" obtain l r xs' where |
|
223 |
b1: "bal xs (n div 2) = (l,xs')" and |
|
224 |
b2: "bal (tl xs') (n - 1 - n div 2) = (r,ys)" and |
|
225 |
t: "t = \<langle>l, hd xs', r\<rangle>" |
|
226 |
by(auto simp: bal_simps Let_def split: prod.splits) |
|
227 |
let ?log1 = "nat \<lceil>log 2 (n div 2 + 1)\<rceil>" |
|
228 |
let ?log2 = "nat \<lceil>log 2 (n - 1 - n div 2 + 1)\<rceil>" |
|
229 |
have IH1: "height l = ?log1" using "1.IH"(1) b1 by simp |
|
230 |
have IH2: "height r = ?log2" using "1.IH"(2) b1 b2 by simp |
|
231 |
have 0: "log 2 (n div 2 + 1) \<ge> 0" by auto |
|
232 |
have "n - 1 - n div 2 + 1 \<le> n div 2 + 1" by arith |
|
233 |
hence le: "?log2 \<le> ?log1" |
|
234 |
by(simp add: nat_mono ceiling_mono del: nat_ceiling_le_eq) |
|
235 |
have "height t = max ?log1 ?log2 + 1" by (simp add: t IH1 IH2) |
|
236 |
also have "\<dots> = ?log1 + 1" using le by (simp add: max_absorb1) |
|
237 |
also have "\<dots> = nat \<lceil>log 2 (n div 2 + 1) + 1\<rceil>" using 0 by linarith |
|
238 |
also have "\<dots> = nat \<lceil>log 2 (n + 1)\<rceil>" |
|
239 |
using ceil_log2_div2[of "n+1"] by (simp) |
|
63643 | 240 |
finally show ?thesis . |
241 |
qed |
|
242 |
qed |
|
243 |
||
244 |
lemma balanced_bal: |
|
63755
182c111190e5
Renamed balanced to complete; added balanced; more about both
nipkow
parents:
63663
diff
changeset
|
245 |
assumes "bal xs n = (t,ys)" shows "balanced t" |
64018 | 246 |
unfolding balanced_def |
247 |
using height_bal[OF assms] min_height_bal[OF assms] |
|
248 |
by linarith |
|
63643 | 249 |
|
64018 | 250 |
lemma height_balance_list: |
251 |
"height (balance_list xs) = nat \<lceil>log 2 (length xs + 1)\<rceil>" |
|
252 |
by (metis balance_list_def height_bal prod.collapse) |
|
253 |
||
254 |
corollary height_balance_tree: |
|
255 |
"height (balance_tree t) = nat(ceiling(log 2 (size t + 1)))" |
|
256 |
by(simp add: balance_tree_def height_balance_list) |
|
63829 | 257 |
|
258 |
corollary balanced_balance_list[simp]: "balanced (balance_list xs)" |
|
259 |
by (metis balance_list_def balanced_bal prod.collapse) |
|
260 |
||
261 |
corollary balanced_balance_tree[simp]: "balanced (balance_tree t)" |
|
262 |
by (simp add: balance_tree_def) |
|
263 |
||
63861 | 264 |
lemma wbalanced_bal: "bal xs n = (t,ys) \<Longrightarrow> wbalanced t" |
265 |
proof(induction xs n arbitrary: t ys rule: bal.induct) |
|
266 |
case (1 xs n) |
|
267 |
show ?case |
|
268 |
proof cases |
|
269 |
assume "n = 0" |
|
270 |
thus ?thesis |
|
271 |
using "1.prems" by(simp add: bal_simps) |
|
272 |
next |
|
273 |
assume "n \<noteq> 0" |
|
274 |
with "1.prems" obtain l ys r zs where |
|
275 |
rec1: "bal xs (n div 2) = (l, ys)" and |
|
276 |
rec2: "bal (tl ys) (n - 1 - n div 2) = (r, zs)" and |
|
277 |
t: "t = \<langle>l, hd ys, r\<rangle>" |
|
278 |
by(auto simp add: bal_simps Let_def split: prod.splits) |
|
279 |
have l: "wbalanced l" using "1.IH"(1)[OF \<open>n\<noteq>0\<close> refl rec1] . |
|
280 |
have "wbalanced r" using "1.IH"(2)[OF \<open>n\<noteq>0\<close> refl rec1[symmetric] refl rec2] . |
|
281 |
with l t size_bal[OF rec1] size_bal[OF rec2] |
|
282 |
show ?thesis by auto |
|
283 |
qed |
|
284 |
qed |
|
285 |
||
286 |
lemma wbalanced_balance_tree: "wbalanced (balance_tree t)" |
|
287 |
by(simp add: balance_tree_def balance_list_def) |
|
288 |
(metis prod.collapse wbalanced_bal) |
|
289 |
||
63829 | 290 |
hide_const (open) bal |
63643 | 291 |
|
292 |
end |