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