src/HOL/Data_Structures/RBT_Set.thy
author nipkow
Thu Jul 07 18:08:02 2016 +0200 (2016-07-07)
changeset 63411 e051eea34990
parent 62526 347150095fd2
child 64242 93c6f0da5c70
permissions -rw-r--r--
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow@63411
     1
(* Author: Tobias Nipkow, Daniel Stüwe *)
nipkow@61224
     2
nipkow@61224
     3
section \<open>Red-Black Tree Implementation of Sets\<close>
nipkow@61224
     4
nipkow@61224
     5
theory RBT_Set
nipkow@61224
     6
imports
nipkow@61224
     7
  RBT
nipkow@61581
     8
  Cmp
nipkow@61224
     9
  Isin2
nipkow@61224
    10
begin
nipkow@61224
    11
nipkow@63411
    12
fun ins :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
nipkow@61749
    13
"ins x Leaf = R Leaf x Leaf" |
nipkow@61749
    14
"ins x (B l a r) =
nipkow@61678
    15
  (case cmp x a of
nipkow@61749
    16
     LT \<Rightarrow> bal (ins x l) a r |
nipkow@61749
    17
     GT \<Rightarrow> bal l a (ins x r) |
nipkow@61678
    18
     EQ \<Rightarrow> B l a r)" |
nipkow@61749
    19
"ins x (R l a r) =
nipkow@61678
    20
  (case cmp x a of
nipkow@61749
    21
    LT \<Rightarrow> R (ins x l) a r |
nipkow@61749
    22
    GT \<Rightarrow> R l a (ins x r) |
nipkow@61678
    23
    EQ \<Rightarrow> R l a r)"
nipkow@61224
    24
nipkow@63411
    25
definition insert :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
nipkow@61749
    26
"insert x t = paint Black (ins x t)"
nipkow@61749
    27
nipkow@63411
    28
fun del :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
nipkow@63411
    29
and delL :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
nipkow@63411
    30
and delR :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
nipkow@61224
    31
where
nipkow@61749
    32
"del x Leaf = Leaf" |
nipkow@61749
    33
"del x (Node _ l a r) =
nipkow@61678
    34
  (case cmp x a of
nipkow@61749
    35
     LT \<Rightarrow> delL x l a r |
nipkow@61749
    36
     GT \<Rightarrow> delR x l a r |
nipkow@61678
    37
     EQ \<Rightarrow> combine l r)" |
nipkow@61749
    38
"delL x (B t1 a t2) b t3 = balL (del x (B t1 a t2)) b t3" |
nipkow@61749
    39
"delL x l a r = R (del x l) a r" |
nipkow@61749
    40
"delR x t1 a (B t2 b t3) = balR t1 a (del x (B t2 b t3))" | 
nipkow@61749
    41
"delR x l a r = R l a (del x r)"
nipkow@61749
    42
nipkow@63411
    43
definition delete :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
nipkow@61749
    44
"delete x t = paint Black (del x t)"
nipkow@61224
    45
nipkow@61224
    46
nipkow@61224
    47
subsection "Functional Correctness Proofs"
nipkow@61224
    48
nipkow@61749
    49
lemma inorder_paint: "inorder(paint c t) = inorder t"
nipkow@62526
    50
by(cases t) (auto)
nipkow@61749
    51
nipkow@61224
    52
lemma inorder_bal:
nipkow@61224
    53
  "inorder(bal l a r) = inorder l @ a # inorder r"
nipkow@62526
    54
by(cases "(l,a,r)" rule: bal.cases) (auto)
nipkow@61224
    55
nipkow@61749
    56
lemma inorder_ins:
nipkow@61749
    57
  "sorted(inorder t) \<Longrightarrow> inorder(ins x t) = ins_list x (inorder t)"
nipkow@61749
    58
by(induction x t rule: ins.induct) (auto simp: ins_list_simps inorder_bal)
nipkow@61749
    59
nipkow@61224
    60
lemma inorder_insert:
nipkow@61749
    61
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
nipkow@61749
    62
by (simp add: insert_def inorder_ins inorder_paint)
nipkow@61224
    63
nipkow@61224
    64
lemma inorder_balL:
nipkow@61224
    65
  "inorder(balL l a r) = inorder l @ a # inorder r"
nipkow@62526
    66
by(cases "(l,a,r)" rule: balL.cases)(auto simp: inorder_bal inorder_paint)
nipkow@61224
    67
nipkow@61224
    68
lemma inorder_balR:
nipkow@61224
    69
  "inorder(balR l a r) = inorder l @ a # inorder r"
nipkow@62526
    70
by(cases "(l,a,r)" rule: balR.cases) (auto simp: inorder_bal inorder_paint)
nipkow@61224
    71
nipkow@61224
    72
lemma inorder_combine:
nipkow@61224
    73
  "inorder(combine l r) = inorder l @ inorder r"
