8797
|
1 |
(* Title: HOL/ex/AVL.thy
|
|
2 |
ID: $Id$
|
14419
|
3 |
Author: Cornelia Pusch and Tobias Nipkow, converted to Isar by Gerwin Klein
|
8797
|
4 |
Copyright 1998 TUM
|
|
5 |
*)
|
|
6 |
|
14419
|
7 |
header "AVL Trees"
|
|
8 |
|
|
9 |
theory AVL = Main:
|
|
10 |
|
|
11 |
text {*
|
|
12 |
At the moment only insertion is formalized.
|
|
13 |
|
|
14 |
This theory would be a nice candidate for structured Isar proof
|
|
15 |
texts and for extensions (delete operation).
|
|
16 |
*}
|
|
17 |
|
|
18 |
(*
|
|
19 |
This version works exclusively with nat. Balance check could be
|
|
20 |
simplified by working with int:
|
|
21 |
is_bal (MKT n l r) = (abs(int(height l) - int(height r)) <= 1 & is_bal l & is_bal r)
|
|
22 |
*)
|
8797
|
23 |
|
|
24 |
datatype tree = ET | MKT nat tree tree
|
|
25 |
|
|
26 |
consts
|
14419
|
27 |
height :: "tree \<Rightarrow> nat"
|
|
28 |
is_in :: "nat \<Rightarrow> tree \<Rightarrow> bool"
|
|
29 |
is_ord :: "tree \<Rightarrow> bool"
|
|
30 |
is_bal :: "tree \<Rightarrow> bool"
|
8797
|
31 |
|
|
32 |
primrec
|
14419
|
33 |
"height ET = 0"
|
|
34 |
"height (MKT n l r) = 1 + max (height l) (height r)"
|
8797
|
35 |
|
|
36 |
primrec
|
14419
|
37 |
"is_in k ET = False"
|
|
38 |
"is_in k (MKT n l r) = (k=n \<or> is_in k l \<or> is_in k r)"
|
8797
|
39 |
|
|
40 |
primrec
|
14419
|
41 |
"is_ord ET = True"
|
|
42 |
"is_ord (MKT n l r) = ((\<forall>n'. is_in n' l \<longrightarrow> n' < n) \<and>
|
|
43 |
(\<forall>n'. is_in n' r \<longrightarrow> n < n') \<and>
|
|
44 |
is_ord l \<and> is_ord r)"
|
8797
|
45 |
|
|
46 |
primrec
|
14419
|
47 |
"is_bal ET = True"
|
|
48 |
"is_bal (MKT n l r) = ((height l = height r \<or>
|
|
49 |
height l = 1+height r \<or>
|
|
50 |
height r = 1+height l) \<and>
|
|
51 |
is_bal l \<and> is_bal r)"
|
8797
|
52 |
|
|
53 |
|
|
54 |
datatype bal = Just | Left | Right
|
|
55 |
|
|
56 |
constdefs
|
14419
|
57 |
bal :: "tree \<Rightarrow> bal"
|
|
58 |
"bal t \<equiv> case t of ET \<Rightarrow> Just
|
|
59 |
| (MKT n l r) \<Rightarrow> if height l = height r then Just
|
|
60 |
else if height l < height r then Right else Left"
|
8797
|
61 |
|
|
62 |
consts
|
14419
|
63 |
r_rot :: "nat \<times> tree \<times> tree \<Rightarrow> tree"
|
|
64 |
l_rot :: "nat \<times> tree \<times> tree \<Rightarrow> tree"
|
|
65 |
lr_rot :: "nat \<times> tree \<times> tree \<Rightarrow> tree"
|
|
66 |
rl_rot :: "nat \<times> tree \<times> tree \<Rightarrow> tree"
|
8797
|
67 |
|
|
68 |
recdef r_rot "{}"
|
14419
|
69 |
"r_rot (n, MKT ln ll lr, r) = MKT ln ll (MKT n lr r)"
|
8797
|
70 |
|
|
71 |
recdef l_rot "{}"
|
14419
|
72 |
"l_rot(n, l, MKT rn rl rr) = MKT rn (MKT n l rl) rr"
|
8797
|
73 |
|
|
74 |
recdef lr_rot "{}"
|
14419
|
75 |
"lr_rot(n, MKT ln ll (MKT lrn lrl lrr), r) = MKT lrn (MKT ln ll lrl) (MKT n lrr r)"
|
8797
|
76 |
|
|
77 |
recdef rl_rot "{}"
|
14419
|
78 |
"rl_rot(n, l, MKT rn (MKT rln rll rlr) rr) = MKT rln (MKT n l rll) (MKT rn rlr rr)"
|
8797
|
79 |
|
|
80 |
|
|
81 |
constdefs
|
14419
|
82 |
l_bal :: "nat \<Rightarrow> tree \<Rightarrow> tree \<Rightarrow> tree"
|
|
83 |
"l_bal n l r \<equiv> if bal l = Right
|
|
84 |
then lr_rot (n, l, r)
|
|
85 |
else r_rot (n, l, r)"
|
8797
|
86 |
|
14419
|
87 |
r_bal :: "nat \<Rightarrow> tree \<Rightarrow> tree \<Rightarrow> tree"
|
|
88 |
"r_bal n l r \<equiv> if bal r = Left
|
|
89 |
then rl_rot (n, l, r)
|
|
90 |
else l_rot (n, l, r)"
|
8797
|
91 |
|
|
92 |
consts
|
14419
|
93 |
insert :: "nat \<Rightarrow> tree \<Rightarrow> tree"
|
8797
|
94 |
primrec
|
|
95 |
"insert x ET = MKT x ET ET"
|
|
96 |
"insert x (MKT n l r) =
|
|
97 |
(if x=n
|
|
98 |
then MKT n l r
|
|
99 |
else if x<n
|
|
100 |
then let l' = insert x l
|
14419
|
101 |
in if height l' = 2+height r
|
8797
|
102 |
then l_bal n l' r
|
|
103 |
else MKT n l' r
|
|
104 |
else let r' = insert x r
|
14419
|
105 |
in if height r' = 2+height l
|
8797
|
106 |
then r_bal n l r'
|
|
107 |
else MKT n l r')"
|
|
108 |
|
14419
|
109 |
|
|
110 |
|
|
111 |
subsection "is-bal"
|
|
112 |
|
|
113 |
declare Let_def [simp]
|
|
114 |
|
|
115 |
lemma is_bal_lr_rot:
|
|
116 |
"\<lbrakk> height l = Suc(Suc(height r)); bal l = Right; is_bal l; is_bal r \<rbrakk>
|
|
117 |
\<Longrightarrow> is_bal (lr_rot (n, l, r))"
|
|
118 |
apply (unfold bal_def)
|
|
119 |
apply (cases l)
|
|
120 |
apply simp
|
|
121 |
apply (rename_tac t1 t2)
|
|
122 |
apply (case_tac t2)
|
|
123 |
apply simp
|
|
124 |
apply (simp add: max_def split: split_if_asm)
|
|
125 |
apply arith
|
|
126 |
done
|
|
127 |
|
|
128 |
|
|
129 |
lemma is_bal_r_rot:
|
|
130 |
"\<lbrakk> height l = Suc(Suc(height r)); bal l \<noteq> Right; is_bal l; is_bal r \<rbrakk>
|
|
131 |
\<Longrightarrow> is_bal (r_rot (n, l, r))"
|
|
132 |
apply (unfold bal_def)
|
|
133 |
apply (cases "l")
|
|
134 |
apply simp
|
|
135 |
apply (simp add: max_def split: split_if_asm)
|
|
136 |
done
|
|
137 |
|
|
138 |
|
|
139 |
lemma is_bal_rl_rot:
|
|
140 |
"\<lbrakk> height r = Suc(Suc(height l)); bal r = Left; is_bal l; is_bal r \<rbrakk>
|
|
141 |
\<Longrightarrow> is_bal (rl_rot (n, l, r))"
|
|
142 |
apply (unfold bal_def)
|
|
143 |
apply (cases r)
|
|
144 |
apply simp
|
|
145 |
apply (rename_tac t1 t2)
|
|
146 |
apply (case_tac t1)
|
|
147 |
apply (simp add: max_def split: split_if_asm)
|
|
148 |
apply (simp add: max_def split: split_if_asm)
|
|
149 |
apply arith
|
|
150 |
done
|
|
151 |
|
|
152 |
|
|
153 |
lemma is_bal_l_rot:
|
|
154 |
"\<lbrakk> height r = Suc(Suc(height l)); bal r \<noteq> Left; is_bal l; is_bal r \<rbrakk>
|
|
155 |
\<Longrightarrow> is_bal (l_rot (n, l, r))"
|
|
156 |
apply (unfold bal_def)
|
|
157 |
apply (cases r)
|
|
158 |
apply simp
|
|
159 |
apply (simp add: max_def split: split_if_asm)
|
|
160 |
done
|
|
161 |
|
|
162 |
|
|
163 |
text {* Lemmas about height after rotation *}
|
|
164 |
|
|
165 |
lemma height_lr_rot:
|
|
166 |
"\<lbrakk> bal l = Right; height l = Suc(Suc(height r)) \<rbrakk>
|
|
167 |
\<Longrightarrow> Suc(height (lr_rot (n, l, r))) = height (MKT n l r) "
|
|
168 |
apply (unfold bal_def)
|
|
169 |
apply (cases l)
|
|
170 |
apply simp
|
|
171 |
apply (rename_tac t1 t2)
|
|
172 |
apply (case_tac t2)
|
|
173 |
apply simp
|
|
174 |
apply (simp add: max_def split: split_if_asm)
|
|
175 |
done
|
|
176 |
|
|
177 |
|
|
178 |
lemma height_r_rot:
|
|
179 |
"\<lbrakk> height l = Suc(Suc(height r)); bal l \<noteq> Right \<rbrakk>
|
|
180 |
\<Longrightarrow> Suc(height (r_rot (n, l, r))) = height (MKT n l r) \<or>
|
|
181 |
height (r_rot (n, l, r)) = height (MKT n l r)"
|
|
182 |
apply (unfold bal_def)
|
|
183 |
apply (cases l)
|
|
184 |
apply simp
|
|
185 |
apply (simp add: max_def split: split_if_asm)
|
|
186 |
done
|
|
187 |
|
|
188 |
|
|
189 |
lemma height_l_bal:
|
|
190 |
"height l = Suc(Suc(height r))
|
|
191 |
\<Longrightarrow> Suc(height (l_bal n l r)) = height (MKT n l r) |
|
|
192 |
height (l_bal n l r) = height (MKT n l r)"
|
|
193 |
apply (unfold l_bal_def)
|
|
194 |
apply (cases "bal l = Right")
|
|
195 |
apply (fastsimp dest: height_lr_rot)
|
|
196 |
apply (fastsimp dest: height_r_rot)
|
|
197 |
done
|
|
198 |
|
|
199 |
|
|
200 |
lemma height_rl_rot [rule_format (no_asm)]:
|
|
201 |
"height r = Suc(Suc(height l)) \<longrightarrow> bal r = Left
|
|
202 |
\<longrightarrow> Suc(height (rl_rot (n, l, r))) = height (MKT n l r)"
|
|
203 |
apply (unfold bal_def)
|
|
204 |
apply (cases r)
|
|
205 |
apply simp
|
|
206 |
apply (rename_tac t1 t2)
|
|
207 |
apply (case_tac t1)
|
|
208 |
apply simp
|
|
209 |
apply (simp add: max_def split: split_if_asm)
|
|
210 |
done
|
|
211 |
|
|
212 |
|
|
213 |
lemma height_l_rot [rule_format (no_asm)]:
|
|
214 |
"height r = Suc(Suc(height l)) \<longrightarrow> bal r \<noteq> Left
|
|
215 |
\<longrightarrow> Suc(height (l_rot (n, l, r))) = height (MKT n l r) \<or>
|
|
216 |
height (l_rot (n, l, r)) = height (MKT n l r)"
|
|
217 |
apply (unfold bal_def)
|
|
218 |
apply (cases r)
|
|
219 |
apply simp
|
|
220 |
apply (simp add: max_def)
|
|
221 |
done
|
|
222 |
|
|
223 |
|
|
224 |
lemma height_r_bal:
|
|
225 |
"height r = Suc(Suc(height l))
|
|
226 |
\<Longrightarrow> Suc(height (r_bal n l r)) = height (MKT n l r) \<or>
|
|
227 |
height (r_bal n l r) = height (MKT n l r)"
|
|
228 |
apply (unfold r_bal_def)
|
|
229 |
apply (cases "bal r = Left")
|
|
230 |
apply (fastsimp dest: height_rl_rot)
|
|
231 |
apply (fastsimp dest: height_l_rot)
|
|
232 |
done
|
|
233 |
|
|
234 |
lemma height_insert [rule_format (no_asm)]:
|
|
235 |
"is_bal t
|
|
236 |
\<longrightarrow> height (insert x t) = height t \<or> height (insert x t) = Suc(height t)"
|
|
237 |
apply (induct_tac "t")
|
|
238 |
apply simp
|
|
239 |
apply (rename_tac n t1 t2)
|
|
240 |
apply (case_tac "x=n")
|
|
241 |
apply simp
|
|
242 |
apply (case_tac "x<n")
|
|
243 |
apply (case_tac "height (insert x t1) = Suc (Suc (height t2))")
|
|
244 |
apply (frule_tac n = n in height_l_bal)
|
|
245 |
apply (simp add: max_def)
|
|
246 |
apply fastsimp
|
|
247 |
apply (simp add: max_def)
|
|
248 |
apply fastsimp
|
|
249 |
apply (case_tac "height (insert x t2) = Suc (Suc (height t1))")
|
|
250 |
apply (frule_tac n = n in height_r_bal)
|
|
251 |
apply (fastsimp simp add: max_def)
|
|
252 |
apply (simp add: max_def)
|
|
253 |
apply fastsimp
|
|
254 |
done
|
|
255 |
|
|
256 |
|
|
257 |
lemma is_bal_insert_left:
|
|
258 |
"\<lbrakk>height (insert x l) \<noteq> Suc(Suc(height r)); is_bal (insert x l); is_bal (MKT n l r)\<rbrakk>
|
|
259 |
\<Longrightarrow> is_bal (MKT n (insert x l) r)"
|
|
260 |
apply (cut_tac x = "x" and t = "l" in height_insert)
|
|
261 |
apply simp
|
|
262 |
apply fastsimp
|
|
263 |
done
|
|
264 |
|
|
265 |
|
|
266 |
lemma is_bal_insert_right:
|
|
267 |
"\<lbrakk> height (insert x r) \<noteq> Suc(Suc(height l)); is_bal (insert x r); is_bal (MKT n l r) \<rbrakk>
|
|
268 |
\<Longrightarrow> is_bal (MKT n l (insert x r))"
|
|
269 |
apply (cut_tac x = "x" and t = "r" in height_insert)
|
|
270 |
apply simp
|
|
271 |
apply fastsimp
|
|
272 |
done
|
|
273 |
|
|
274 |
|
|
275 |
lemma is_bal_insert [rule_format (no_asm)]:
|
|
276 |
"is_bal t \<longrightarrow> is_bal(insert x t)"
|
|
277 |
apply (induct_tac "t")
|
|
278 |
apply simp
|
|
279 |
apply (rename_tac n t1 t2)
|
|
280 |
apply (case_tac "x=n")
|
|
281 |
apply simp
|
|
282 |
apply (case_tac "x<n")
|
|
283 |
apply (case_tac "height (insert x t1) = Suc (Suc (height t2))")
|
|
284 |
apply (case_tac "bal (insert x t1) = Right")
|
|
285 |
apply (fastsimp intro: is_bal_lr_rot simp add: l_bal_def)
|
|
286 |
apply (fastsimp intro: is_bal_r_rot simp add: l_bal_def)
|
|
287 |
apply clarify
|
|
288 |
apply (frule is_bal_insert_left)
|
|
289 |
apply simp
|
|
290 |
apply assumption
|
|
291 |
apply simp
|
|
292 |
apply (case_tac "height (insert x t2) = Suc (Suc (height t1))")
|
|
293 |
apply (case_tac "bal (insert x t2) = Left")
|
|
294 |
apply (fastsimp intro: is_bal_rl_rot simp add: r_bal_def)
|
|
295 |
apply (fastsimp intro: is_bal_l_rot simp add: r_bal_def)
|
|
296 |
apply clarify
|
|
297 |
apply (frule is_bal_insert_right)
|
|
298 |
apply simp
|
|
299 |
apply assumption
|
|
300 |
apply simp
|
|
301 |
done
|
|
302 |
|
|
303 |
|
|
304 |
subsection "is-in"
|
|
305 |
|
|
306 |
lemma is_in_lr_rot:
|
|
307 |
"\<lbrakk> height l = Suc(Suc(height r)); bal l = Right \<rbrakk>
|
|
308 |
\<Longrightarrow> is_in x (lr_rot (n, l, r)) = is_in x (MKT n l r)"
|
|
309 |
apply (unfold bal_def)
|
|
310 |
apply (cases l)
|
|
311 |
apply simp
|
|
312 |
apply (rename_tac t1 t2)
|
|
313 |
apply (case_tac t2)
|
|
314 |
apply simp
|
|
315 |
apply fastsimp
|
|
316 |
done
|
|
317 |
|
|
318 |
|
|
319 |
lemma is_in_r_rot:
|
|
320 |
"\<lbrakk> height l = Suc(Suc(height r)); bal l \<noteq> Right \<rbrakk>
|
|
321 |
\<Longrightarrow> is_in x (r_rot (n, l, r)) = is_in x (MKT n l r)"
|
|
322 |
apply (unfold bal_def)
|
|
323 |
apply (cases l)
|
|
324 |
apply simp
|
|
325 |
apply fastsimp
|
|
326 |
done
|
|
327 |
|
|
328 |
|
|
329 |
lemma is_in_rl_rot:
|
|
330 |
"\<lbrakk> height r = Suc(Suc(height l)); bal r = Left \<rbrakk>
|
|
331 |
\<Longrightarrow> is_in x (rl_rot (n, l, r)) = is_in x (MKT n l r)"
|
|
332 |
apply (unfold bal_def)
|
|
333 |
apply (cases r)
|
|
334 |
apply simp
|
|
335 |
apply (rename_tac t1 t2)
|
|
336 |
apply (case_tac t1)
|
|
337 |
apply (simp add: max_def le_def)
|
|
338 |
apply fastsimp
|
|
339 |
done
|
|
340 |
|
|
341 |
|
|
342 |
lemma is_in_l_rot:
|
|
343 |
"\<lbrakk> height r = Suc(Suc(height l)); bal r ~= Left \<rbrakk>
|
|
344 |
\<Longrightarrow> is_in x (l_rot (n, l, r)) = is_in x (MKT n l r)"
|
|
345 |
apply (unfold bal_def)
|
|
346 |
apply (cases r)
|
|
347 |
apply simp
|
|
348 |
apply fastsimp
|
|
349 |
done
|
|
350 |
|
|
351 |
|
|
352 |
lemma is_in_insert:
|
|
353 |
"is_in y (insert x t) = (y=x \<or> is_in y t)"
|
|
354 |
apply (induct t)
|
|
355 |
apply simp
|
|
356 |
apply (simp add: l_bal_def is_in_lr_rot is_in_r_rot r_bal_def
|
|
357 |
is_in_rl_rot is_in_l_rot)
|
|
358 |
apply blast
|
|
359 |
done
|
|
360 |
|
|
361 |
|
|
362 |
subsection "is-ord"
|
|
363 |
|
|
364 |
lemma is_ord_lr_rot [rule_format (no_asm)]:
|
|
365 |
"\<lbrakk> height l = Suc(Suc(height r)); bal l = Right; is_ord (MKT n l r) \<rbrakk>
|
|
366 |
\<Longrightarrow> is_ord (lr_rot (n, l, r))"
|
|
367 |
apply (unfold bal_def)
|
|
368 |
apply (cases l)
|
|
369 |
apply simp
|
|
370 |
apply (rename_tac t1 t2)
|
|
371 |
apply (case_tac t2)
|
|
372 |
apply simp
|
|
373 |
apply simp
|
|
374 |
apply (blast intro: less_trans)
|
|
375 |
done
|
|
376 |
|
|
377 |
|
|
378 |
lemma is_ord_r_rot:
|
|
379 |
"\<lbrakk> height l = Suc(Suc(height r)); bal l \<noteq> Right; is_ord (MKT n l r) \<rbrakk>
|
|
380 |
\<Longrightarrow> is_ord (r_rot (n, l, r))"
|
|
381 |
apply (unfold bal_def)
|
|
382 |
apply (cases l)
|
|
383 |
apply (auto intro: less_trans)
|
|
384 |
done
|
|
385 |
|
|
386 |
|
|
387 |
lemma is_ord_rl_rot:
|
|
388 |
"\<lbrakk> height r = Suc(Suc(height l)); bal r = Left; is_ord (MKT n l r) \<rbrakk>
|
|
389 |
\<Longrightarrow> is_ord (rl_rot (n, l, r))"
|
|
390 |
apply (unfold bal_def)
|
|
391 |
apply (cases r)
|
|
392 |
apply simp
|
|
393 |
apply (rename_tac t1 t2)
|
|
394 |
apply (case_tac t1)
|
|
395 |
apply (simp add: le_def)
|
|
396 |
apply simp
|
|
397 |
apply (blast intro: less_trans)
|
|
398 |
done
|
|
399 |
|
|
400 |
|
|
401 |
lemma is_ord_l_rot:
|
|
402 |
"\<lbrakk> height r = Suc(Suc(height l)); bal r \<noteq> Left; is_ord (MKT n l r) \<rbrakk>
|
|
403 |
\<Longrightarrow> is_ord (l_rot (n, l, r))"
|
|
404 |
apply (unfold bal_def)
|
|
405 |
apply (cases r)
|
|
406 |
apply simp
|
|
407 |
apply simp
|
|
408 |
apply (blast intro: less_trans)
|
|
409 |
done
|
|
410 |
|
|
411 |
|
|
412 |
lemma is_ord_insert:
|
|
413 |
"is_ord t \<Longrightarrow> is_ord(insert x t)"
|
|
414 |
apply (induct t)
|
|
415 |
apply simp
|
|
416 |
apply (cut_tac m = "x" and n = "nat" in less_linear)
|
|
417 |
apply (fastsimp simp add: l_bal_def is_ord_lr_rot is_ord_r_rot r_bal_def
|
|
418 |
is_ord_l_rot is_ord_rl_rot is_in_insert)
|
|
419 |
done
|
|
420 |
|
8797
|
421 |
end
|