src/HOL/Data_Structures/RBT_Set.thy
author haftmann
Mon, 06 Feb 2017 20:56:34 +0100
changeset 64990 c6a7de505796
parent 64960 8be78855ee7a
child 66087 6e0c330f4051
permissions -rw-r--r--
more explicit errors in pathological cases
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64951
140addd19343 removed contribution by Daniel Stuewe, too detailed.
nipkow
parents: 64950
diff changeset
     1
(* Author: Tobias Nipkow *)
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     2
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     3
section \<open>Red-Black Tree Implementation of Sets\<close>
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     4
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     5
theory RBT_Set
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     6
imports
64950
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
     7
  Complex_Main
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
     8
  RBT
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
     9
  Cmp
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    10
  Isin2
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    11
begin
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    12
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
    13
fun ins :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    14
"ins x Leaf = R Leaf x Leaf" |
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    15
"ins x (B l a r) =
61678
b594e9277be3 tuned white space
nipkow
parents: 61588
diff changeset
    16
  (case cmp x a of
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    17
     LT \<Rightarrow> baliL (ins x l) a r |
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    18
     GT \<Rightarrow> baliR l a (ins x r) |
61678
b594e9277be3 tuned white space
nipkow
parents: 61588
diff changeset
    19
     EQ \<Rightarrow> B l a r)" |
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    20
"ins x (R l a r) =
61678
b594e9277be3 tuned white space
nipkow
parents: 61588
diff changeset
    21
  (case cmp x a of
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    22
    LT \<Rightarrow> R (ins x l) a r |
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    23
    GT \<Rightarrow> R l a (ins x r) |
61678
b594e9277be3 tuned white space
nipkow
parents: 61588
diff changeset
    24
    EQ \<Rightarrow> R l a r)"
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    25
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
    26
definition insert :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    27
"insert x t = paint Black (ins x t)"
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    28
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
    29
fun del :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
    30
and delL :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
    31
and delR :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt"
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    32
where
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    33
"del x Leaf = Leaf" |
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    34
"del x (Node _ l a r) =
61678
b594e9277be3 tuned white space
nipkow
parents: 61588
diff changeset
    35
  (case cmp x a of
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    36
     LT \<Rightarrow> delL x l a r |
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    37
     GT \<Rightarrow> delR x l a r |
61678
b594e9277be3 tuned white space
nipkow
parents: 61588
diff changeset
    38
     EQ \<Rightarrow> combine l r)" |
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    39
"delL x (B t1 a t2) b t3 = baldL (del x (B t1 a t2)) b t3" |
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    40
"delL x l a r = R (del x l) a r" |
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    41
"delR x t1 a (B t2 b t3) = baldR t1 a (del x (B t2 b t3))" | 
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    42
"delR x l a r = R l a (del x r)"
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    43
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
    44
definition delete :: "'a::linorder \<Rightarrow> 'a rbt \<Rightarrow> 'a rbt" where
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    45
"delete x t = paint Black (del x t)"
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    46
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    47
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    48
subsection "Functional Correctness Proofs"
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    49
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    50
lemma inorder_paint: "inorder(paint c t) = inorder t"
62526
nipkow
parents: 61754
diff changeset
    51
by(cases t) (auto)
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    52
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    53
lemma inorder_baliL:
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    54
  "inorder(baliL l a r) = inorder l @ a # inorder r"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    55
by(cases "(l,a,r)" rule: baliL.cases) (auto)
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    56
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    57
lemma inorder_baliR:
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    58
  "inorder(baliR l a r) = inorder l @ a # inorder r"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    59
by(cases "(l,a,r)" rule: baliR.cases) (auto)
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    60
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    61
lemma inorder_ins:
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    62
  "sorted(inorder t) \<Longrightarrow> inorder(ins x t) = ins_list x (inorder t)"
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    63
by(induction x t rule: ins.induct)
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    64
  (auto simp: ins_list_simps inorder_baliL inorder_baliR)
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    65
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    66
lemma inorder_insert:
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    67
  "sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)"
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    68
by (simp add: insert_def inorder_ins inorder_paint)
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    69
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    70
lemma inorder_baldL:
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    71
  "inorder(baldL l a r) = inorder l @ a # inorder r"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    72
