src/HOL/Data_Structures/AVL_Set.thy
author nipkow
Sun Mar 06 10:33:34 2016 +0100 (2016-03-06)
changeset 62526 347150095fd2
parent 61678 b594e9277be3
child 63411 e051eea34990
permissions -rw-r--r--
tuned
nipkow@61232
     1
(*
nipkow@61232
     2
Author:     Tobias Nipkow
nipkow@61232
     3
Derived from AFP entry AVL.
nipkow@61232
     4
*)
nipkow@61232
     5
nipkow@61232
     6
section "AVL Tree Implementation of Sets"
nipkow@61232
     7
nipkow@61232
     8
theory AVL_Set
nipkow@61581
     9
imports Cmp Isin2
nipkow@61232
    10
begin
nipkow@61232
    11
nipkow@61232
    12
type_synonym 'a avl_tree = "('a,nat) tree"
nipkow@61232
    13
nipkow@61232
    14
text {* Invariant: *}
nipkow@61232
    15
nipkow@61232
    16
fun avl :: "'a avl_tree \<Rightarrow> bool" where
nipkow@61232
    17
"avl Leaf = True" |
nipkow@61232
    18
"avl (Node h l a r) =
nipkow@61232
    19
 ((height l = height r \<or> height l = height r + 1 \<or> height r = height l + 1) \<and> 
nipkow@61232
    20
  h = max (height l) (height r) + 1 \<and> avl l \<and> avl r)"
nipkow@61232
    21
nipkow@61232
    22
fun ht :: "'a avl_tree \<Rightarrow> nat" where
nipkow@61232
    23
"ht Leaf = 0" |
nipkow@61232
    24
"ht (Node h l a r) = h"
nipkow@61232
    25
nipkow@61232
    26
definition node :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow@61232
    27
"node l a r = Node (max (ht l) (ht r) + 1) l a r"
nipkow@61232
    28
nipkow@61581
    29
definition balL :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow@61678
    30
"balL l a r =
nipkow@61678
    31
  (if ht l = ht r + 2 then
nipkow@61678
    32
     case l of 
nipkow@61678
    33
       Node _ bl b br \<Rightarrow>
nipkow@61678
    34
         if ht bl < ht br then
nipkow@61678
    35
           case br of
nipkow@61678
    36
             Node _ cl c cr \<Rightarrow> node (node bl b cl) c (node cr a r)
nipkow@61678
    37
         else node bl b (node br a r)
nipkow@61678
    38
   else node l a r)"
nipkow@61232
    39
nipkow@61581
    40
definition balR :: "'a avl_tree \<Rightarrow> 'a \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow@61678
    41
"balR l a r =
nipkow@61678
    42
   (if ht r = ht l + 2 then
nipkow@61678
    43
      case r of
nipkow@61678
    44
        Node _ bl b br \<Rightarrow>
nipkow@61678
    45
          if ht bl > ht br then
nipkow@61678
    46
            case bl of
nipkow@61678
    47
              Node _ cl c cr \<Rightarrow> node (node l a cl) c (node cr b br)
nipkow@61678
    48
          else node (node l a bl) b br
nipkow@61232
    49
  else node l a r)"
nipkow@61232
    50
nipkow@61581
    51
fun insert :: "'a::cmp \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow@61232
    52
"insert x Leaf = Node 1 Leaf x Leaf" |
nipkow@61581
    53
"insert x (Node h l a r) = (case cmp x a of
nipkow@61581
    54
   EQ \<Rightarrow> Node h l a r |
nipkow@61581
    55
   LT \<Rightarrow> balL (insert x l) a r |
nipkow@61581
    56
   GT \<Rightarrow> balR l a (insert x r))"
nipkow@61232
    57
nipkow@61647
    58
fun del_max :: "'a avl_tree \<Rightarrow> 'a avl_tree * 'a" where
nipkow@61678
    59
"del_max (Node _ l a r) =
nipkow@61678
    60
  (if r = Leaf then (l,a) else let (r',a') = del_max r in (balL l a r', a'))"
nipkow@61232
    61
nipkow@61647
    62
lemmas del_max_induct = del_max.induct[case_names Node Leaf]
nipkow@61232
    63
nipkow@61647
    64