nipkow@61224
    74
by(induction l r rule: combine.induct)
nipkow@61231
    75
  (auto simp: inorder_balL inorder_balR split: tree.split color.split)
nipkow@61224
    76
nipkow@61749
    77
lemma inorder_del:
nipkow@61749
    78
 "sorted(inorder t) \<Longrightarrow>  inorder(del x t) = del_list x (inorder t)"
nipkow@61749
    79
 "sorted(inorder l) \<Longrightarrow>  inorder(delL x l a r) =
nipkow@61678
    80
    del_list x (inorder l) @ a # inorder r"
nipkow@61749
    81
 "sorted(inorder r) \<Longrightarrow>  inorder(delR x l a r) =
nipkow@61224
    82
    inorder l @ a # del_list x (inorder r)"
nipkow@61749
    83
by(induction x t and x l a r and x l a r rule: del_delL_delR.induct)
nipkow@61231
    84
  (auto simp: del_list_simps inorder_combine inorder_balL inorder_balR)
nipkow@61224
    85
nipkow@61749
    86
lemma inorder_delete:
nipkow@61749
    87
  "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
nipkow@61749
    88
by (auto simp: delete_def inorder_del inorder_paint)
nipkow@61749
    89
nipkow@61581
    90
nipkow@63411
    91
subsection \<open>Structural invariants\<close>
nipkow@61224
    92
nipkow@63411
    93
text\<open>The proofs are due to Markus Reiter and Alexander Krauss,\<close>
nipkow@61754
    94
nipkow@61754
    95
fun color :: "'a rbt \<Rightarrow> color" where
nipkow@61754
    96
"color Leaf = Black" |
nipkow@61754
    97
"color (Node c _ _ _) = c"
nipkow@61754
    98
nipkow@61754
    99
fun bheight :: "'a rbt \<Rightarrow> nat" where
nipkow@61754
   100
"bheight Leaf = 0" |
nipkow@61754
   101
"bheight (Node c l x r) = (if c = Black then Suc(bheight l) else bheight l)"
nipkow@61754
   102
nipkow@63411
   103
fun invc :: "'a rbt \<Rightarrow> bool" where
nipkow@63411
   104
"invc Leaf = True" |
nipkow@63411
   105
"invc (Node c l a r) =
nipkow@63411
   106
  (invc l \<and> invc r \<and> (c = Black \<or> color l = Black \<and> color r = Black))"
nipkow@61754
   107
nipkow@63411
   108
fun invc_sons :: "'a rbt \<Rightarrow> bool" \<comment> \<open>Weaker version\<close> where
nipkow@63411
   109
"invc_sons Leaf = True" |
nipkow@63411
   110
"invc_sons (Node c l a r) = (invc l \<and> invc r)"
nipkow@61754
   111
nipkow@63411
   112
fun invh :: "'a rbt \<Rightarrow> bool" where
nipkow@63411
   113
"invh Leaf = True" |
nipkow@63411
   114
"invh (Node c l x r) = (invh l \<and> invh r \<and> bheight l = bheight r)"
nipkow@61754
   115
nipkow@63411
   116
lemma invc_sonsI: "invc t \<Longrightarrow> invc_sons t"
nipkow@61754
   117
by (cases t) simp+
nipkow@61754
   118
nipkow@61754
   119
definition rbt :: "'a rbt \<Rightarrow> bool" where
nipkow@63411
   120
"rbt t = (invc t \<and> invh t \<and> color t = Black)"
nipkow@61754
   121
nipkow@61754
   122
lemma color_paint_Black: "color (paint Black t) = Black"
nipkow@61754
   123
by (cases t) auto
nipkow@61754
   124
nipkow@61754
   125
theorem rbt_Leaf: "rbt Leaf"
nipkow@61754
   126
by (simp add: rbt_def)
nipkow@61754
   127
nipkow@63411
   128
lemma paint_invc_sons: "invc_sons t \<Longrightarrow> invc_sons (paint c t)"
nipkow@61754
   129
by (cases t) auto
nipkow@61754
   130
nipkow@63411
   131
lemma invc_paint_Black: "invc_sons t \<Longrightarrow> invc (paint Black t)"
nipkow@61754
   132
by (cases t) auto
nipkow@61754
   133
nipkow@63411
   134
lemma invh_paint: "invh t \<Longrightarrow> invh (paint c t)"
nipkow@61754
   135
by (cases t) auto
nipkow@61754
   136
nipkow@63411
   137
lemma invc_bal: "\<lbrakk>invc_sons l; invc_sons r\<rbrakk> \<Longrightarrow> invc (bal l a r)" 
nipkow@61754
   138
by (induct l a r rule: bal.induct) auto
nipkow@61754
   139
nipkow@61754
   140
lemma bheight_bal:
nipkow@61754
   141
  "bheight l = bheight r \<Longrightarrow> bheight (bal l a r) = Suc (bheight l)"