by(cases "(l,a,r)" rule: baldL.cases)
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    73
  (auto simp:  inorder_baliL inorder_baliR inorder_paint)
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    74
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    75
lemma inorder_baldR:
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    76
  "inorder(baldR l a r) = inorder l @ a # inorder r"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    77
by(cases "(l,a,r)" rule: baldR.cases)
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    78
  (auto simp:  inorder_baliL inorder_baliR inorder_paint)
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    79
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    80
lemma inorder_combine:
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    81
  "inorder(combine l r) = inorder l @ inorder r"
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    82
by(induction l r rule: combine.induct)
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    83
  (auto simp: inorder_baldL inorder_baldR split: tree.split color.split)
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    84
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    85
lemma inorder_del:
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    86
 "sorted(inorder t) \<Longrightarrow>  inorder(del x t) = del_list x (inorder t)"
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    87
 "sorted(inorder l) \<Longrightarrow>  inorder(delL x l a r) =
61678
b594e9277be3 tuned white space
nipkow
parents: 61588
diff changeset
    88
    del_list x (inorder l) @ a # inorder r"
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    89
 "sorted(inorder r) \<Longrightarrow>  inorder(delR x l a r) =
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    90
    inorder l @ a # del_list x (inorder r)"
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    91
by(induction x t and x l a r and x l a r rule: del_delL_delR.induct)
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
    92
  (auto simp: del_list_simps inorder_combine inorder_baldL inorder_baldR)
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
    93
61749
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    94
lemma inorder_delete:
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    95
  "sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    96
by (auto simp: delete_def inorder_del inorder_paint)
7f530d7e552d paint root black after insert and delete
nipkow
parents: 61678
diff changeset
    97
61581
00d9682e8dd7 Convertd to 3-way comparisons
nipkow
parents: 61428
diff changeset
    98
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
    99
subsection \<open>Structural invariants\<close>
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
   100
64952
f11e974b47e0 removed unclear clause; slower but clearer
nipkow
parents: 64951
diff changeset
   101
text\<open>The proofs are due to Markus Reiter and Alexander Krauss.\<close>
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   102
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   103
fun color :: "'a rbt \<Rightarrow> color" where
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   104
"color Leaf = Black" |
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   105
"color (Node c _ _ _) = c"
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   106
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   107
fun bheight :: "'a rbt \<Rightarrow> nat" where
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   108
"bheight Leaf = 0" |
64951
140addd19343 removed contribution by Daniel Stuewe, too detailed.
nipkow
parents: 64950
diff changeset
   109
"bheight (Node c l x r) = (if c = Black then bheight l + 1 else bheight l)"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   110
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   111
fun invc :: "'a rbt \<Rightarrow> bool" where
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   112
"invc Leaf = True" |
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   113
"invc (Node c l a r) =
64947
nipkow
parents: 64242
diff changeset
   114
  (invc l \<and> invc r \<and> (c = Red \<longrightarrow> color l = Black \<and> color r = Black))"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   115
64953
f9cfb10761ff tuned name
nipkow
parents: 64952
diff changeset
   116
fun invc2 :: "'a rbt \<Rightarrow> bool" \<comment> \<open>Weaker version\<close> where
f9cfb10761ff tuned name
nipkow
parents: 64952
diff changeset
   117
"invc2 Leaf = True" |
f9cfb10761ff tuned name
nipkow
parents: 64952
diff changeset
   118
"invc2 (Node c l a r) = (invc l \<and> invc r)"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   119
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   120
fun invh :: "'a rbt \<Rightarrow> bool" where
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   121
"invh Leaf = True" |
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   122
"invh (Node c l x r) = (invh l \<and> invh r \<and> bheight l = bheight r)"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   123
64953
f9cfb10761ff tuned name
nipkow
parents: 64952
diff changeset
   124
lemma invc2I: "invc t \<Longrightarrow> invc2 t"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   125
by (cases t) simp+
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   126
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   127
definition rbt :: "'a rbt \<Rightarrow> bool" where
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   128
"rbt t = (invc t \<and> invh t \<and> color t = Black)"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   129
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   130
lemma color_paint_Black: "color (paint Black t) = Black"
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   131
by (cases t) auto
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   132
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   133
theorem rbt_Leaf: "rbt Leaf"
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   134
by (simp add: rbt_def)
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   135
64953
f9cfb10761ff tuned name
nipkow
parents: 64952
diff changeset
   136