fun del_root :: "'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow@61647
    65
"del_root (Node h Leaf a r) = r" |
nipkow@61647
    66
"del_root (Node h l a Leaf) = l" |
nipkow@61647
    67
"del_root (Node h l a r) = (let (l', a') = del_max l in balR l' a' r)"
nipkow@61232
    68
nipkow@61647
    69
lemmas del_root_cases = del_root.cases[case_names Leaf_t Node_Leaf Node_Node]
nipkow@61232
    70
nipkow@61581
    71
fun delete :: "'a::cmp \<Rightarrow> 'a avl_tree \<Rightarrow> 'a avl_tree" where
nipkow@61232
    72
"delete _ Leaf = Leaf" |
nipkow@61678
    73
"delete x (Node h l a r) =
nipkow@61678
    74
  (case cmp x a of
nipkow@61678
    75
     EQ \<Rightarrow> del_root (Node h l a r) |
nipkow@61678
    76
     LT \<Rightarrow> balR (delete x l) a r |
nipkow@61678
    77
     GT \<Rightarrow> balL l a (delete x r))"
nipkow@61232
    78
nipkow@61232
    79
nipkow@61232
    80
subsection {* Functional Correctness Proofs *}
nipkow@61232
    81
nipkow@61232
    82
text{* Very different from the AFP/AVL proofs *}
nipkow@61232
    83
nipkow@61232
    84
nipkow@61232
    85
subsubsection "Proofs for insert"
nipkow@61232
    86
nipkow@61581
    87
lemma inorder_balL:
nipkow@61581
    88
  "inorder (balL l a r) = inorder l @ a # inorder r"
nipkow@61581
    89
by (auto simp: node_def balL_def split:tree.splits)
nipkow@61232
    90
nipkow@61581
    91
lemma inorder_balR:
nipkow@61581
    92
  "inorder (balR l a r) = inorder l @ a # inorder r"
nipkow@61581
    93
by (auto simp: node_def balR_def split:tree.splits)
nipkow@61232
    94
nipkow@61232
    95
theorem inorder_insert:
nipkow@61232
    96
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
nipkow@61232
    97
by (induct t) 
nipkow@61581
    98
   (auto simp: ins_list_simps inorder_balL inorder_balR)
nipkow@61232
    99
nipkow@61232
   100
nipkow@61232
   101
subsubsection "Proofs for delete"
nipkow@61232
   102
nipkow@61647
   103
lemma inorder_del_maxD:
nipkow@61647
   104
  "\<lbrakk> del_max t = (t',a); t \<noteq> Leaf \<rbrakk> \<Longrightarrow>
nipkow@61232
   105
   inorder t' @ [a] = inorder t"
nipkow@61647
   106