nipkow@61754
   142
by (induct l a r rule: bal.induct) auto
nipkow@61754
   143
nipkow@63411
   144
lemma invh_bal: 
nipkow@63411
   145
  "\<lbrakk> invh l; invh r; bheight l = bheight r \<rbrakk> \<Longrightarrow> invh (bal l a r)"
nipkow@61754
   146
by (induct l a r rule: bal.induct) auto
nipkow@61754
   147
nipkow@61754
   148
nipkow@61754
   149
subsubsection \<open>Insertion\<close>
nipkow@61754
   150
nipkow@63411
   151
lemma invc_ins: assumes "invc t"
nipkow@63411
   152
  shows "color t = Black \<Longrightarrow> invc (ins x t)" "invc_sons (ins x t)"
nipkow@61754
   153
using assms
nipkow@63411
   154
by (induct x t rule: ins.induct) (auto simp: invc_bal invc_sonsI)
nipkow@61754
   155
nipkow@63411
   156
lemma invh_ins: assumes "invh t"
nipkow@63411
   157
  shows "invh (ins x t)" "bheight (ins x t) = bheight t"
nipkow@61754
   158
using assms
nipkow@63411
   159
by (induct x t rule: ins.induct) (auto simp: invh_bal bheight_bal)
nipkow@61754
   160
nipkow@63411
   161
theorem rbt_insert: "rbt t \<Longrightarrow> rbt (insert x t)"
nipkow@63411
   162
by (simp add: invc_ins invh_ins color_paint_Black invc_paint_Black invh_paint
nipkow@61754
   163
  rbt_def insert_def)
nipkow@61754
   164
nipkow@63411
   165
nipkow@63411
   166
subsubsection \<open>Deletion\<close>
nipkow@63411
   167
nipkow@63411
   168
lemma bheight_paint_Red:
nipkow@63411
   169
  "color t = Black \<Longrightarrow> bheight (paint Red t) = bheight t - 1"
nipkow@61754
   170
by (cases t) auto
nipkow@61754
   171
nipkow@63411
   172
lemma balL_invh_with_invc:
nipkow@63411
   173
  assumes "invh lt" "invh rt" "bheight lt + 1 = bheight rt" "invc rt"
nipkow@63411
   174
  shows "bheight (balL lt a rt) = bheight lt + 1"  "invh (balL lt a rt)"
nipkow@61754
   175
using assms 
nipkow@63411
   176
by (induct lt a rt rule: balL.induct)
nipkow@63411
   177
   (auto simp: invh_bal invh_paint bheight_bal bheight_paint_Red)
nipkow@61754
   178
nipkow@63411
   179
lemma balL_invh_app: 
nipkow@63411
   180
  assumes "invh lt" "invh rt" "bheight lt + 1 = bheight rt" "color rt = Black"
nipkow@63411
   181
  shows "invh (balL lt a rt)" 
nipkow@61754
   182
        "bheight (balL lt a rt) = bheight rt"
nipkow@61754
   183
using assms 
nipkow@63411
   184
by (induct lt a rt rule: balL.induct) (auto simp add: invh_bal bheight_bal) 
nipkow@61754
   185
nipkow@63411
   186
lemma balL_invc: "\<lbrakk>invc_sons l; invc r; color r = Black\<rbrakk> \<Longrightarrow> invc (balL l a r)"
nipkow@63411
   187
by (induct l a r rule: balL.induct) (simp_all add: invc_bal)
nipkow@61754
   188
nipkow@63411
   189
lemma balL_invc_sons: "\<lbrakk> invc_sons lt; invc rt \<rbrakk> \<Longrightarrow> invc_sons (balL lt a rt)"
nipkow@63411
   190
by (induct lt a rt rule: balL.induct) (auto simp: invc_bal paint_invc_sons invc_sonsI)
nipkow@61754
   191
nipkow@63411
   192
lemma balR_invh_with_invc:
nipkow@63411
   193
  assumes "invh lt" "invh rt" "bheight lt = bheight rt + 1" "invc lt"
nipkow@63411
   194
  shows "invh (balR lt a rt) \<and> bheight (balR lt a rt) = bheight lt"
nipkow@61754
   195
using assms
nipkow@63411
   196
by(induct lt a rt rule: balR.induct)
nipkow@63411
   197
  (auto simp: invh_bal bheight_bal invh_paint bheight_paint_Red)
nipkow@61754
   198
nipkow@63411
   199
lemma invc_balR: "\<lbrakk>invc a; invc_sons b; color a = Black\<rbrakk> \<Longrightarrow> invc (balR a x b)"
nipkow@63411
   200
by (induct a x b rule: balR.induct) (simp_all add: invc_bal)
nipkow@61754
   201
nipkow@63411
   202