lemma paint_invc2: "invc2 t \<Longrightarrow> invc2 (paint c t)"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   137
by (cases t) auto
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   138
64953
f9cfb10761ff tuned name
nipkow
parents: 64952
diff changeset
   139
lemma invc_paint_Black: "invc2 t \<Longrightarrow> invc (paint Black t)"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   140
by (cases t) auto
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   141
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   142
lemma invh_paint: "invh t \<Longrightarrow> invh (paint c t)"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   143
by (cases t) auto
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   144
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   145
lemma invc_baliL:
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   146
  "\<lbrakk>invc2 l; invc r\<rbrakk> \<Longrightarrow> invc (baliL l a r)" 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   147
by (induct l a r rule: baliL.induct) auto
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   148
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   149
lemma invc_baliR:
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   150
  "\<lbrakk>invc l; invc2 r\<rbrakk> \<Longrightarrow> invc (baliR l a r)" 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   151
by (induct l a r rule: baliR.induct) auto
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   152
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   153
lemma bheight_baliL:
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   154
  "bheight l = bheight r \<Longrightarrow> bheight (baliL l a r) = Suc (bheight l)"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   155
by (induct l a r rule: baliL.induct) auto
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   156
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   157
lemma bheight_baliR:
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   158
  "bheight l = bheight r \<Longrightarrow> bheight (baliR l a r) = Suc (bheight l)"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   159
by (induct l a r rule: baliR.induct) auto
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   160
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   161
lemma invh_baliL: 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   162
  "\<lbrakk> invh l; invh r; bheight l = bheight r \<rbrakk> \<Longrightarrow> invh (baliL l a r)"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   163
by (induct l a r rule: baliL.induct) auto
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   164
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   165
lemma invh_baliR: 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   166
  "\<lbrakk> invh l; invh r; bheight l = bheight r \<rbrakk> \<Longrightarrow> invh (baliR l a r)"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   167
by (induct l a r rule: baliR.induct) auto
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   168
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   169
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   170
subsubsection \<open>Insertion\<close>
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   171
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   172
lemma invc_ins: assumes "invc t"
64953
f9cfb10761ff tuned name
nipkow
parents: 64952
diff changeset
   173
  shows "color t = Black \<Longrightarrow> invc (ins x t)" "invc2 (ins x t)"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   174
using assms
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   175
by (induct x t rule: ins.induct) (auto simp: invc_baliL invc_baliR invc2I)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   176
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   177
lemma invh_ins: assumes "invh t"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   178
  shows "invh (ins x t)" "bheight (ins x t) = bheight t"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   179
using assms
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   180
by(induct x t rule: ins.induct)
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   181
  (auto simp: invh_baliL invh_baliR bheight_baliL bheight_baliR)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   182
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   183
theorem rbt_insert: "rbt t \<Longrightarrow> rbt (insert x t)"
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   184
by (simp add: invc_ins invh_ins color_paint_Black invc_paint_Black invh_paint
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   185
  rbt_def insert_def)
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   186
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   187
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   188
subsubsection \<open>Deletion\<close>
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   189
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   190
lemma bheight_paint_Red:
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   191
  "color t = Black \<Longrightarrow> bheight (paint Red t) = bheight t - 1"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   192
by (cases t) auto
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   193
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   194
lemma baldL_invh_with_invc:
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   195
  assumes "invh l" "invh r" "bheight l + 1 = bheight r" "invc r"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   196
  shows "bheight (baldL l a r) = bheight l + 1"  "invh (baldL l a r)"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   197
using assms 
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   198
by (induct l a r rule: baldL.induct)
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   199
   (auto simp: invh_baliR invh_paint bheight_baliR bheight_paint_Red)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   200
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   201
lemma baldL_invh_app: 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   202
  assumes "invh l" "invh r" "bheight l + 1 = bheight r" "color r = Black"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   203
  shows "invh (baldL l a r)" 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   204
        "bheight (baldL l a r) = bheight r"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   205