by(induction t arbitrary: t' rule: del_max.induct)
nipkow@61647
   107
  (auto simp: inorder_balL split: if_splits prod.splits tree.split)
nipkow@61232
   108
nipkow@61647
   109
lemma inorder_del_root:
nipkow@61647
   110
  "inorder (del_root (Node h l a r)) = inorder l @ inorder r"
nipkow@62526
   111
by(cases "Node h l a r" rule: del_root.cases)
nipkow@61647
   112
  (auto simp: inorder_balL inorder_balR inorder_del_maxD split: if_splits prod.splits)
nipkow@61232
   113
nipkow@61232
   114
theorem inorder_delete:
nipkow@61232
   115
  "sorted(inorder t) \<Longrightarrow> inorder (delete x t) = del_list x (inorder t)"
nipkow@61232
   116
by(induction t)
nipkow@61581
   117
  (auto simp: del_list_simps inorder_balL inorder_balR
nipkow@61647
   118
    inorder_del_root inorder_del_maxD split: prod.splits)
nipkow@61232
   119
nipkow@61232
   120
nipkow@61232
   121
subsubsection "Overall functional correctness"
nipkow@61232
   122
nipkow@61232
   123
interpretation Set_by_Ordered
nipkow@61232
   124
where empty = Leaf and isin = isin and insert = insert and delete = delete
nipkow@61588
   125
and inorder = inorder and inv = "\<lambda>_. True"
nipkow@61232
   126
proof (standard, goal_cases)
nipkow@61232
   127
  case 1 show ?case by simp
nipkow@61232
   128
next
nipkow@61232
   129
  case 2 thus ?case by(simp add: isin_set)
nipkow@61232
   130
next
nipkow@61232
   131
  case 3 thus ?case by(simp add: inorder_insert)
nipkow@61232
   132
next
nipkow@61232
   133
  case 4 thus ?case by(simp add: inorder_delete)
nipkow@61428
   134
qed (rule TrueI)+
nipkow@61232
   135
nipkow@61232
   136
nipkow@61232
   137
subsection {* AVL invariants *}
nipkow@61232
   138
nipkow@61232
   139
text{* Essentially the AFP/AVL proofs *}
nipkow@61232
   140
nipkow@61232
   141
nipkow@61232
   142
subsubsection {* Insertion maintains AVL balance *}
nipkow@61232
   143
nipkow@61232
   144
declare Let_def [simp]
nipkow@61232
   145
nipkow@61232
   146
lemma [simp]: "avl t \<Longrightarrow> ht t = height t"
nipkow@61232
   147
by (induct t) simp_all
nipkow@61232
   148
nipkow@61581
   149
lemma height_balL:
nipkow@61232
   150
  "\<lbrakk> height l = height r + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
nipkow@61581
   151
   height (balL l a r) = height r + 2 \<or>
nipkow@61581
   152
   height (balL l a r) = height r + 3"
nipkow@61581
   153
by (cases l) (auto simp:node_def balL_def split:tree.split)
nipkow@61232
   154
       
nipkow@61581
   155
lemma height_balR:
nipkow@61232
   156
  "\<lbrakk> height r = height l + 2; avl l; avl r \<rbrakk> \<Longrightarrow>
nipkow@61581
   157
   height (balR l a r) = height l + 2 \<or>
nipkow@61581
   158
   height (balR l a r) = height l + 3"
nipkow@61581
   159
by (cases r) (auto simp add:node_def balR_def split:tree.split)
nipkow@61232
   160
nipkow@61232
   161
lemma [simp]: "height(node l a r) = max (height l) (height r) + 1"
nipkow@61232
   162
by (simp add: node_def)
nipkow@61232
   163
nipkow@61232
   164
lemma avl_node:
nipkow@61232
   165
  "\<lbrakk> avl l; avl r;
nipkow@61232
   166
     height l = height r \<or> height l = height r + 1 \<or> height r = height l + 1
nipkow@61232
   167
   \<rbrakk> \<Longrightarrow> avl(node l a r)"
nipkow@61232
   168
by (auto simp add:max_def node_def)
nipkow@61232
   169
nipkow@61581
   170
lemma height_balL2:
nipkow@61232
   171
  "\<lbrakk> avl l; avl r; height l \<noteq> height r + 2 \<rbrakk> \<Longrightarrow>
nipkow@61581
   172
   height (balL l a r) = (1 + max (height l) (height r))"
nipkow@61581
   173
by (cases l, cases r) (simp_all add: balL_def)
nipkow@61232
   174
nipkow@61581
   175
lemma height_balR2:
nipkow@61232
   176
  "\<lbrakk> avl l;  avl r;  height r \<noteq> height l + 2 \<rbrakk> \<Longrightarrow>
nipkow@61581
   177
   height (balR l a r) = (1 + max (height l) (height r))"
nipkow@61581
   178
by (cases l, cases r) (simp_all add: balR_def)
nipkow@61232
   179
nipkow@61581
   180
lemma avl_balL: 
nipkow@61232
   181
  assumes "avl l" "avl r" and "height l = height r \<or> height l = height r + 1
nipkow@61232
   182
    \<or> height r = height l + 1 \<or> height l = height r + 2" 
nipkow@61581
   183
  shows "avl(balL l a r)"
nipkow@61232
   184
proof(cases l)
nipkow@61232
   185
  case Leaf
nipkow@61581
   186
  with assms show ?thesis by (simp add: node_def balL_def)
nipkow@61232
   187
next
nipkow@61232
   188
  case (Node ln ll lr lh)
nipkow@61232
   189
  with assms show ?thesis
nipkow@61232
   190
  proof(cases "height l = height r + 2")
nipkow@61232
   191
    case True
nipkow@61232
   192
    from True Node assms show ?thesis
nipkow@61581
   193
      by (auto simp: balL_def intro!: avl_node split: tree.split) arith+
nipkow@61232
   194
  next
nipkow@61232
   195
    case False
nipkow@61581
   196
    with assms show ?thesis by (simp add: avl_node balL_def)
nipkow@61232
   197
  qed
nipkow@61232
   198
qed
nipkow@61232
   199
nipkow@61581
   200
lemma avl_balR: 
nipkow@61232
   201
  assumes "avl l" and "avl r" and "height l = height r \<or> height l = height r + 1
nipkow@61232
   202
    \<or> height r = height l + 1 \<or> height r = height l + 2" 
nipkow@61581
   203
  shows "avl(balR l a r)"
nipkow@61232
   204
proof(cases r)
nipkow@61232
   205
  case Leaf
nipkow@61581
   206
  with assms show ?thesis by (simp add: node_def balR_def)
nipkow@61232
   207
next
nipkow@61232
   208
  case (Node rn rl rr rh)
nipkow@61232
   209
  with assms show ?thesis
nipkow@61232
   210
  proof(cases "height r = height l + 2")
nipkow@61232
   211
    case True
nipkow@61232
   212
      from True Node assms show ?thesis
nipkow@61581
   213
        by (auto simp: balR_def intro!: avl_node split: tree.split) arith+
nipkow@61232
   214
  next
nipkow@61232
   215
    case False
nipkow@61581
   216
    with assms show ?thesis by (simp add: balR_def avl_node)
nipkow@61232
   217
  qed
nipkow@61232
   218
qed
nipkow@61232
   219
nipkow@61232
   220
(* It appears that these two properties need to be proved simultaneously: *)
nipkow@61232
   221
nipkow@61232
   222
text{* Insertion maintains the AVL property: *}
nipkow@61232
   223
nipkow@61232
   224
theorem avl_insert_aux:
nipkow@61232
   225
  assumes "avl t"
nipkow@61232
   226
  shows "avl(insert x t)"
nipkow@61232
   227
        "(height (insert x t) = height t \<or> height (insert x t) = height t + 1)"
nipkow@61232
   228
using assms
nipkow@61232
   229
proof (induction t)
nipkow@61232
   230
  case (Node h l a r)
nipkow@61232
   231
  case 1
nipkow@61232
   232
  with Node show ?case
nipkow@61232
   233
  proof(cases "x = a")
nipkow@61232
   234
    case True
nipkow@61232
   235
    with Node 1 show ?thesis by simp
nipkow@61232
   236
  next
nipkow@61232
   237
    case False
nipkow@61232
   238
    with Node 1 show ?thesis 
nipkow@61232
   239
    proof(cases "x<a")
nipkow@61232
   240
      case True
nipkow@61581
   241
      with Node 1 show ?thesis by (auto simp add:avl_balL)
nipkow@61232
   242
    next
nipkow@61232
   243
      case False
nipkow@61581
   244
      with Node 1 `x\<noteq>a` show ?thesis by (auto simp add:avl_balR)
nipkow@61232
   245
    qed
nipkow@61232
   246
  qed
nipkow@61232
   247
  case 2
nipkow@61232
   248
  from 2 Node show ?case
nipkow@61232
   249
  proof(cases "x = a")
nipkow@61232
   250
    case True
nipkow@61232
   251
    with Node 1 show ?thesis by simp
nipkow@61232
   252
  next
nipkow@61232
   253
    case False
nipkow@61232
   254
    with Node 1 show ?thesis 
nipkow@61232
   255
     proof(cases "x<a")
nipkow@61232
   256
      case True
nipkow@61232
   257
      with Node 2 show ?thesis
nipkow@61232
   258
      proof(cases "height (insert x l) = height r + 2")
nipkow@61581
   259
        case False with Node 2 `x < a` show ?thesis by (auto simp: height_balL2)
nipkow@61232
   260
      next
nipkow@61232
   261
        case True 
nipkow@61581
   262
        hence "(height (balL (insert x l) a r) = height r + 2) \<or>
nipkow@61581
   263
          (height (balL (insert x l) a r) = height r + 3)" (is "?A \<or> ?B")
nipkow@61581
   264
          using Node 2 by (intro height_balL) simp_all
nipkow@61232
   265
        thus ?thesis
nipkow@61232
   266
        proof
nipkow@61232
   267
          assume ?A
nipkow@61232
   268
          with 2 `x < a` show ?thesis by (auto)
nipkow@61232
   269
        next
nipkow@61232
   270
          assume ?B
nipkow@61232
   271
          with True 1 Node(2) `x < a` show ?thesis by (simp) arith
nipkow@61232
   272
        qed
nipkow@61232
   273
      qed
nipkow@61232
   274
    next
nipkow@61232
   275
      case False
nipkow@61232
   276
      with Node 2 show ?thesis 
nipkow@61232
   277
      proof(cases "height (insert x r) = height l + 2")
nipkow@61232
   278
        case False
nipkow@61581
   279
        with Node 2 `\<not>x < a` show ?thesis by (auto simp: height_balR2)
nipkow@61232
   280
      next
nipkow@61232
   281
        case True 
nipkow@61581
   282
        hence "(height (balR l a (insert x r)) = height l + 2) \<or>
nipkow@61581
   283
          (height (balR l a (insert x r)) = height l + 3)"  (is "?A \<or> ?B")
nipkow@61581
   284
          using Node 2 by (intro height_balR) simp_all
nipkow@61232
   285
        thus ?thesis 
nipkow@61232
   286
        proof
nipkow@61232
   287
          assume ?A
nipkow@61232
   288
          with 2 `\<not>x < a` show ?thesis by (auto)
nipkow@61232
   289
        next
nipkow@61232
   290
          assume ?B
nipkow@61232
   291
          with True 1 Node(4) `\<not>x < a` show ?thesis by (simp) arith
nipkow@61232
   292
        qed
nipkow@61232
   293
      qed
nipkow@61232
   294
    qed
nipkow@61232
   295
  qed
nipkow@61232
   296
qed simp_all
nipkow@61232
   297
nipkow@61232
   298
nipkow@61232
   299
subsubsection {* Deletion maintains AVL balance *}
nipkow@61232
   300
nipkow@61647
   301
lemma avl_del_max:
nipkow@61232
   302
  assumes "avl x" and "x \<noteq> Leaf"
nipkow@61647
   303
  shows "avl (fst (del_max x))" "height x = height(fst (del_max x)) \<or>
nipkow@61647
   304
         height x = height(fst (del_max x)) + 1"
nipkow@61232
   305
using assms
nipkow@61647
   306
proof (induct x rule: del_max_induct)
nipkow@61647
   307
  case (Node h l a r)
nipkow@61232
   308
  case 1
nipkow@61647
   309
  thus ?case using Node
nipkow@61647
   310
    by (auto simp: height_balL height_balL2 avl_balL
nipkow@61232
   311
      linorder_class.max.absorb1 linorder_class.max.absorb2
nipkow@61232
   312
      split:prod.split)
nipkow@61232
   313
next
nipkow@61647
   314
  case (Node h l a r)
nipkow@61232
   315
  case 2
nipkow@61647
   316
  let ?r' = "fst (del_max r)"
nipkow@61647
   317
  from `avl x` Node 2 have "avl l" and "avl r" by simp_all
nipkow@61581
   318
  thus ?case using Node 2 height_balL[of l ?r' a] height_balL2[of l ?r' a]
nipkow@61232
   319
    apply (auto split:prod.splits simp del:avl.simps) by arith+
nipkow@61232
   320
qed auto
nipkow@61232
   321
nipkow@61647
   322
lemma avl_del_root:
nipkow@61232
   323
  assumes "avl t" and "t \<noteq> Leaf"
nipkow@61647
   324
  shows "avl(del_root t)" 
nipkow@61232
   325
using assms
nipkow@61647
   326
proof (cases t rule:del_root_cases)
nipkow@61232
   327
  case (Node_Node h lh ll ln lr n rh rl rn rr)
nipkow@61232
   328
  let ?l = "Node lh ll ln lr"
nipkow@61232
   329
  let ?r = "Node rh rl rn rr"
nipkow@61647
   330
  let ?l' = "fst (del_max ?l)"
nipkow@61232
   331
  from `avl t` and Node_Node have "avl ?r" by simp
nipkow@61232
   332
  from `avl t` and Node_Node have "avl ?l" by simp
nipkow@61232
   333
  hence "avl(?l')" "height ?l = height(?l') \<or>
nipkow@61647
   334
         height ?l = height(?l') + 1" by (rule avl_del_max,simp)+
nipkow@61232
   335
  with `avl t` Node_Node have "height ?l' = height ?r \<or> height ?l' = height ?r + 1
nipkow@61232
   336
            \<or> height ?r = height ?l' + 1 \<or> height ?r = height ?l' + 2" by fastforce
nipkow@61647
   337
  with `avl ?l'` `avl ?r` have "avl(balR ?l' (snd(del_max ?l)) ?r)"
nipkow@61581
   338
    by (rule avl_balR)
nipkow@61232
   339
  with Node_Node show ?thesis by (auto split:prod.splits)
nipkow@61232
   340
qed simp_all
nipkow@61232
   341
nipkow@61647
   342
lemma height_del_root:
nipkow@61232
   343
  assumes "avl t" and "t \<noteq> Leaf" 
nipkow@61647
   344
  shows "height t = height(del_root t) \<or> height t = height(del_root t) + 1"
nipkow@61232
   345
using assms
nipkow@61647
   346
proof (cases t rule: del_root_cases)
nipkow@61232
   347
  case (Node_Node h lh ll ln lr n rh rl rn rr)
nipkow@61232
   348
  let ?l = "Node lh ll ln lr"
nipkow@61232
   349
  let ?r = "Node rh rl rn rr"
nipkow@61647
   350
  let ?l' = "fst (del_max ?l)"
nipkow@61647
   351
  let ?t' = "balR ?l' (snd(del_max ?l)) ?r"
nipkow@61232
   352
  from `avl t` and Node_Node have "avl ?r" by simp
nipkow@61232
   353
  from `avl t` and Node_Node have "avl ?l" by simp
nipkow@61647
   354
  hence "avl(?l')"  by (rule avl_del_max,simp)
nipkow@61647
   355
  have l'_height: "height ?l = height ?l' \<or> height ?l = height ?l' + 1" using `avl ?l` by (intro avl_del_max) auto
nipkow@61232
   356
  have t_height: "height t = 1 + max (height ?l) (height ?r)" using `avl t` Node_Node by simp
nipkow@61232
   357
  have "height t = height ?t' \<or> height t = height ?t' + 1" using  `avl t` Node_Node
nipkow@61232
   358
  proof(cases "height ?r = height ?l' + 2")
nipkow@61232
   359
    case False
nipkow@61581
   360
    show ?thesis using l'_height t_height False by (subst  height_balR2[OF `avl ?l'` `avl ?r` False])+ arith
nipkow@61232
   361
  next
nipkow@61232
   362
    case True
nipkow@61232
   363
    show ?thesis
nipkow@61647
   364
    proof(cases rule: disjE[OF height_balR[OF True `avl ?l'` `avl ?r`, of "snd (del_max ?l)"]])
nipkow@61232
   365
      case 1
nipkow@61232
   366
      thus ?thesis using l'_height t_height True by arith
nipkow@61232
   367
    next
nipkow@61232
   368
      case 2
nipkow@61232
   369
      thus ?thesis using l'_height t_height True by arith
nipkow@61232
   370
    qed
nipkow@61232
   371
  qed
nipkow@61232
   372
  thus ?thesis using Node_Node by (auto split:prod.splits)
nipkow@61232
   373
qed simp_all
nipkow@61232
   374
nipkow@61232
   375
text{* Deletion maintains the AVL property: *}
nipkow@61232
   376
nipkow@61232
   377
theorem avl_delete_aux:
nipkow@61232
   378
  assumes "avl t" 
nipkow@61232
   379
  shows "avl(delete x t)" and "height t = (height (delete x t)) \<or> height t = height (delete x t) + 1"
nipkow@61232
   380
using assms
nipkow@61232
   381
proof (induct t)
nipkow@61232
   382
  case (Node h l n r)
nipkow@61232
   383
  case 1
nipkow@61232
   384
  with Node show ?case
nipkow@61232
   385
  proof(cases "x = n")
nipkow@61232
   386
    case True
nipkow@61647
   387
    with Node 1 show ?thesis by (auto simp:avl_del_root)
nipkow@61232
   388
  next
nipkow@61232
   389
    case False
nipkow@61232
   390
    with Node 1 show ?thesis 
nipkow@61232
   391
    proof(cases "x<n")
nipkow@61232
   392
      case True
nipkow@61581
   393
      with Node 1 show ?thesis by (auto simp add:avl_balR)
nipkow@61232
   394
    next
nipkow@61232
   395
      case False
nipkow@61581
   396
      with Node 1 `x\<noteq>n` show ?thesis by (auto simp add:avl_balL)
nipkow@61232
   397
    qed
nipkow@61232
   398
  qed
nipkow@61232
   399
  case 2
nipkow@61232
   400
  with Node show ?case
nipkow@61232
   401
  proof(cases "x = n")
nipkow@61232
   402
    case True
nipkow@61647
   403
    with 1 have "height (Node h l n r) = height(del_root (Node h l n r))
nipkow@61647
   404
      \<or> height (Node h l n r) = height(del_root (Node h l n r)) + 1"
nipkow@61647
   405
      by (subst height_del_root,simp_all)
nipkow@61232
   406
    with True show ?thesis by simp
nipkow@61232
   407
  next
nipkow@61232
   408
    case False
nipkow@61232
   409
    with Node 1 show ?thesis 
nipkow@61232
   410
     proof(cases "x<n")
nipkow@61232
   411
      case True
nipkow@61232
   412
      show ?thesis
nipkow@61232
   413
      proof(cases "height r = height (delete x l) + 2")
nipkow@61581
   414
        case False with Node 1 `x < n` show ?thesis by(auto simp: balR_def)
nipkow@61232
   415
      next
nipkow@61232
   416
        case True 
nipkow@61581
   417
        hence "(height (balR (delete x l) n r) = height (delete x l) + 2) \<or>
nipkow@61581
   418
          height (balR (delete x l) n r) = height (delete x l) + 3" (is "?A \<or> ?B")
nipkow@61581
   419
          using Node 2 by (intro height_balR) auto
nipkow@61232
   420
        thus ?thesis 
nipkow@61232
   421
        proof
nipkow@61232
   422
          assume ?A
nipkow@61581
   423
          with `x < n` Node 2 show ?thesis by(auto simp: balR_def)
nipkow@61232
   424
        next
nipkow@61232
   425
          assume ?B
nipkow@61581
   426
          with `x < n` Node 2 show ?thesis by(auto simp: balR_def)
nipkow@61232
   427
        qed
nipkow@61232
   428
      qed
nipkow@61232
   429
    next
nipkow@61232
   430
      case False
nipkow@61232
   431
      show ?thesis
nipkow@61232
   432
      proof(cases "height l = height (delete x r) + 2")
nipkow@61581
   433
        case False with Node 1 `\<not>x < n` `x \<noteq> n` show ?thesis by(auto simp: balL_def)
nipkow@61232
   434
      next
nipkow@61232
   435
        case True 
nipkow@61581
   436
        hence "(height (balL l n (delete x r)) = height (delete x r) + 2) \<or>
nipkow@61581
   437
          height (balL l n (delete x r)) = height (delete x r) + 3" (is "?A \<or> ?B")
nipkow@61581
   438
          using Node 2 by (intro height_balL) auto
nipkow@61232
   439
        thus ?thesis 
nipkow@61232
   440
        proof
nipkow@61232
   441
          assume ?A
nipkow@61581
   442
          with `\<not>x < n` `x \<noteq> n` Node 2 show ?thesis by(auto simp: balL_def)
nipkow@61232
   443
        next
nipkow@61232
   444
          assume ?B
nipkow@61581
   445
          with `\<not>x < n` `x \<noteq> n` Node 2 show ?thesis by(auto simp: balL_def)
nipkow@61232
   446
        qed
nipkow@61232
   447
      qed
nipkow@61232
   448
    qed
nipkow@61232
   449
  qed
nipkow@61232
   450
qed simp_all
nipkow@61232
   451
nipkow@61232
   452
end