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