lemma invc_sons_balR: "\<lbrakk> invc lt; invc_sons rt \<rbrakk> \<Longrightarrow>invc_sons (balR lt x rt)"
nipkow@63411
   203
by (induct lt x rt rule: balR.induct) (auto simp: invc_bal paint_invc_sons invc_sonsI)
nipkow@61754
   204
nipkow@63411
   205
lemma invh_combine:
nipkow@63411
   206
  assumes "invh lt" "invh rt" "bheight lt = bheight rt"
nipkow@63411
   207
  shows "bheight (combine lt rt) = bheight lt" "invh (combine lt rt)"
nipkow@61754
   208
using assms 
nipkow@61754
   209
by (induct lt rt rule: combine.induct) 
nipkow@63411
   210
   (auto simp: balL_invh_app split: tree.splits color.splits)
nipkow@61754
   211
nipkow@63411
   212
lemma invc_combine: 
nipkow@63411
   213
  assumes "invc lt" "invc rt"
nipkow@63411
   214
  shows "color lt = Black \<Longrightarrow> color rt = Black \<Longrightarrow> invc (combine lt rt)"
nipkow@63411
   215
         "invc_sons (combine lt rt)"
nipkow@61754
   216
using assms 
nipkow@61754
   217
by (induct lt rt rule: combine.induct)
nipkow@63411
   218
   (auto simp: balL_invc invc_sonsI split: tree.splits color.splits)
nipkow@61754
   219
nipkow@61754
   220
nipkow@63411
   221
lemma assumes "invh lt" "invc lt"
nipkow@61754
   222
  shows
nipkow@63411
   223
  del_invc_invh: "invh (del x lt) \<and> (color lt = Red \<and> bheight (del x lt) = bheight lt \<and> invc (del x lt) 
nipkow@63411
   224
  \<or> color lt = Black \<and> bheight (del x lt) = bheight lt - 1 \<and> invc_sons (del x lt))"
nipkow@63411
   225
and  "\<lbrakk>invh rt; bheight lt = bheight rt; invc rt\<rbrakk> \<Longrightarrow>
nipkow@63411
   226
   invh (delL x lt k rt) \<and> 
nipkow@63411
   227
   bheight (delL x lt k rt) = bheight lt \<and> 
nipkow@63411
   228
   (color lt = Black \<and> color rt = Black \<and> invc (delL x lt k rt) \<or> 
nipkow@63411
   229
    (color lt \<noteq> Black \<or> color rt \<noteq> Black) \<and> invc_sons (delL x lt k rt))"
nipkow@63411
   230
  and "\<lbrakk>invh rt; bheight lt = bheight rt; invc rt\<rbrakk> \<Longrightarrow>
nipkow@63411
   231
  invh (delR x lt k rt) \<and> 
nipkow@63411
   232
  bheight (delR x lt k rt) = bheight lt \<and> 
nipkow@63411
   233
  (color lt = Black \<and> color rt = Black \<and> invc (delR x lt k rt) \<or> 
nipkow@63411
   234
   (color lt \<noteq> Black \<or> color rt \<noteq> Black) \<and> invc_sons (delR x lt k rt))"
nipkow@61754
   235
using assms
nipkow@63411
   236
proof (induct x lt and x lt k rt and x lt k rt rule: del_delL_delR.induct)
nipkow@61754
   237