using assms 
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   206
by (induct l a r rule: baldL.induct) (auto simp add: invh_baliR bheight_baliR) 
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   207
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   208
lemma baldL_invc: "\<lbrakk>invc2 l; invc r; color r = Black\<rbrakk> \<Longrightarrow> invc (baldL l a r)"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   209
by (induct l a r rule: baldL.induct) (simp_all add: invc_baliR)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   210
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   211
lemma baldL_invc2: "\<lbrakk> invc2 l; invc r \<rbrakk> \<Longrightarrow> invc2 (baldL l a r)"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   212
by (induct l a r rule: baldL.induct) (auto simp: invc_baliR paint_invc2 invc2I)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   213
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   214
lemma baldR_invh_with_invc:
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   215
  assumes "invh l" "invh r" "bheight l = bheight r + 1" "invc l"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   216
  shows "invh (baldR l a r) \<and> bheight (baldR l a r) = bheight l"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   217
using assms
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   218
by(induct l a r rule: baldR.induct)
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   219
  (auto simp: invh_baliL bheight_baliL invh_paint bheight_paint_Red)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   220
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   221
lemma invc_baldR: "\<lbrakk>invc a; invc2 b; color a = Black\<rbrakk> \<Longrightarrow> invc (baldR a x b)"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   222
by (induct a x b rule: baldR.induct) (simp_all add: invc_baliL)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   223
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   224
lemma invc2_baldR: "\<lbrakk> invc l; invc2 r \<rbrakk> \<Longrightarrow>invc2 (baldR l x r)"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   225
by (induct l x r rule: baldR.induct) (auto simp: invc_baliL paint_invc2 invc2I)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   226
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   227
lemma invh_combine:
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   228
  assumes "invh l" "invh r" "bheight l = bheight r"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   229
  shows "bheight (combine l r) = bheight l" "invh (combine l r)"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   230
using assms 
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   231
by (induct l r rule: combine.induct) 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   232
   (auto simp: baldL_invh_app split: tree.splits color.splits)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   233
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   234
lemma invc_combine: 
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   235
  assumes "invc l" "invc r"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   236
  shows "color l = Black \<Longrightarrow> color r = Black \<Longrightarrow> invc (combine l r)"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   237
         "invc2 (combine l r)"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   238
using assms 
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   239
by (induct l r rule: combine.induct)
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   240
   (auto simp: baldL_invc invc2I split: tree.splits color.splits)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   241
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   242
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   243
lemma assumes "invh l" "invc l"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   244
  shows del_invc_invh:
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   245
   "invh (del x l) \<and>
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   246
   (color l = Red \<and> bheight (del x l) = bheight l \<and> invc (del x l) \<or>
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   247
    color l = Black \<and> bheight (del x l) = bheight l - 1 \<and> invc2 (del x l))"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   248
