[5fdc19]: thys / AVL-Trees / AVL.thy  Maximize  Restore  History

Download this file

506 lines (416 with data), 13.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
(* Title: AVL Trees
ID: $Id: AVL.thy,v 1.6.2.1 2006-05-17 22:47:48 lsf37 Exp $
Author: Cornelia Pusch and Tobias Nipkow, converted to Isar by Gerwin Klein
Author: contributions by Achim Brucker, Burkhart Wolff and Jan Smaus
Maintainer: Gerwin Klein <gerwin.klein at nicta.com.au>
Copyright: 1998 TUM
see the file Changelog for a list of changes
*)
header "Parameterized AVL Trees"
theory AVL
imports Main
begin
text {*
This theory would be a nice candidate for structured Isar proof
texts and for extensions (delete operation).
At the moment only insertion is formalized.
*}
datatype 'a tree = ET | MKT 'a "'a tree" "'a tree"
consts
height :: "'a tree \<Rightarrow> nat"
is_in :: "'a \<Rightarrow> 'a tree \<Rightarrow> bool"
is_ord :: "('a::order) tree \<Rightarrow> bool"
is_bal :: "'a tree \<Rightarrow> bool"
primrec
"height ET = 0"
"height (MKT n l r) = 1 + max (height l) (height r)"
primrec
"is_in k ET = False"
"is_in k (MKT n l r) = (k=n \<or> is_in k l \<or> is_in k r)"
primrec
"is_ord ET = True"
"is_ord (MKT n l r) = ((\<forall>n'. is_in n' l \<longrightarrow> n' < n) \<and>
(\<forall>n'. is_in n' r \<longrightarrow> n < n') \<and>
is_ord l \<and> is_ord r)"
primrec
"is_bal ET = True"
"is_bal (MKT n l r) = ((height l = height r \<or>
height l = 1+height r \<or>
height r = 1+height l) \<and>
is_bal l \<and> is_bal r)"
text {*
We also provide a more efficient variant of @{text is_in}:
*}
consts
is_in_eff :: "('a::order) \<Rightarrow> 'a tree \<Rightarrow> bool"
primrec
"is_in_eff k ET = False"
"is_in_eff k (MKT n l r) = (if k = n then True
else (if k<n then (is_in_eff k l)
else (is_in_eff k r)))"
datatype bal = Just | Left | Right
constdefs
bal :: "'a tree \<Rightarrow> bal"
"bal t \<equiv> case t of ET \<Rightarrow> Just
| (MKT n l r) \<Rightarrow> if height l = height r then Just
else if height l < height r then Right else Left"
consts
r_rot :: "'a \<times> 'a tree \<times> 'a tree \<Rightarrow> 'a tree"
l_rot :: "'a \<times> 'a tree \<times> 'a tree \<Rightarrow> 'a tree"
lr_rot :: "'a \<times> 'a tree \<times> 'a tree \<Rightarrow> 'a tree"
rl_rot :: "'a \<times> 'a tree \<times> 'a tree \<Rightarrow> 'a tree"
recdef r_rot "{}"
"r_rot (n, MKT ln ll lr, r) = MKT ln ll (MKT n lr r)"
recdef l_rot "{}"
"l_rot(n, l, MKT rn rl rr) = MKT rn (MKT n l rl) rr"
recdef lr_rot "{}"
"lr_rot(n, MKT ln ll (MKT lrn lrl lrr), r) = MKT lrn (MKT ln ll lrl) (MKT n lrr r)"
recdef rl_rot "{}"
"rl_rot(n, l, MKT rn (MKT rln rll rlr) rr) = MKT rln (MKT n l rll) (MKT rn rlr rr)"
constdefs
l_bal :: "'a \<Rightarrow> 'a tree \<Rightarrow> 'a tree \<Rightarrow> 'a tree"
"l_bal n l r \<equiv> if bal l = Right
then lr_rot (n, l, r)
else r_rot (n, l, r)"
r_bal :: "'a \<Rightarrow> 'a tree \<Rightarrow> 'a tree \<Rightarrow> 'a tree"
"r_bal n l r \<equiv> if bal r = Left
then rl_rot (n, l, r)
else l_rot (n, l, r)"
consts
insert :: "'a::order \<Rightarrow> 'a tree \<Rightarrow> 'a tree"
primrec
"insert x ET = MKT x ET ET"
"insert x (MKT n l r) =
(if x=n
then MKT n l r
else if x<n
then let l' = insert x l
in if height l' = 2+height r
then l_bal n l' r
else MKT n l' r
else let r' = insert x r
in if height r' = 2+height l
then r_bal n l r'
else MKT n l r')"
subsection "is-bal"
declare Let_def [simp]
lemma is_bal_lr_rot:
"\<lbrakk> height l = Suc(Suc(height r)); bal l = Right; is_bal l; is_bal r \<rbrakk>
\<Longrightarrow> is_bal (lr_rot (n, l, r))"
apply (unfold bal_def)
apply (cases l)
apply simp
apply (rename_tac t1 t2)
apply (case_tac t2)
apply simp
apply (simp add: max_def split: split_if_asm)
apply arith
done
lemma is_bal_r_rot:
"\<lbrakk> height l = Suc(Suc(height r)); bal l \<noteq> Right; is_bal l; is_bal r \<rbrakk>
\<Longrightarrow> is_bal (r_rot (n, l, r))"
apply (unfold bal_def)
apply (cases "l")
apply simp
apply (simp add: max_def split: split_if_asm)
done
lemma is_bal_rl_rot:
"\<lbrakk> height r = Suc(Suc(height l)); bal r = Left; is_bal l; is_bal r \<rbrakk>
\<Longrightarrow> is_bal (rl_rot (n, l, r))"
apply (unfold bal_def)
apply (cases r)
apply simp
apply (rename_tac t1 t2)
apply (case_tac t1)
apply (simp add: max_def split: split_if_asm)
apply (simp add: max_def split: split_if_asm)
apply arith
done
lemma is_bal_l_rot:
"\<lbrakk> height r = Suc(Suc(height l)); bal r \<noteq> Left; is_bal l; is_bal r \<rbrakk>
\<Longrightarrow> is_bal (l_rot (n, l, r))"
apply (unfold bal_def)
apply (cases r)
apply simp
apply (simp add: max_def split: split_if_asm)
done
text {* Lemmas about height after rotation *}
lemma height_lr_rot:
"\<lbrakk> bal l = Right; height l = Suc(Suc(height r)) \<rbrakk>
\<Longrightarrow> Suc(height (lr_rot (n, l, r))) = height (MKT n l r) "
apply (unfold bal_def)
apply (cases l)
apply simp
apply (rename_tac t1 t2)
apply (case_tac t2)
apply simp
apply (simp add: max_def split: split_if_asm)
done
lemma height_r_rot:
"\<lbrakk> height l = Suc(Suc(height r)); bal l \<noteq> Right \<rbrakk>
\<Longrightarrow> Suc(height (r_rot (n, l, r))) = height (MKT n l r) \<or>
height (r_rot (n, l, r)) = height (MKT n l r)"
apply (unfold bal_def)
apply (cases l)
apply simp
apply (simp add: max_def split: split_if_asm)
done
lemma height_l_bal:
"height l = Suc(Suc(height r))
\<Longrightarrow> Suc(height (l_bal n l r)) = height (MKT n l r) |
height (l_bal n l r) = height (MKT n l r)"
apply (unfold l_bal_def)
apply (cases "bal l = Right")
apply (fastsimp dest: height_lr_rot)
apply (fastsimp dest: height_r_rot)
done
lemma height_rl_rot [rule_format (no_asm)]:
"height r = Suc(Suc(height l)) \<longrightarrow> bal r = Left
\<longrightarrow> Suc(height (rl_rot (n, l, r))) = height (MKT n l r)"
apply (unfold bal_def)
apply (cases r)
apply simp
apply (rename_tac t1 t2)
apply (case_tac t1)
apply simp
apply (simp add: max_def split: split_if_asm)
done
lemma height_l_rot [rule_format (no_asm)]:
"height r = Suc(Suc(height l)) \<longrightarrow> bal r \<noteq> Left
\<longrightarrow> Suc(height (l_rot (n, l, r))) = height (MKT n l r) \<or>
height (l_rot (n, l, r)) = height (MKT n l r)"
apply (unfold bal_def)
apply (cases r)
apply simp
apply (simp add: max_def)
done
lemma height_r_bal:
"height r = Suc(Suc(height l))
\<Longrightarrow> Suc(height (r_bal n l r)) = height (MKT n l r) \<or>
height (r_bal n l r) = height (MKT n l r)"
apply (unfold r_bal_def)
apply (cases "bal r = Left")
apply (fastsimp dest: height_rl_rot)
apply (fastsimp dest: height_l_rot)
done
lemma height_insert [rule_format (no_asm)]:
"is_bal t
\<longrightarrow> height (insert x t) = height t \<or> height (insert x t) = Suc(height t)"
apply (induct_tac "t")
apply simp
apply (rename_tac n t1 t2)
apply (case_tac "x=n")
apply simp
apply (case_tac "x<n")
apply (case_tac "height (insert x t1) = Suc (Suc (height t2))")
apply (frule_tac n = n in height_l_bal)
apply (simp add: max_def)
apply fastsimp
apply (simp add: max_def)
apply fastsimp
apply (case_tac "height (insert x t2) = Suc (Suc (height t1))")
apply (frule_tac n = n in height_r_bal)
apply (fastsimp simp add: max_def)
apply (simp add: max_def)
apply fastsimp
done
lemma is_bal_insert_left:
"\<lbrakk>height (insert x l) \<noteq> Suc(Suc(height r)); is_bal (insert x l); is_bal (MKT n l r)\<rbrakk>
\<Longrightarrow> is_bal (MKT n (insert x l) r)"
apply (cut_tac x = "x" and t = "l" in height_insert)
apply simp
apply fastsimp
done
lemma is_bal_insert_right:
"\<lbrakk> height (insert x r) \<noteq> Suc(Suc(height l)); is_bal (insert x r); is_bal (MKT n l r) \<rbrakk>
\<Longrightarrow> is_bal (MKT n l (insert x r))"
apply (cut_tac x = "x" and t = "r" in height_insert)
apply simp
apply fastsimp
done
lemma is_bal_insert [rule_format (no_asm)]:
"is_bal t \<longrightarrow> is_bal(insert x t)"
apply (induct_tac "t")
apply simp
apply (rename_tac n t1 t2)
apply (case_tac "x=n")
apply simp
apply (case_tac "x<n")
apply (case_tac "height (insert x t1) = Suc (Suc (height t2))")
apply (case_tac "bal (insert x t1) = Right")
apply (fastsimp intro: is_bal_lr_rot simp add: l_bal_def)
apply (fastsimp intro: is_bal_r_rot simp add: l_bal_def)
apply clarify
apply (frule is_bal_insert_left)
apply simp
apply assumption
apply simp
apply (case_tac "height (insert x t2) = Suc (Suc (height t1))")
apply (case_tac "bal (insert x t2) = Left")
apply (fastsimp intro: is_bal_rl_rot simp add: r_bal_def)
apply (fastsimp intro: is_bal_l_rot simp add: r_bal_def)
apply clarify
apply (frule is_bal_insert_right)
apply simp
apply assumption
apply simp
done
subsection "is-in"
lemma is_in_lr_rot:
"\<lbrakk> height l = Suc(Suc(height r)); bal l = Right \<rbrakk>
\<Longrightarrow> is_in x (lr_rot (n, l, r)) = is_in x (MKT n l r)"
apply (unfold bal_def)
apply (cases l)
apply simp
apply (rename_tac t1 t2)
apply (case_tac t2)
apply simp
apply fastsimp
done
lemma is_in_r_rot:
"\<lbrakk> height l = Suc(Suc(height r)); bal l \<noteq> Right \<rbrakk>
\<Longrightarrow> is_in x (r_rot (n, l, r)) = is_in x (MKT n l r)"
apply (unfold bal_def)
apply (cases l)
apply simp
apply fastsimp
done
lemma is_in_rl_rot:
"\<lbrakk> height r = Suc(Suc(height l)); bal r = Left \<rbrakk>
\<Longrightarrow> is_in x (rl_rot (n, l, r)) = is_in x (MKT n l r)"
apply (unfold bal_def)
apply (cases r)
apply simp
apply (rename_tac t1 t2)
apply (case_tac t1)
apply (simp add: max_def le_def)
apply fastsimp
done
lemma is_in_l_rot:
"\<lbrakk> height r = Suc(Suc(height l)); bal r \<noteq> Left \<rbrakk>
\<Longrightarrow> is_in x (l_rot (n, l, r)) = is_in x (MKT n l r)"
apply (unfold bal_def)
apply (cases r)
apply simp
apply fastsimp
done
lemma is_in_insert:
"is_in y (insert x t) = (y=x \<or> is_in y t)"
apply (induct t)
apply simp
apply (simp add: l_bal_def is_in_lr_rot is_in_r_rot r_bal_def
is_in_rl_rot is_in_l_rot)
apply blast
done
lemma is_in_ord_l [rule_format (no_asm)]:
"is_ord (MKT n l r) \<longrightarrow> x < n \<longrightarrow> is_in x (MKT n l r) \<longrightarrow> is_in x l"
apply(auto)
done
lemma is_in_ord_r [rule_format (no_asm)]:
"is_ord (MKT n l r) \<longrightarrow> n < x \<longrightarrow> is_in x (MKT n l r) \<longrightarrow> is_in x r"
apply (auto)
done
subsection "is-in-eff"
lemma is_in_eff_correct [rule_format (no_asm)]: "is_ord t \<longrightarrow> (is_in k t = is_in_eff k t)"
apply (induct_tac "t")
apply (simp (no_asm))
apply (case_tac "k = a")
apply (auto);
done
subsection "is-ord"
lemma is_ord_lr_rot [rule_format (no_asm)]:
"\<lbrakk> height l = Suc(Suc(height r)); bal l = Right; is_ord (MKT n l r) \<rbrakk>
\<Longrightarrow> is_ord (lr_rot (n, l, r))"
apply (unfold bal_def)
apply (cases l)
apply simp
apply (rename_tac t1 t2)
apply (case_tac t2)
apply simp
apply simp
apply (blast intro: order_less_trans)
done
lemma is_ord_r_rot:
"\<lbrakk> height l = Suc(Suc(height r)); bal l \<noteq> Right; is_ord (MKT n l r) \<rbrakk>
\<Longrightarrow> is_ord (r_rot (n, l, r))"
apply (unfold bal_def)
apply (cases l)
apply (simp (no_asm_simp))
apply (auto intro: order_less_trans)
done
lemma is_ord_rl_rot:
"\<lbrakk> height r = Suc(Suc(height l)); bal r = Left; is_ord (MKT n l r) \<rbrakk>
\<Longrightarrow> is_ord (rl_rot (n, l, r))"
apply (unfold bal_def)
apply (cases r)
apply simp
apply (rename_tac t1 t2)
apply (case_tac t1)
apply (simp add: le_def)
apply simp
apply (blast intro: order_less_trans)
done
lemma is_ord_l_rot:
"\<lbrakk> height r = Suc(Suc(height l)); bal r \<noteq> Left; is_ord (MKT n l r) \<rbrakk>
\<Longrightarrow> is_ord (l_rot (n, l, r))"
apply (unfold bal_def)
apply (cases r)
apply simp
apply simp
apply (blast intro: order_less_trans)
done
(* insert operation presreves is_ord property *)
lemma is_ord_insert:
"is_ord t \<Longrightarrow> is_ord(insert (x::'a::linorder) t)"
apply (induct t)
apply simp
apply (cut_tac x = "x" and y = "a" in linorder_less_linear)
apply (fastsimp simp add: l_bal_def is_ord_lr_rot is_ord_r_rot r_bal_def
is_ord_l_rot is_ord_rl_rot is_in_insert)
done
subsection "An extended tree datatype with labels for the balancing information"
datatype 'a etree = EET | EMKT bal 'a "'a etree" "'a etree"
text {*
Pruning, i.e. throwing away the balancing labels:
*}
consts
prune :: "'a etree \<Rightarrow> 'a tree"
primrec
"prune EET = ET"
"prune (EMKT b n l r) =
MKT n (prune l) (prune r)"
text {*
Test if the balancing arguments are correct:
*}
consts
correct :: "'a etree \<Rightarrow> bool"
primrec
"correct EET = True"
"correct (EMKT b n l r) =
(b = bal (MKT n (prune l) (prune r))\<and> correct l\<and> correct r)"
text {*
Add correct balancing labels:
*}
consts
label :: "'a tree \<Rightarrow> 'a etree"
primrec
"label ET = EET"
"label (MKT n l r) =
EMKT (bal (MKT n l r)) n (label l)
(label r)"
lemma correct_prune: "correct (EMKT b n l r) \<longrightarrow> (bal (prune (EMKT b n l r)) = b)"
apply (simp (no_asm_simp) add: bal_def)
done
subsection "Reversing of prune and label"
lemma prune_label: "prune (label t) = t"
apply (induct_tac "t")
apply (simp (no_asm))
apply (simp (no_asm))
apply (erule_tac conjI)
apply assumption
done
lemma label_prune: "correct t \<Longrightarrow> label (prune t) = t"
apply (induct t)
apply auto
done
end