author | haftmann |
Thu, 23 Nov 2017 17:03:27 +0000 | |
changeset 67087 | 733017b19de9 |
parent 66516 | 97c2d3846e10 |
child 70747 | 548420d389ea |
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 |
66510 | 7 |
"HOL-Library.Tree_Real" |
63643 | 8 |
begin |
9 |
||
64444 | 10 |
fun bal :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a tree * 'a list" where |
11 |
"bal n xs = (if n=0 then (Leaf,xs) else |
|
63643 | 12 |
(let m = n div 2; |
64444 | 13 |
(l, ys) = bal m xs; |
14 |
(r, zs) = bal (n-1-m) (tl ys) |
|
63643 | 15 |
in (Node l (hd ys) r, zs)))" |
16 |
||
17 |
declare bal.simps[simp del] |
|
18 |
||
64444 | 19 |
definition bal_list :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a tree" where |
20 |
"bal_list n xs = fst (bal n xs)" |
|
21 |
||
63829 | 22 |
definition balance_list :: "'a list \<Rightarrow> 'a tree" where |
64444 | 23 |
"balance_list xs = bal_list (length xs) xs" |
24 |
||
25 |
definition bal_tree :: "nat \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where |
|
26 |
"bal_tree n t = bal_list n (inorder t)" |
|
63829 | 27 |
|
28 |
definition balance_tree :: "'a tree \<Rightarrow> 'a tree" where |
|
64444 | 29 |
"balance_tree t = bal_tree (size t) t" |
63829 | 30 |
|
63843 | 31 |
lemma bal_simps: |
64444 | 32 |
"bal 0 xs = (Leaf, xs)" |
63843 | 33 |
"n > 0 \<Longrightarrow> |
64444 | 34 |
bal n xs = |
63843 | 35 |
(let m = n div 2; |
64444 | 36 |
(l, ys) = bal m xs; |
37 |
(r, zs) = bal (n-1-m) (tl ys) |
|
63843 | 38 |
in (Node l (hd ys) r, zs))" |
39 |
by(simp_all add: bal.simps) |
|
63643 | 40 |
|
64444 | 41 |
text\<open>Some of the following lemmas take advantage of the fact |
63861 | 42 |
that \<open>bal xs n\<close> yields a result even if \<open>n > length xs\<close>.\<close> |
43 |
||
64444 | 44 |
lemma size_bal: "bal n xs = (t,ys) \<Longrightarrow> size t = n" |
45 |
proof(induction n xs arbitrary: t ys rule: bal.induct) |
|
46 |
case (1 n xs) |
|
63861 | 47 |
thus ?case |
48 |
by(cases "n=0") |
|
49 |
(auto simp add: bal_simps Let_def split: prod.splits) |
|
50 |
qed |
|
51 |
||
63643 | 52 |
lemma bal_inorder: |
64444 | 53 |
"\<lbrakk> bal n xs = (t,ys); n \<le> length xs \<rbrakk> |
63755
182c111190e5
Renamed balanced to complete; added balanced; more about both
nipkow
parents:
63663
diff
changeset
|
54 |
\<Longrightarrow> inorder t = take n xs \<and> ys = drop n xs" |
64444 | 55 |
proof(induction n xs arbitrary: t ys rule: bal.induct) |
56 |
case (1 n xs) show ?case |
|
63643 | 57 |
proof cases |
63843 | 58 |
assume "n = 0" thus ?thesis using 1 by (simp add: bal_simps) |
63643 | 59 |
next |
60 |
assume [arith]: "n \<noteq> 0" |
|
61 |
let ?n1 = "n div 2" let ?n2 = "n - 1 - ?n1" |
|
62 |
from "1.prems" obtain l r xs' where |
|
64444 | 63 |
b1: "bal ?n1 xs = (l,xs')" and |
64 |
b2: "bal ?n2 (tl xs') = (r,ys)" and |
|
63643 | 65 |
t: "t = \<langle>l, hd xs', r\<rangle>" |
63843 | 66 |
by(auto simp: Let_def bal_simps split: prod.splits) |
63643 | 67 |
have IH1: "inorder l = take ?n1 xs \<and> xs' = drop ?n1 xs" |
68 |
using b1 "1.prems" by(intro "1.IH"(1)) auto |
|
69 |
have IH2: "inorder r = take ?n2 (tl xs') \<and> ys = drop ?n2 (tl xs')" |
|
70 |
using b1 b2 IH1 "1.prems" by(intro "1.IH"(2)) auto |
|
71 |
have "drop (n div 2) xs \<noteq> []" using "1.prems"(2) by simp |
|
72 |
hence "hd (drop ?n1 xs) # take ?n2 (tl (drop ?n1 xs)) = take (?n2 + 1) (drop ?n1 xs)" |
|
73 |
by (metis Suc_eq_plus1 take_Suc) |
|
74 |
hence *: "inorder t = take n xs" using t IH1 IH2 |
|
75 |
using take_add[of ?n1 "?n2+1" xs] by(simp) |
|
76 |
have "n - n div 2 + n div 2 = n" by simp |
|
77 |
hence "ys = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric]) |
|
78 |
thus ?thesis using * by blast |
|
79 |
qed |
|
80 |
qed |
|
81 |
||
64444 | 82 |
corollary inorder_bal_list[simp]: |
83 |
"n \<le> length xs \<Longrightarrow> inorder(bal_list n xs) = take n xs" |
|
84 |
unfolding bal_list_def by (metis bal_inorder eq_fst_iff) |
|
85 |
||
86 |
corollary inorder_balance_list[simp]: "inorder(balance_list xs) = xs" |
|
87 |
by(simp add: balance_list_def) |
|
88 |
||
89 |
corollary inorder_bal_tree: |
|
90 |
"n \<le> size t \<Longrightarrow> inorder(bal_tree n t) = take n (inorder t)" |
|
91 |
by(simp add: bal_tree_def) |
|
63643 | 92 |
|
64018 | 93 |
corollary inorder_balance_tree[simp]: "inorder(balance_tree t) = inorder t" |
64444 | 94 |
by(simp add: balance_tree_def inorder_bal_tree) |
95 |
||
96 |
corollary size_bal_list[simp]: "size(bal_list n xs) = n" |
|
97 |
unfolding bal_list_def by (metis prod.collapse size_bal) |
|
64018 | 98 |
|
99 |
corollary size_balance_list[simp]: "size(balance_list xs) = length xs" |
|
64444 | 100 |
by (simp add: balance_list_def) |
101 |
||
102 |
corollary size_bal_tree[simp]: "size(bal_tree n t) = n" |
|
103 |
by(simp add: bal_tree_def) |
|
64018 | 104 |
|
105 |
corollary size_balance_tree[simp]: "size(balance_tree t) = size t" |
|
64444 | 106 |
by(simp add: balance_tree_def) |
64018 | 107 |
|
108 |
lemma min_height_bal: |
|
66515 | 109 |
"bal n xs = (t,ys) \<Longrightarrow> min_height t = nat(\<lfloor>log 2 (n + 1)\<rfloor>)" |
64444 | 110 |
proof(induction n xs arbitrary: t ys rule: bal.induct) |
111 |
case (1 n xs) show ?case |
|
63643 | 112 |
proof cases |
113 |
assume "n = 0" thus ?thesis |
|
64018 | 114 |
using "1.prems" by (simp add: bal_simps) |
63643 | 115 |
next |
116 |
assume [arith]: "n \<noteq> 0" |
|
117 |
from "1.prems" obtain l r xs' where |
|
64444 | 118 |
b1: "bal (n div 2) xs = (l,xs')" and |
119 |
b2: "bal (n - 1 - n div 2) (tl xs') = (r,ys)" and |
|
63643 | 120 |
t: "t = \<langle>l, hd xs', r\<rangle>" |
63843 | 121 |
by(auto simp: bal_simps Let_def split: prod.splits) |
64018 | 122 |
let ?log1 = "nat (floor(log 2 (n div 2 + 1)))" |
123 |
let ?log2 = "nat (floor(log 2 (n - 1 - n div 2 + 1)))" |
|
63643 | 124 |
have IH1: "min_height l = ?log1" using "1.IH"(1) b1 by simp |
64018 | 125 |
have IH2: "min_height r = ?log2" using "1.IH"(2) b1 b2 by simp |
126 |
have "(n+1) div 2 \<ge> 1" by arith |
|
127 |
hence 0: "log 2 ((n+1) div 2) \<ge> 0" by simp |
|
128 |
have "n - 1 - n div 2 + 1 \<le> n div 2 + 1" by arith |
|
129 |
hence le: "?log2 \<le> ?log1" |
|
130 |
by(simp add: nat_mono floor_mono) |
|
131 |
have "min_height t = min ?log1 ?log2 + 1" by (simp add: t IH1 IH2) |
|
132 |
also have "\<dots> = ?log2 + 1" using le by (simp add: min_absorb2) |
|
133 |
also have "n - 1 - n div 2 + 1 = (n+1) div 2" by linarith |
|
134 |
also have "nat (floor(log 2 ((n+1) div 2))) + 1 |
|
135 |
= nat (floor(log 2 ((n+1) div 2) + 1))" |
|
136 |
using 0 by linarith |
|
137 |
also have "\<dots> = nat (floor(log 2 (n + 1)))" |
|
138 |
using floor_log2_div2[of "n+1"] by (simp add: log_mult) |
|
139 |
finally show ?thesis . |
|
140 |
qed |
|
141 |
qed |
|
142 |
||
143 |
lemma height_bal: |
|
64444 | 144 |
"bal n xs = (t,ys) \<Longrightarrow> height t = nat \<lceil>log 2 (n + 1)\<rceil>" |
145 |
proof(induction n xs arbitrary: t ys rule: bal.induct) |
|
146 |
case (1 n xs) show ?case |
|
64018 | 147 |
proof cases |
148 |
assume "n = 0" thus ?thesis |
|
149 |
using "1.prems" by (simp add: bal_simps) |
|
150 |
next |
|
151 |
assume [arith]: "n \<noteq> 0" |
|
152 |
from "1.prems" obtain l r xs' where |
|
64444 | 153 |
b1: "bal (n div 2) xs = (l,xs')" and |
154 |
b2: "bal (n - 1 - n div 2) (tl xs') = (r,ys)" and |
|
64018 | 155 |
t: "t = \<langle>l, hd xs', r\<rangle>" |
156 |
by(auto simp: bal_simps Let_def split: prod.splits) |
|
157 |
let ?log1 = "nat \<lceil>log 2 (n div 2 + 1)\<rceil>" |
|
158 |
let ?log2 = "nat \<lceil>log 2 (n - 1 - n div 2 + 1)\<rceil>" |
|
159 |
have IH1: "height l = ?log1" using "1.IH"(1) b1 by simp |
|
160 |
have IH2: "height r = ?log2" using "1.IH"(2) b1 b2 by simp |
|
161 |
have 0: "log 2 (n div 2 + 1) \<ge> 0" by auto |
|
162 |
have "n - 1 - n div 2 + 1 \<le> n div 2 + 1" by arith |
|
163 |
hence le: "?log2 \<le> ?log1" |
|
164 |
by(simp add: nat_mono ceiling_mono del: nat_ceiling_le_eq) |
|
165 |
have "height t = max ?log1 ?log2 + 1" by (simp add: t IH1 IH2) |
|
166 |
also have "\<dots> = ?log1 + 1" using le by (simp add: max_absorb1) |
|
167 |
also have "\<dots> = nat \<lceil>log 2 (n div 2 + 1) + 1\<rceil>" using 0 by linarith |
|
168 |
also have "\<dots> = nat \<lceil>log 2 (n + 1)\<rceil>" |
|
66515 | 169 |
using ceiling_log2_div2[of "n+1"] by (simp) |
63643 | 170 |
finally show ?thesis . |
171 |
qed |
|
172 |
qed |
|
173 |
||
174 |
lemma balanced_bal: |
|
64444 | 175 |
assumes "bal n xs = (t,ys)" shows "balanced t" |
64018 | 176 |
unfolding balanced_def |
177 |
using height_bal[OF assms] min_height_bal[OF assms] |
|
178 |
by linarith |
|
63643 | 179 |
|
64444 | 180 |
lemma height_bal_list: |
181 |
"n \<le> length xs \<Longrightarrow> height (bal_list n xs) = nat \<lceil>log 2 (n + 1)\<rceil>" |
|
182 |
unfolding bal_list_def by (metis height_bal prod.collapse) |
|
183 |
||
64018 | 184 |
lemma height_balance_list: |
185 |
"height (balance_list xs) = nat \<lceil>log 2 (length xs + 1)\<rceil>" |
|
64444 | 186 |
by (simp add: balance_list_def height_bal_list) |
187 |
||
188 |
corollary height_bal_tree: |
|
66515 | 189 |
"n \<le> length xs \<Longrightarrow> height (bal_tree n t) = nat\<lceil>log 2 (n + 1)\<rceil>" |
64444 | 190 |
unfolding bal_list_def bal_tree_def |
191 |
using height_bal prod.exhaust_sel by blast |
|
64018 | 192 |
|
193 |
corollary height_balance_tree: |
|
66515 | 194 |
"height (balance_tree t) = nat\<lceil>log 2 (size t + 1)\<rceil>" |
64444 | 195 |
by (simp add: bal_tree_def balance_tree_def height_bal_list) |
196 |
||
197 |
corollary balanced_bal_list[simp]: "balanced (bal_list n xs)" |
|
198 |
unfolding bal_list_def by (metis balanced_bal prod.collapse) |
|
63829 | 199 |
|
200 |
corollary balanced_balance_list[simp]: "balanced (balance_list xs)" |
|
64444 | 201 |
by (simp add: balance_list_def) |
202 |
||
203 |
corollary balanced_bal_tree[simp]: "balanced (bal_tree n t)" |
|
204 |
by (simp add: bal_tree_def) |
|
63829 | 205 |
|
206 |
corollary balanced_balance_tree[simp]: "balanced (balance_tree t)" |
|
207 |
by (simp add: balance_tree_def) |
|
208 |
||
64444 | 209 |
lemma wbalanced_bal: "bal n xs = (t,ys) \<Longrightarrow> wbalanced t" |
210 |
proof(induction n xs arbitrary: t ys rule: bal.induct) |
|
211 |
case (1 n xs) |
|
63861 | 212 |
show ?case |
213 |
proof cases |
|
214 |
assume "n = 0" |
|
215 |
thus ?thesis |
|
216 |
using "1.prems" by(simp add: bal_simps) |
|
217 |
next |
|
218 |
assume "n \<noteq> 0" |
|
219 |
with "1.prems" obtain l ys r zs where |
|
64444 | 220 |
rec1: "bal (n div 2) xs = (l, ys)" and |
221 |
rec2: "bal (n - 1 - n div 2) (tl ys) = (r, zs)" and |
|
63861 | 222 |
t: "t = \<langle>l, hd ys, r\<rangle>" |
223 |
by(auto simp add: bal_simps Let_def split: prod.splits) |
|
224 |
have l: "wbalanced l" using "1.IH"(1)[OF \<open>n\<noteq>0\<close> refl rec1] . |
|
225 |
have "wbalanced r" using "1.IH"(2)[OF \<open>n\<noteq>0\<close> refl rec1[symmetric] refl rec2] . |
|
226 |
with l t size_bal[OF rec1] size_bal[OF rec2] |
|
227 |
show ?thesis by auto |
|
228 |
qed |
|
229 |
qed |
|
230 |
||
64541 | 231 |
text\<open>An alternative proof via @{thm balanced_if_wbalanced}:\<close> |
232 |
lemma "bal n xs = (t,ys) \<Longrightarrow> balanced t" |
|
233 |
by(rule balanced_if_wbalanced[OF wbalanced_bal]) |
|
234 |
||
64444 | 235 |
lemma wbalanced_bal_list[simp]: "wbalanced (bal_list n xs)" |
236 |
by(simp add: bal_list_def) (metis prod.collapse wbalanced_bal) |
|
237 |
||
238 |
lemma wbalanced_balance_list[simp]: "wbalanced (balance_list xs)" |
|
239 |
by(simp add: balance_list_def) |
|
240 |
||
241 |
lemma wbalanced_bal_tree[simp]: "wbalanced (bal_tree n t)" |
|
242 |
by(simp add: bal_tree_def) |
|
243 |
||
63861 | 244 |
lemma wbalanced_balance_tree: "wbalanced (balance_tree t)" |
64444 | 245 |
by (simp add: balance_tree_def) |
63861 | 246 |
|
63829 | 247 |
hide_const (open) bal |
63643 | 248 |
|
249 |
end |