case (2 y c _ y')
nipkow@61754
   238
  have "y = y' \<or> y < y' \<or> y > y'" by auto
nipkow@61754
   239
  thus ?case proof (elim disjE)
nipkow@61754
   240
    assume "y = y'"
nipkow@63411
   241
    with 2 show ?thesis
nipkow@63411
   242
    by (cases c) (simp_all add: invh_combine invc_combine)
nipkow@61754
   243
  next
nipkow@61754
   244
    assume "y < y'"
nipkow@63411
   245
    with 2 show ?thesis by (cases c) (auto simp: invc_sonsI)
nipkow@61754
   246
  next
nipkow@61754
   247
    assume "y' < y"
nipkow@63411
   248
    with 2 show ?thesis by (cases c) (auto simp: invc_sonsI)
nipkow@61754
   249
  qed
nipkow@61754
   250
next
nipkow@63411
   251
  case (3 y lt z rta y' bb)
nipkow@63411
   252
  thus ?case by (cases "color (Node Black lt z rta) = Black \<and> color bb = Black") (simp add: balL_invh_with_invc balL_invc balL_invc_sons)+
nipkow@61754
   253
next
nipkow@63411
   254
  case (5 y a y' lt z rta)
nipkow@63411
   255
  thus ?case by (cases "color a = Black \<and> color (Node Black lt z rta) = Black") (simp add: balR_invh_with_invc invc_balR invc_sons_balR)+
nipkow@61754
   256
next
nipkow@63411
   257
  case ("6_1" y a y') thus ?case by (cases "color a = Black \<and> color Leaf = Black") simp+
nipkow@61754
   258
qed auto
nipkow@61754
   259
nipkow@63411
   260
theorem rbt_delete: "rbt t \<Longrightarrow> rbt (delete k t)"
nipkow@63411
   261
by (metis delete_def rbt_def color_paint_Black del_invc_invh invc_paint_Black invc_sonsI invh_paint)
nipkow@63411
   262
nipkow@63411
   263
text \<open>Overall correctness:\<close>
nipkow@63411
   264
nipkow@63411
   265
interpretation Set_by_Ordered
nipkow@63411
   266
where empty = Leaf and isin = isin and insert = insert and delete = delete
nipkow@63411
   267
and inorder = inorder and inv = rbt
nipkow@63411
   268
proof (standard, goal_cases)
nipkow@63411
   269
  case 1 show ?case by simp
nipkow@63411
   270
next
nipkow@63411
   271
  case 2 thus ?case by(simp add: isin_set)
nipkow@63411
   272
next
nipkow@63411
   273
  case 3 thus ?case by(simp add: inorder_insert)
nipkow@63411
   274
next
nipkow@63411
   275
  case 4 thus ?case by(simp add: inorder_delete)
nipkow@63411
   276
next
nipkow@63411
   277
  case 5 thus ?case by (simp add: rbt_Leaf) 
nipkow@63411
   278
next
nipkow@63411
   279
  case 6 thus ?case by (simp add: rbt_insert) 
nipkow@63411
   280
next
nipkow@63411
   281
  case 7 thus ?case by (simp add: rbt_delete) 
nipkow@63411
   282
qed
nipkow@63411
   283
nipkow@63411
   284
nipkow@63411
   285
subsection \<open>Height-Size Relation\<close>
nipkow@63411
   286
nipkow@63411
   287
text \<open>By Daniel St\"uwe\<close>
nipkow@63411
   288
nipkow@63411
   289
lemma color_RedE:"color t = Red \<Longrightarrow> invc t =
nipkow@63411
   290
 (\<exists> l a r . t = R l a r \<and> color l = Black \<and> color r = Black \<and> invc l \<and> invc r)"
nipkow@63411
   291
by (cases t) auto
nipkow@63411
   292
nipkow@63411
   293
lemma rbt_induct[consumes 1]:
nipkow@63411
   294
  assumes "rbt t"
nipkow@63411
   295
  assumes [simp]: "P Leaf"
nipkow@63411
   296
  assumes "\<And> t l a r. \<lbrakk>t = B l a r; invc t; invh t; Q(l); Q(r)\<rbrakk> \<Longrightarrow> P t"
nipkow@63411
   297
  assumes "\<And> t l a r. \<lbrakk>t = R l a r; invc t; invh t; P(l); P(r)\<rbrakk> \<Longrightarrow> Q t"
nipkow@63411
   298
  assumes "\<And> t . P(t) \<Longrightarrow> Q(t)"
nipkow@63411
   299
  shows "P t"
nipkow@63411
   300
using assms(1) unfolding rbt_def apply safe
nipkow@63411
   301
proof (induction t rule: measure_induct[of size])
nipkow@63411
   302
case (1 t)
nipkow@63411
   303
  note * = 1 assms
nipkow@63411
   304
  show ?case proof (cases t)
nipkow@63411
   305
    case [simp]: (Node c l a r)
nipkow@63411
   306
    show ?thesis proof (cases c)
nipkow@63411
   307
      case Red thus ?thesis using 1 by simp
nipkow@63411
   308
    next
nipkow@63411
   309
      case [simp]: Black
nipkow@63411
   310
      show ?thesis
nipkow@63411
   311
      proof (cases "color l")
nipkow@63411
   312
        case Red
nipkow@63411
   313
        thus ?thesis using * by (cases "color r") (auto simp: color_RedE)
nipkow@63411
   314
      next
nipkow@63411
   315
        case Black
nipkow@63411
   316
        thus ?thesis using * by (cases "color r") (auto simp: color_RedE)
nipkow@63411
   317
      qed
nipkow@63411
   318
    qed
nipkow@63411
   319
  qed simp
nipkow@63411
   320
qed
nipkow@63411
   321
nipkow@63411
   322
lemma rbt_b_height: "rbt t \<Longrightarrow> bheight t * 2 \<ge> height t"
nipkow@63411
   323
by (induction t rule: rbt_induct[where Q="\<lambda> t. bheight t * 2 + 1 \<ge> height t"]) auto
nipkow@63411
   324
nipkow@63411
   325
lemma red_b_height: "invc t \<Longrightarrow> invh t \<Longrightarrow> bheight t * 2 + 1 \<ge> height t"
nipkow@63411
   326
apply (cases t) apply simp
nipkow@63411
   327
  using rbt_b_height unfolding rbt_def
nipkow@63411
   328
  by (cases "color t") fastforce+
nipkow@63411
   329
nipkow@63411
   330
lemma red_b_height2: "invc t \<Longrightarrow> invh t \<Longrightarrow> bheight t \<ge> height t div 2"
nipkow@63411
   331
using red_b_height by fastforce
nipkow@63411
   332
nipkow@63411
   333
lemma rbt_b_height2: "bheight t \<le> height t"
nipkow@63411
   334
by (induction t) auto
nipkow@63411
   335
nipkow@63411
   336
lemma "rbt t \<Longrightarrow> size1 t \<le>  4 ^ (bheight t)"
nipkow@63411
   337
by(induction t rule: rbt_induct[where Q="\<lambda> t. size1 t \<le>  2 * 4 ^ (bheight t)"]) auto
nipkow@63411
   338
nipkow@63411
   339
lemma bheight_size_bound:  "rbt t \<Longrightarrow> size1 t \<ge>  2 ^ (bheight t)"
nipkow@63411
   340
by (induction t rule: rbt_induct[where Q="\<lambda> t. size1 t \<ge>  2 ^ (bheight t)"]) auto
nipkow@63411
   341
nipkow@63411
   342
text \<open>Balanced red-balck tree with all black nodes:\<close>
nipkow@63411
   343
inductive balB :: "nat \<Rightarrow> unit rbt \<Rightarrow> bool"  where
nipkow@63411
   344
"balB 0 Leaf" |
nipkow@63411
   345
"balB h t \<Longrightarrow> balB (Suc h) (B t () t)"
nipkow@63411
   346
nipkow@63411
   347
inductive_cases [elim!]: "balB 0 t"
nipkow@63411
   348
inductive_cases [elim]: "balB (Suc h) t"
nipkow@63411
   349
nipkow@63411
   350
lemma balB_hs: "balB h t \<Longrightarrow> bheight t = height t"
nipkow@63411
   351
by (induction h t rule: "balB.induct") auto
nipkow@63411
   352
nipkow@63411
   353
lemma balB_h: "balB h t \<Longrightarrow> h = height t"
nipkow@63411
   354
by (induction h t rule: "balB.induct") auto
nipkow@63411
   355
nipkow@63411
   356
lemma "rbt t \<Longrightarrow> balB (bheight t) t' \<Longrightarrow> size t' \<le> size t"
nipkow@63411
   357
by (induction t arbitrary: t' 
nipkow@63411
   358
 rule: rbt_induct[where Q="\<lambda> t . \<forall> h t'. balB (bheight t) t' \<longrightarrow> size t' \<le> size t"])
nipkow@63411
   359
 fastforce+
nipkow@63411
   360
nipkow@63411
   361
lemma balB_bh: "invc t \<Longrightarrow> invh t \<Longrightarrow> balB (bheight t) t' \<Longrightarrow> size t' \<le> size t"
nipkow@63411
   362
by (induction t arbitrary: t') (fastforce split: if_split_asm)+
nipkow@63411
   363
nipkow@63411
   364
lemma balB_bh3:"\<lbrakk> balB h t; balB (h' + h) t' \<rbrakk> \<Longrightarrow> size t \<le> size t'"
nipkow@63411
   365
by (induction h t arbitrary: t' h' rule: balB.induct)  fastforce+
nipkow@63411
   366
nipkow@63411
   367
corollary balB_bh3': "\<lbrakk> balB h t; balB h' t'; h \<le> h' \<rbrakk> \<Longrightarrow> size t \<le> size t'"
nipkow@63411
   368
using balB_bh3 le_Suc_ex by (fastforce simp: algebra_simps)
nipkow@63411
   369
nipkow@63411
   370
lemma exist_pt: "\<exists> t . balB h t"
nipkow@63411
   371
by (induction h) (auto intro: balB.intros)
nipkow@63411
   372
nipkow@63411
   373
corollary compact_pt:
nipkow@63411
   374
  assumes "invc t" "invh t" "h \<le> bheight t" "balB h t'"
nipkow@63411
   375
  shows   "size t' \<le> size t"
nipkow@61754
   376
proof -
nipkow@63411
   377
  obtain t'' where "balB (bheight t) t''" using exist_pt by blast
nipkow@63411
   378
  thus ?thesis using assms balB_bh[of t t''] balB_bh3'[of h t' "bheight t" t''] by auto
nipkow@63411
   379
qed
nipkow@63411
   380
nipkow@63411
   381
lemma balB_bh2: "balB (bheight t) t'\<Longrightarrow> invc t \<Longrightarrow> invh t \<Longrightarrow> height t' \<le> height t"
nipkow@63411
   382
apply (induction "(bheight t)" t' arbitrary: t rule: balB.induct)
nipkow@63411
   383
using balB_h rbt_b_height2 by auto
nipkow@63411
   384
nipkow@63411
   385
lemma balB_rbt: "balB h t \<Longrightarrow> rbt t"
nipkow@63411
   386
unfolding rbt_def
nipkow@63411
   387
by (induction h t rule: balB.induct) auto
nipkow@63411
   388
nipkow@63411
   389
lemma balB_size[simp]: "balB h t \<Longrightarrow> size1 t = 2^h"
nipkow@63411
   390
by (induction h t rule: balB.induct) auto
nipkow@63411
   391
nipkow@63411
   392
text \<open>Red-black tree (except that the root may be red) of minimal size
nipkow@63411
   393
for a given height:\<close>
nipkow@63411
   394
nipkow@63411
   395
inductive RB :: "nat \<Rightarrow> unit rbt \<Rightarrow> bool" where
nipkow@63411
   396
"RB 0 Leaf" |
nipkow@63411
   397
"balB (h div 2) t \<Longrightarrow> RB h t' \<Longrightarrow> color t' = Red \<Longrightarrow> RB (Suc h) (B t' () t)" |
nipkow@63411
   398
"balB (h div 2) t \<Longrightarrow> RB h t' \<Longrightarrow> color t' = Black \<Longrightarrow> RB (Suc h) (R t' () t)" 
nipkow@63411
   399
nipkow@63411
   400
lemmas RB.intros[intro]
nipkow@63411
   401
nipkow@63411
   402
lemma RB_invc: "RB h t \<Longrightarrow> invc t"
nipkow@63411
   403
apply (induction h t rule: RB.induct)
nipkow@63411
   404
using balB_rbt unfolding rbt_def by auto
nipkow@63411
   405
nipkow@63411
   406
lemma RB_h: "RB h t \<Longrightarrow> h = height t"
nipkow@63411
   407
apply (induction h t rule: RB.induct)
nipkow@63411
   408
using balB_h by auto
nipkow@63411
   409
nipkow@63411
   410
lemma RB_mod: "RB h t \<Longrightarrow> (color t = Black \<longleftrightarrow> h mod 2 = 0)"
nipkow@63411
   411
apply (induction h t rule: RB.induct)
nipkow@63411
   412
apply auto
nipkow@63411
   413
by presburger
nipkow@63411
   414
nipkow@63411
   415
lemma RB_b_height: "RB h t \<Longrightarrow> height t div 2 = bheight t"
nipkow@63411
   416
proof  (induction h t rule: RB.induct)
nipkow@63411
   417
  case 1 
nipkow@63411
   418
  thus ?case by auto 
nipkow@63411
   419
next
nipkow@63411
   420
  case (2 h t t')
nipkow@63411
   421
  with RB_mod obtain n where "2*n + 1 = h" 
nipkow@63411
   422
    by (metis color.distinct(1) mod_div_equality2 parity) 
nipkow@63411
   423
  with 2 balB_h RB_h show ?case by auto
nipkow@63411
   424
next
nipkow@63411
   425
  case (3 h t t')
nipkow@63411
   426
  with RB_mod[OF 3(2)] parity obtain n where "2*n = h" by blast
nipkow@63411
   427
  with 3 balB_h RB_h show ?case by auto
nipkow@61754
   428
qed
nipkow@63411
   429
nipkow@63411
   430
lemma weak_RB_induct[consumes 1]: 
nipkow@63411
   431
  "RB h t \<Longrightarrow> P 0 \<langle>\<rangle> \<Longrightarrow> (\<And>h t t' c . balB (h div 2) t \<Longrightarrow> RB h t' \<Longrightarrow>
nipkow@63411
   432
    P h t' \<Longrightarrow> P (Suc h) (Node c t' () t)) \<Longrightarrow> P h t"
nipkow@63411
   433
using RB.induct by metis
nipkow@63411
   434
nipkow@63411
   435
lemma RB_invh: "RB h t \<Longrightarrow> invh t"
nipkow@63411
   436
apply (induction h t rule: weak_RB_induct)
nipkow@63411
   437
  using balB_h balB_hs RB_h balB_rbt RB_b_height
nipkow@63411
   438
  unfolding rbt_def
nipkow@63411
   439
by auto
nipkow@63411
   440
nipkow@63411
   441
lemma RB_bheight_minimal:
nipkow@63411
   442
  "\<lbrakk>RB (height t') t; invc t'; invh t'\<rbrakk> \<Longrightarrow> bheight t \<le> bheight t'"
nipkow@63411
   443
using RB_b_height RB_h red_b_height2 by fastforce
nipkow@63411
   444
nipkow@63411
   445
lemma RB_minimal: "RB (height t') t \<Longrightarrow> invh t \<Longrightarrow> invc t' \<Longrightarrow> invh t' \<Longrightarrow> size t \<le> size t'"
nipkow@63411
   446
proof (induction "(height t')" t arbitrary: t' rule: weak_RB_induct)
nipkow@63411
   447
  case 1 thus ?case by auto 
nipkow@63411
   448
next
nipkow@63411
   449
  case (2 h t t'')
nipkow@63411
   450
  have ***: "size (Node c t'' () t) \<le> size t'"
nipkow@63411
   451
    if assms:
nipkow@63411
   452
      "\<And> (t' :: 'a rbt) . \<lbrakk> h = height t'; invh t''; invc t'; invh t' \<rbrakk>
nipkow@63411
   453
                            \<Longrightarrow> size t'' \<le> size t'"
nipkow@63411
   454
      "Suc h = height t'" "balB (h div 2) t" "RB h t''"
nipkow@63411
   455
      "invc t'" "invh t'" "height l \<ge> height r"
nipkow@63411
   456
      and tt[simp]:"t' = Node c l a r" and last: "invh (Node c t'' () t)"
nipkow@63411
   457
  for t' :: "'a rbt" and c l a r
nipkow@63411
   458
  proof -
nipkow@63411
   459
    from assms have inv: "invc r" "invh r" by auto
nipkow@63411
   460
    from assms have "height l = h" using max_def by auto
nipkow@63411
   461
    with RB_bheight_minimal[of l t''] have
nipkow@63411
   462
      "bheight t \<le> bheight r" using assms last by auto
nipkow@63411
   463
    with compact_pt[OF inv] balB_h balB_hs have 
nipkow@63411
   464
      "size t \<le> size r" using assms(3) by auto moreover
nipkow@63411
   465
    have "size t'' \<le> size l" using assms last by auto ultimately
nipkow@63411
   466
    show ?thesis by simp
nipkow@63411
   467
  qed
nipkow@63411
   468
  
nipkow@63411
   469
  from 2 obtain c l a r where 
nipkow@63411
   470
    t': "t' = Node c l a r" by (cases t') auto
nipkow@63411
   471
  with 2 have inv: "invc l" "invh l" "invc r" "invh r" by auto
nipkow@63411
   472
  show ?case proof (cases "height r \<le> height l")
nipkow@63411
   473
    case True thus ?thesis using ***[OF 2(3,4,1,2,6,7)] t' 2(5) by auto
nipkow@63411
   474
  next
nipkow@63411
   475
    case False 
nipkow@63411
   476
    obtain t''' where t''' : "t''' = Node c r a l" "invc t'''" "invh t'''" using 2 t' by auto
nipkow@63411
   477
    have "size t''' = size t'" and 4 : "Suc h = height t'''" using 2(4) t' t''' by auto
nipkow@63411
   478
    thus ?thesis using ***[OF 2(3) 4 2(1,2) t'''(2,3) _ t'''(1)] 2(5) False by auto
nipkow@63411
   479
  qed
nipkow@63411
   480
qed
nipkow@63411
   481
nipkow@63411
   482
lemma RB_size: "RB h t \<Longrightarrow> size1 t + 1 = 2^((h+1) div 2) + 2^(h div 2)"
nipkow@63411
   483
by (induction h t rule: "RB.induct" ) auto
nipkow@63411
   484
nipkow@63411
   485
lemma RB_exist: "\<exists> t . RB h t"
nipkow@63411
   486
proof (induction h) 
nipkow@63411
   487
  case (Suc n)
nipkow@63411
   488
  obtain r where r: "balB (n div 2) r"  using  exist_pt by blast
nipkow@63411
   489
  obtain l where l: "RB n l"  using  Suc by blast
nipkow@63411
   490
  obtain t where 
nipkow@63411
   491
    "color l = Red   \<Longrightarrow> t = B l () r"
nipkow@63411
   492
    "color l = Black \<Longrightarrow> t = R l () r" by auto
nipkow@63411
   493
  with l and r have "RB (Suc n) t" by (cases "color l") auto
nipkow@63411
   494
  thus ?case by auto
nipkow@63411
   495
qed auto
nipkow@63411
   496
nipkow@63411
   497
lemma bound:
nipkow@63411
   498
  assumes "invc t"  "invh t" and [simp]:"height t = h"
nipkow@63411
   499
  shows "size t \<ge> 2^((h+1) div 2) + 2^(h div 2) - 2"
nipkow@63411
   500
proof -
nipkow@63411
   501
  obtain t' where t': "RB h t'" using RB_exist by auto
nipkow@63411
   502
  show ?thesis using RB_size[OF t'] 
nipkow@63411
   503
  RB_minimal[OF _ _ assms(1,2), simplified, OF t' RB_invh[OF t']] assms t' 
nipkow@63411
   504
  unfolding  size1_def by auto
nipkow@63411
   505
qed
nipkow@63411
   506
nipkow@63411
   507
corollary "rbt t \<Longrightarrow> h = height t \<Longrightarrow> size t \<ge> 2^((h+1) div 2) + 2^(h div 2) - 2"
nipkow@63411
   508
using bound unfolding rbt_def by blast
nipkow@63411
   509
nipkow@61224
   510
end