and  "\<lbrakk>invh r; bheight l = bheight r; invc r\<rbrakk> \<Longrightarrow>
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   249
   invh (delL x l k r) \<and> 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   250
   bheight (delL x l k r) = bheight l \<and> 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   251
   (color l = Black \<and> color r = Black \<and> invc (delL x l k r) \<or> 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   252
    (color l \<noteq> Black \<or> color r \<noteq> Black) \<and> invc2 (delL x l k r))"
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   253
  and "\<lbrakk>invh r; bheight l = bheight r; invc r\<rbrakk> \<Longrightarrow>
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   254
  invh (delR x l k r) \<and> 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   255
  bheight (delR x l k r) = bheight l \<and> 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   256
  (color l = Black \<and> color r = Black \<and> invc (delR x l k r) \<or> 
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   257
   (color l \<noteq> Black \<or> color r \<noteq> Black) \<and> invc2 (delR x l k r))"
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   258
using assms
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   259
proof (induct x l and x l k r and x l k r rule: del_delL_delR.induct)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   260
case (2 y c _ y')
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   261
  have "y = y' \<or> y < y' \<or> y > y'" by auto
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   262
  thus ?case proof (elim disjE)
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   263
    assume "y = y'"
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   264
    with 2 show ?thesis
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   265
    by (cases c) (simp_all add: invh_combine invc_combine)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   266
  next
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   267
    assume "y < y'"
64953
f9cfb10761ff tuned name
nipkow
parents: 64952
diff changeset
   268
    with 2 show ?thesis by (cases c) (auto simp: invc2I)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   269
  next
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   270
    assume "y' < y"
64953
f9cfb10761ff tuned name
nipkow
parents: 64952
diff changeset
   271
    with 2 show ?thesis by (cases c) (auto simp: invc2I)
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   272
  qed
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   273
next
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   274
  case (3 y l z ra y' bb)
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   275
  thus ?case by (cases "color (Node Black l z ra) = Black \<and> color bb = Black") (simp add: baldL_invh_with_invc baldL_invc baldL_invc2)+
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   276
next
64960
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   277
  case (5 y a y' l z ra)
8be78855ee7a split balance into two, clearer etc
nipkow
parents: 64953
diff changeset
   278
  thus ?case by (cases "color a = Black \<and> color (Node Black l z ra) = Black") (simp add: baldR_invh_with_invc invc_baldR invc2_baldR)+
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   279
next
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   280
  case ("6_1" y a y') thus ?case by (cases "color a = Black \<and> color Leaf = Black") simp+
61754
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   281
qed auto
862daa8144f3 RBT invariants for insert
nipkow
parents: 61749
diff changeset
   282
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   283
theorem rbt_delete: "rbt t \<Longrightarrow> rbt (delete k t)"
64953
f9cfb10761ff tuned name
nipkow
parents: 64952
diff changeset
   284
by (metis delete_def rbt_def color_paint_Black del_invc_invh invc_paint_Black invc2I invh_paint)
63411
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   285
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   286
text \<open>Overall correctness:\<close>
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   287
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   288
interpretation Set_by_Ordered
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   289
where empty = Leaf and isin = isin and insert = insert and delete = delete
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   290
and inorder = inorder and inv = rbt
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   291
proof (standard, goal_cases)
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   292
  case 1 show ?case by simp
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   293
next
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   294
  case 2 thus ?case by(simp add: isin_set)
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   295
next
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   296
  case 3 thus ?case by(simp add: inorder_insert)
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   297
next
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   298
  case 4 thus ?case by(simp add: inorder_delete)
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   299
next
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   300
  case 5 thus ?case by (simp add: rbt_Leaf) 
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   301
next
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   302
  case 6 thus ?case by (simp add: rbt_insert) 
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   303
next
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   304
  case 7 thus ?case by (simp add: rbt_delete) 
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   305
qed
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   306
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   307
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   308
subsection \<open>Height-Size Relation\<close>
e051eea34990 got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents: 62526
diff changeset
   309
64950
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   310
lemma neq_Black[simp]: "(c \<noteq> Black) = (c = Red)"
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   311
by (cases c) auto
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   312
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   313
lemma rbt_height_bheight_if_nat: "invc t \<Longrightarrow> invh t \<Longrightarrow>
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   314
  height t \<le> (if color t = Black then 2 * bheight t else 2 * bheight t + 1)"
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   315
by(induction t) (auto split: if_split_asm)
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   316
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   317
lemma rbt_height_bheight_if: "invc t \<Longrightarrow> invh t \<Longrightarrow>
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   318
  (if color t = Black then height t / 2 else (height t - 1) / 2) \<le> bheight t"
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   319
by(induction t) (auto split: if_split_asm)
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   320
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   321
lemma rbt_height_bheight: "rbt t \<Longrightarrow> height t / 2 \<le> bheight t "
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   322
by(auto simp: rbt_def dest: rbt_height_bheight_if)
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   323
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   324
lemma bheight_size_bound:  "invc t \<Longrightarrow> invh t \<Longrightarrow> size1 t \<ge>  2 ^ (bheight t)"
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   325
by (induction t) auto
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   326
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   327
lemma rbt_height_le: assumes "rbt t" shows "height t \<le> 2 * log 2 (size1 t)"
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   328
proof -
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   329
  have "2 powr (height t / 2) \<le> 2 powr bheight t"
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   330
    using rbt_height_bheight[OF assms] by (simp)
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   331
  also have "\<dots> \<le> size1 t" using assms
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   332
    by (simp add: powr_realpow bheight_size_bound rbt_def)
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   333
  finally have "2 powr (height t / 2) \<le> size1 t" .
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   334
  hence "height t / 2 \<le> log 2 (size1 t)"
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   335
    by(simp add: le_log_iff size1_def del: Int.divide_le_eq_numeral1(1))
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   336
  thus ?thesis by simp
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   337
qed
10b8d31634cc added concise log height bound lemma
nipkow
parents: 64947
diff changeset
   338
61224
759b5299a9f2 added red black trees
nipkow
parents:
diff changeset
   339
end