src/HOL/Library/Tree.thy
author haftmann
Fri Mar 22 19:18:08 2019 +0000 (3 months ago)
changeset 69946 494934c30f38
parent 69655 2b56cbb02e8a
permissions -rw-r--r--
improved code equations taken over from AFP
nipkow@57250
     1
(* Author: Tobias Nipkow *)
nipkow@64887
     2
(* Todo: minimal ipl of balanced trees *)
nipkow@57250
     3
wenzelm@60500
     4
section \<open>Binary Tree\<close>
nipkow@57250
     5
nipkow@57250
     6
theory Tree
nipkow@57250
     7
imports Main
nipkow@57250
     8
begin
nipkow@57250
     9
nipkow@58424
    10
datatype 'a tree =
nipkow@64887
    11
  Leaf ("\<langle>\<rangle>") |
nipkow@69655
    12
  Node "'a tree" ("value": 'a) "'a tree" ("(1\<langle>_,/ _,/ _\<rangle>)")
hoelzl@57569
    13
datatype_compat tree
nipkow@57250
    14
nipkow@69218
    15
primrec left :: "'a tree \<Rightarrow> 'a tree" where
nipkow@69218
    16
"left (Node l v r) = l" |
nipkow@69218
    17
"left Leaf = Leaf"
nipkow@69218
    18
nipkow@69218
    19
primrec right :: "'a tree \<Rightarrow> 'a tree" where
nipkow@69218
    20
"right (Node l v r) = r" |
nipkow@69218
    21
"right Leaf = Leaf"
nipkow@69218
    22
nipkow@68998
    23
text\<open>Counting the number of leaves rather than nodes:\<close>
nipkow@58438
    24
nipkow@68998
    25
fun size1 :: "'a tree \<Rightarrow> nat" where
nipkow@68998
    26
"size1 \<langle>\<rangle> = 1" |
nipkow@68998
    27
"size1 \<langle>l, x, r\<rangle> = size1 l + size1 r"
nipkow@58438
    28
nipkow@63861
    29
fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where
nipkow@63861
    30
"subtrees \<langle>\<rangle> = {\<langle>\<rangle>}" |
nipkow@63861
    31
"subtrees (\<langle>l, a, r\<rangle>) = insert \<langle>l, a, r\<rangle> (subtrees l \<union> subtrees r)"
nipkow@63861
    32
nipkow@63861
    33
fun mirror :: "'a tree \<Rightarrow> 'a tree" where
nipkow@63861
    34
"mirror \<langle>\<rangle> = Leaf" |
nipkow@63861
    35
"mirror \<langle>l,x,r\<rangle> = \<langle>mirror r, x, mirror l\<rangle>"
nipkow@63861
    36
nipkow@63861
    37
class height = fixes height :: "'a \<Rightarrow> nat"
nipkow@63861
    38
nipkow@63861
    39
instantiation tree :: (type)height
nipkow@63861
    40
begin
nipkow@63861
    41
nipkow@63861
    42
fun height_tree :: "'a tree => nat" where
nipkow@63861
    43
"height Leaf = 0" |
nipkow@68999
    44
"height (Node l a r) = max (height l) (height r) + 1"
nipkow@63861
    45
nipkow@63861
    46
instance ..
nipkow@63861
    47
nipkow@63861
    48
end
nipkow@63861
    49
nipkow@63861
    50
fun min_height :: "'a tree \<Rightarrow> nat" where
nipkow@63861
    51
"min_height Leaf = 0" |
nipkow@63861
    52
"min_height (Node l _ r) = min (min_height l) (min_height r) + 1"
nipkow@63861
    53
nipkow@63861
    54
fun complete :: "'a tree \<Rightarrow> bool" where
nipkow@63861
    55
"complete Leaf = True" |
nipkow@63861
    56
"complete (Node l x r) = (complete l \<and> complete r \<and> height l = height r)"
nipkow@63861
    57
nipkow@63861
    58
definition balanced :: "'a tree \<Rightarrow> bool" where
nipkow@63861
    59
"balanced t = (height t - min_height t \<le> 1)"
nipkow@63861
    60
nipkow@63861
    61
text \<open>Weight balanced:\<close>
nipkow@63861
    62
fun wbalanced :: "'a tree \<Rightarrow> bool" where
nipkow@63861
    63
"wbalanced Leaf = True" |
nipkow@63861
    64
"wbalanced (Node l x r) = (abs(int(size l) - int(size r)) \<le> 1 \<and> wbalanced l \<and> wbalanced r)"
nipkow@63861
    65
nipkow@63861
    66
text \<open>Internal path length:\<close>
nipkow@64887
    67
fun ipl :: "'a tree \<Rightarrow> nat" where
nipkow@64887
    68
"ipl Leaf = 0 " |
nipkow@64887
    69
"ipl (Node l _ r) = ipl l + size l + ipl r + size r"
nipkow@63861
    70
nipkow@63861
    71
fun preorder :: "'a tree \<Rightarrow> 'a list" where
nipkow@63861
    72
"preorder \<langle>\<rangle> = []" |
nipkow@63861
    73
"preorder \<langle>l, x, r\<rangle> = x # preorder l @ preorder r"
nipkow@63861
    74
nipkow@63861
    75
fun inorder :: "'a tree \<Rightarrow> 'a list" where
nipkow@63861
    76
"inorder \<langle>\<rangle> = []" |
nipkow@63861
    77
"inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r"
nipkow@63861
    78
nipkow@63861
    79
text\<open>A linear version avoiding append:\<close>
nipkow@63861
    80
fun inorder2 :: "'a tree \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@63861
    81
"inorder2 \<langle>\<rangle> xs = xs" |
nipkow@63861
    82
"inorder2 \<langle>l, x, r\<rangle> xs = inorder2 l (x # inorder2 r xs)"
nipkow@63861
    83
nipkow@64925
    84
fun postorder :: "'a tree \<Rightarrow> 'a list" where
nipkow@64925
    85
"postorder \<langle>\<rangle> = []" |
nipkow@64925
    86
"postorder \<langle>l, x, r\<rangle> = postorder l @ postorder r @ [x]"
nipkow@64925
    87
nipkow@63861
    88
text\<open>Binary Search Tree:\<close>
nipkow@66606
    89
fun bst_wrt :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a tree \<Rightarrow> bool" where
nipkow@66606
    90
"bst_wrt P \<langle>\<rangle> \<longleftrightarrow> True" |
nipkow@66606
    91
"bst_wrt P \<langle>l, a, r\<rangle> \<longleftrightarrow>
nipkow@66606
    92
 bst_wrt P l \<and> bst_wrt P r \<and> (\<forall>x\<in>set_tree l. P x a) \<and> (\<forall>x\<in>set_tree r. P a x)"
nipkow@63861
    93
nipkow@66606
    94
abbreviation bst :: "('a::linorder) tree \<Rightarrow> bool" where
nipkow@67399
    95
"bst \<equiv> bst_wrt (<)"
nipkow@63861
    96
nipkow@63861
    97
fun (in linorder) heap :: "'a tree \<Rightarrow> bool" where
nipkow@63861
    98
"heap Leaf = True" |
nipkow@63861
    99
"heap (Node l m r) =
nipkow@63861
   100
  (heap l \<and> heap r \<and> (\<forall>x \<in> set_tree l \<union> set_tree r. m \<le> x))"
nipkow@63861
   101
nipkow@63861
   102
wenzelm@69593
   103
subsection \<open>\<^const>\<open>map_tree\<close>\<close>
nipkow@65339
   104
nipkow@65340
   105
lemma eq_map_tree_Leaf[simp]: "map_tree f t = Leaf \<longleftrightarrow> t = Leaf"
nipkow@65339
   106
by (rule tree.map_disc_iff)
nipkow@65339
   107
nipkow@65340
   108
lemma eq_Leaf_map_tree[simp]: "Leaf = map_tree f t \<longleftrightarrow> t = Leaf"
nipkow@65339
   109
by (cases t) auto
nipkow@65339
   110
nipkow@65339
   111
wenzelm@69593
   112
subsection \<open>\<^const>\<open>size\<close>\<close>
nipkow@63861
   113
nipkow@68998
   114
lemma size1_size: "size1 t = size t + 1"
nipkow@68998
   115
by (induction t) simp_all
nipkow@58438
   116
nipkow@62650
   117
lemma size1_ge0[simp]: "0 < size1 t"
nipkow@68998
   118
by (simp add: size1_size)
nipkow@62650
   119
nipkow@65340
   120
lemma eq_size_0[simp]: "size t = 0 \<longleftrightarrow> t = Leaf"
nipkow@65339
   121
by(cases t) auto
nipkow@65339
   122
nipkow@65340
   123
lemma eq_0_size[simp]: "0 = size t \<longleftrightarrow> t = Leaf"
nipkow@60505
   124
by(cases t) auto
nipkow@60505
   125
nipkow@58424
   126
lemma neq_Leaf_iff: "(t \<noteq> \<langle>\<rangle>) = (\<exists>l a r. t = \<langle>l, a, r\<rangle>)"
nipkow@58424
   127
by (cases t) auto
nipkow@57530
   128
nipkow@59776
   129
lemma size_map_tree[simp]: "size (map_tree f t) = size t"
nipkow@59776
   130
by (induction t) auto
nipkow@59776
   131
nipkow@59776
   132
lemma size1_map_tree[simp]: "size1 (map_tree f t) = size1 t"
nipkow@68998
   133
by (simp add: size1_size)
nipkow@59776
   134
nipkow@59776
   135
wenzelm@69593
   136
subsection \<open>\<^const>\<open>set_tree\<close>\<close>
nipkow@65339
   137
nipkow@65340
   138
lemma eq_set_tree_empty[simp]: "set_tree t = {} \<longleftrightarrow> t = Leaf"
nipkow@65339
   139
by (cases t) auto
nipkow@65339
   140
nipkow@65340
   141
lemma eq_empty_set_tree[simp]: "{} = set_tree t \<longleftrightarrow> t = Leaf"
nipkow@65339
   142
by (cases t) auto
nipkow@65339
   143
nipkow@65339
   144
lemma finite_set_tree[simp]: "finite(set_tree t)"
nipkow@65339
   145
by(induction t) auto
nipkow@65339
   146
nipkow@65339
   147
wenzelm@69593
   148
subsection \<open>\<^const>\<open>subtrees\<close>\<close>
nipkow@60808
   149
nipkow@65340
   150
lemma neq_subtrees_empty[simp]: "subtrees t \<noteq> {}"
nipkow@65340
   151
by (cases t)(auto)
nipkow@65340
   152
nipkow@65340
   153
lemma neq_empty_subtrees[simp]: "{} \<noteq> subtrees t"
nipkow@65340
   154
by (cases t)(auto)
nipkow@65340
   155
nipkow@63861
   156
lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. \<langle>l, a, r\<rangle> \<in> subtrees t"
nipkow@63861
   157
by (induction t)(auto)
nipkow@59776
   158
nipkow@63861
   159
lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t"
nipkow@63861
   160
by (induction t) auto
nipkow@59776
   161
nipkow@63861
   162
lemma in_set_tree_if: "\<langle>l, a, r\<rangle> \<in> subtrees t \<Longrightarrow> a \<in> set_tree t"
nipkow@63861
   163
by (metis Node_notin_subtrees_if)
nipkow@60808
   164
nipkow@63861
   165
wenzelm@69593
   166
subsection \<open>\<^const>\<open>height\<close> and \<^const>\<open>min_height\<close>\<close>
nipkow@60808
   167
nipkow@65340
   168
lemma eq_height_0[simp]: "height t = 0 \<longleftrightarrow> t = Leaf"
nipkow@65339
   169
by(cases t) auto
nipkow@65339
   170
nipkow@65340
   171
lemma eq_0_height[simp]: "0 = height t \<longleftrightarrow> t = Leaf"
nipkow@63665
   172
by(cases t) auto
nipkow@63665
   173
nipkow@60808
   174
lemma height_map_tree[simp]: "height (map_tree f t) = height t"
nipkow@59776
   175
by (induction t) auto
nipkow@59776
   176
nipkow@64414
   177
lemma height_le_size_tree: "height t \<le> size (t::'a tree)"
nipkow@64414
   178
by (induction t) auto
nipkow@64414
   179
nipkow@64533
   180
lemma size1_height: "size1 t \<le> 2 ^ height (t::'a tree)"
nipkow@62202
   181
proof(induction t)
nipkow@62202
   182
  case (Node l a r)
nipkow@62202
   183
  show ?case
nipkow@62202
   184
  proof (cases "height l \<le> height r")
nipkow@62202
   185
    case True
nipkow@64533
   186
    have "size1(Node l a r) = size1 l + size1 r" by simp
nipkow@64918
   187
    also have "\<dots> \<le> 2 ^ height l + 2 ^ height r" using Node.IH by arith
nipkow@64918
   188
    also have "\<dots> \<le> 2 ^ height r + 2 ^ height r" using True by simp
nipkow@64922
   189
    also have "\<dots> = 2 ^ height (Node l a r)"
nipkow@64918
   190
      using True by (auto simp: max_def mult_2)
nipkow@64918
   191
    finally show ?thesis .
nipkow@62202
   192
  next
nipkow@62202
   193
    case False
nipkow@64533
   194
    have "size1(Node l a r) = size1 l + size1 r" by simp
nipkow@64918
   195
    also have "\<dots> \<le> 2 ^ height l + 2 ^ height r" using Node.IH by arith
nipkow@64918
   196
    also have "\<dots> \<le> 2 ^ height l + 2 ^ height l" using False by simp
nipkow@62202
   197
    finally show ?thesis using False by (auto simp: max_def mult_2)
nipkow@62202
   198
  qed
nipkow@62202
   199
qed simp
nipkow@62202
   200
nipkow@63755
   201
corollary size_height: "size t \<le> 2 ^ height (t::'a tree) - 1"
nipkow@68998
   202
using size1_height[of t, unfolded size1_size] by(arith)
nipkow@63755
   203
nipkow@63861
   204
lemma height_subtrees: "s \<in> subtrees t \<Longrightarrow> height s \<le> height t"
nipkow@63861
   205
by (induction t) auto
nipkow@57687
   206
nipkow@63598
   207
nipkow@64540
   208
lemma min_height_le_height: "min_height t \<le> height t"
nipkow@63598
   209
by(induction t) auto
nipkow@63598
   210
nipkow@63598
   211
lemma min_height_map_tree[simp]: "min_height (map_tree f t) = min_height t"
nipkow@63598
   212
by (induction t) auto
nipkow@63598
   213
nipkow@64533
   214
lemma min_height_size1: "2 ^ min_height t \<le> size1 t"
nipkow@63598
   215
proof(induction t)
nipkow@63598
   216
  case (Node l a r)
nipkow@63598
   217
  have "(2::nat) ^ min_height (Node l a r) \<le> 2 ^ min_height l + 2 ^ min_height r"
nipkow@63598
   218
    by (simp add: min_def)
nipkow@64533
   219
  also have "\<dots> \<le> size1(Node l a r)" using Node.IH by simp
nipkow@63598
   220
  finally show ?case .
nipkow@63598
   221
qed simp
nipkow@63598
   222
nipkow@63598
   223
wenzelm@69593
   224
subsection \<open>\<^const>\<open>complete\<close>\<close>
nipkow@63036
   225
nipkow@63755
   226
lemma complete_iff_height: "complete t \<longleftrightarrow> (min_height t = height t)"
nipkow@63598
   227
apply(induction t)
nipkow@63598
   228
 apply simp
nipkow@63598
   229
apply (simp add: min_def max_def)
nipkow@64540
   230
by (metis le_antisym le_trans min_height_le_height)
nipkow@63598
   231
nipkow@63770
   232
lemma size1_if_complete: "complete t \<Longrightarrow> size1 t = 2 ^ height t"
nipkow@63036
   233
by (induction t) auto
nipkow@63036
   234
nipkow@63755
   235
lemma size_if_complete: "complete t \<Longrightarrow> size t = 2 ^ height t - 1"
nipkow@68998
   236
using size1_if_complete[simplified size1_size] by fastforce
nipkow@63770
   237
nipkow@69117
   238
lemma size1_height_if_incomplete:
nipkow@69117
   239
  "\<not> complete t \<Longrightarrow> size1 t < 2 ^ height t"
nipkow@69117
   240
proof(induction t)
nipkow@69117
   241
  case Leaf thus ?case by simp
nipkow@63770
   242
next
nipkow@69117
   243
  case (Node l x r)
nipkow@69117
   244
  have 1: ?case if h: "height l < height r"
nipkow@69117
   245
    using h size1_height[of l] size1_height[of r] power_strict_increasing[OF h, of "2::nat"]
nipkow@69117
   246
    by(auto simp: max_def simp del: power_strict_increasing_iff)
nipkow@69117
   247
  have 2: ?case if h: "height l > height r"
nipkow@69117
   248
    using h size1_height[of l] size1_height[of r] power_strict_increasing[OF h, of "2::nat"]
nipkow@69117
   249
    by(auto simp: max_def simp del: power_strict_increasing_iff)
nipkow@69117
   250
  have 3: ?case if h: "height l = height r" and c: "\<not> complete l"
nipkow@69117
   251
    using h size1_height[of r] Node.IH(1)[OF c] by(simp)
nipkow@69117
   252
  have 4: ?case if h: "height l = height r" and c: "\<not> complete r"
nipkow@69117
   253
    using h size1_height[of l] Node.IH(2)[OF c] by(simp)
nipkow@69117
   254
  from 1 2 3 4 Node.prems show ?case apply (simp add: max_def) by linarith
nipkow@63770
   255
qed
nipkow@63770
   256
nipkow@69117
   257
lemma complete_iff_min_height: "complete t \<longleftrightarrow> (height t = min_height t)"
nipkow@69117
   258
by(auto simp add: complete_iff_height)
nipkow@69117
   259
nipkow@69117
   260
lemma min_height_size1_if_incomplete:
nipkow@69117
   261
  "\<not> complete t \<Longrightarrow> 2 ^ min_height t < size1 t"
nipkow@69117
   262
proof(induction t)
nipkow@69117
   263
  case Leaf thus ?case by simp
nipkow@69117
   264
next
nipkow@69117
   265
  case (Node l x r)
nipkow@69117
   266
  have 1: ?case if h: "min_height l < min_height r"
nipkow@69117
   267
    using h min_height_size1[of l] min_height_size1[of r] power_strict_increasing[OF h, of "2::nat"]
nipkow@69117
   268
    by(auto simp: max_def simp del: power_strict_increasing_iff)
nipkow@69117
   269
  have 2: ?case if h: "min_height l > min_height r"
nipkow@69117
   270
    using h min_height_size1[of l] min_height_size1[of r] power_strict_increasing[OF h, of "2::nat"]
nipkow@69117
   271
    by(auto simp: max_def simp del: power_strict_increasing_iff)
nipkow@69117
   272
  have 3: ?case if h: "min_height l = min_height r" and c: "\<not> complete l"
nipkow@69117
   273
    using h min_height_size1[of r] Node.IH(1)[OF c] by(simp add: complete_iff_min_height)
nipkow@69117
   274
  have 4: ?case if h: "min_height l = min_height r" and c: "\<not> complete r"
nipkow@69117
   275
    using h min_height_size1[of l] Node.IH(2)[OF c] by(simp add: complete_iff_min_height)
nipkow@69117
   276
  from 1 2 3 4 Node.prems show ?case
nipkow@69117
   277
    by (fastforce simp: complete_iff_min_height[THEN iffD1])
nipkow@69117
   278
qed
nipkow@69117
   279
nipkow@69117
   280
lemma complete_if_size1_height: "size1 t = 2 ^ height t \<Longrightarrow> complete t"
nipkow@69117
   281
using  size1_height_if_incomplete by fastforce
nipkow@63755
   282
nipkow@64533
   283
lemma complete_if_size1_min_height: "size1 t = 2 ^ min_height t \<Longrightarrow> complete t"
nipkow@69117
   284
using min_height_size1_if_incomplete by fastforce
nipkow@63755
   285
nipkow@64533
   286
lemma complete_iff_size1: "complete t \<longleftrightarrow> size1 t = 2 ^ height t"
nipkow@64533
   287
using complete_if_size1_height size1_if_complete by blast
nipkow@64533
   288
nipkow@63755
   289
wenzelm@69593
   290
subsection \<open>\<^const>\<open>balanced\<close>\<close>
nipkow@63861
   291
nipkow@63861
   292
lemma balanced_subtreeL: "balanced (Node l x r) \<Longrightarrow> balanced l"
nipkow@63861
   293
by(simp add: balanced_def)
nipkow@63755
   294
nipkow@63861
   295
lemma balanced_subtreeR: "balanced (Node l x r) \<Longrightarrow> balanced r"
nipkow@63861
   296
by(simp add: balanced_def)
nipkow@63861
   297
nipkow@63861
   298
lemma balanced_subtrees: "\<lbrakk> balanced t; s \<in> subtrees t \<rbrakk> \<Longrightarrow> balanced s"
nipkow@63861
   299
using [[simp_depth_limit=1]]
nipkow@63861
   300
by(induction t arbitrary: s)
nipkow@63861
   301
  (auto simp add: balanced_subtreeL balanced_subtreeR)
nipkow@63755
   302
nipkow@63755
   303
text\<open>Balanced trees have optimal height:\<close>
nipkow@63755
   304
nipkow@63755
   305
lemma balanced_optimal:
nipkow@63755
   306
fixes t :: "'a tree" and t' :: "'b tree"
nipkow@63755
   307
assumes "balanced t" "size t \<le> size t'" shows "height t \<le> height t'"
nipkow@63755
   308
proof (cases "complete t")
nipkow@63755
   309
  case True
nipkow@64924
   310
  have "(2::nat) ^ height t \<le> 2 ^ height t'"
nipkow@63755
   311
  proof -
nipkow@64924
   312
    have "2 ^ height t = size1 t"
nipkow@69115
   313
      using True by (simp add: size1_if_complete)
nipkow@68998
   314
    also have "\<dots> \<le> size1 t'" using assms(2) by(simp add: size1_size)
nipkow@64924
   315
    also have "\<dots> \<le> 2 ^ height t'" by (rule size1_height)
nipkow@63755
   316
    finally show ?thesis .
nipkow@63755
   317
  qed
nipkow@64924
   318
  thus ?thesis by (simp)
nipkow@63755
   319
next
nipkow@63755
   320
  case False
nipkow@63755
   321
  have "(2::nat) ^ min_height t < 2 ^ height t'"
nipkow@63755
   322
  proof -
nipkow@64533
   323
    have "(2::nat) ^ min_height t < size1 t"
nipkow@64533
   324
      by(rule min_height_size1_if_incomplete[OF False])
nipkow@68998
   325
    also have "\<dots> \<le> size1 t'" using assms(2) by (simp add: size1_size)
nipkow@64918
   326
    also have "\<dots> \<le> 2 ^ height t'"  by(rule size1_height)
nipkow@64918
   327
    finally have "(2::nat) ^ min_height t < (2::nat) ^ height t'" .
nipkow@64924
   328
    thus ?thesis .
nipkow@63755
   329
  qed
nipkow@63755
   330
  hence *: "min_height t < height t'" by simp
nipkow@63755
   331
  have "min_height t + 1 = height t"
nipkow@64540
   332
    using min_height_le_height[of t] assms(1) False
nipkow@63829
   333
    by (simp add: complete_iff_height balanced_def)
nipkow@63755
   334
  with * show ?thesis by arith
nipkow@63755
   335
qed
nipkow@63036
   336
nipkow@63036
   337
wenzelm@69593
   338
subsection \<open>\<^const>\<open>wbalanced\<close>\<close>
nipkow@63861
   339
nipkow@63861
   340
lemma wbalanced_subtrees: "\<lbrakk> wbalanced t; s \<in> subtrees t \<rbrakk> \<Longrightarrow> wbalanced s"
nipkow@63861
   341
using [[simp_depth_limit=1]] by(induction t arbitrary: s) auto
nipkow@63861
   342
nipkow@63861
   343
wenzelm@69593
   344
subsection \<open>\<^const>\<open>ipl\<close>\<close>
nipkow@63413
   345
nipkow@63413
   346
text \<open>The internal path length of a tree:\<close>
nipkow@63413
   347
nipkow@64923
   348
lemma ipl_if_complete_int:
nipkow@64923
   349
  "complete t \<Longrightarrow> int(ipl t) = (int(height t) - 2) * 2^(height t) + 2"
nipkow@64923
   350
apply(induction t)
nipkow@64923
   351
 apply simp
nipkow@64923
   352
apply simp
nipkow@64923
   353
apply (simp add: algebra_simps size_if_complete of_nat_diff)
nipkow@64923
   354
done
nipkow@63413
   355
nipkow@63413
   356
nipkow@59776
   357
subsection "List of entries"
nipkow@59776
   358
nipkow@65340
   359
lemma eq_inorder_Nil[simp]: "inorder t = [] \<longleftrightarrow> t = Leaf"
nipkow@65339
   360
by (cases t) auto
nipkow@65339
   361
nipkow@65340
   362
lemma eq_Nil_inorder[simp]: "[] = inorder t \<longleftrightarrow> t = Leaf"
nipkow@65339
   363
by (cases t) auto
nipkow@65339
   364
hoelzl@57449
   365
lemma set_inorder[simp]: "set (inorder t) = set_tree t"
nipkow@58424
   366
by (induction t) auto
nipkow@57250
   367
nipkow@59776
   368
lemma set_preorder[simp]: "set (preorder t) = set_tree t"
nipkow@59776
   369
by (induction t) auto
nipkow@59776
   370
nipkow@64925
   371
lemma set_postorder[simp]: "set (postorder t) = set_tree t"
nipkow@64925
   372
by (induction t) auto
nipkow@64925
   373
nipkow@59776
   374
lemma length_preorder[simp]: "length (preorder t) = size t"
nipkow@59776
   375
by (induction t) auto
nipkow@59776
   376
nipkow@59776
   377
lemma length_inorder[simp]: "length (inorder t) = size t"
nipkow@59776
   378
by (induction t) auto
nipkow@59776
   379
nipkow@64925
   380
lemma length_postorder[simp]: "length (postorder t) = size t"
nipkow@64925
   381
by (induction t) auto
nipkow@64925
   382
nipkow@59776
   383
lemma preorder_map: "preorder (map_tree f t) = map f (preorder t)"
nipkow@59776
   384
by (induction t) auto
nipkow@59776
   385
nipkow@59776
   386
lemma inorder_map: "inorder (map_tree f t) = map f (inorder t)"
nipkow@59776
   387
by (induction t) auto
nipkow@59776
   388
nipkow@64925
   389
lemma postorder_map: "postorder (map_tree f t) = map f (postorder t)"
nipkow@64925
   390
by (induction t) auto
nipkow@64925
   391
nipkow@63765
   392
lemma inorder2_inorder: "inorder2 t xs = inorder t @ xs"
nipkow@63765
   393
by (induction t arbitrary: xs) auto
nipkow@63765
   394
nipkow@57687
   395
nipkow@63861
   396
subsection \<open>Binary Search Tree\<close>
nipkow@59561
   397
nipkow@66606
   398
lemma bst_wrt_mono: "(\<And>x y. P x y \<Longrightarrow> Q x y) \<Longrightarrow> bst_wrt P t \<Longrightarrow> bst_wrt Q t"
nipkow@59928
   399
by (induction t) (auto)
nipkow@59928
   400
nipkow@67399
   401
lemma bst_wrt_le_if_bst: "bst t \<Longrightarrow> bst_wrt (\<le>) t"
nipkow@66606
   402
using bst_wrt_mono less_imp_le by blast
nipkow@66606
   403
nipkow@67399
   404
lemma bst_wrt_le_iff_sorted: "bst_wrt (\<le>) t \<longleftrightarrow> sorted (inorder t)"
nipkow@59561
   405
apply (induction t)
nipkow@59561
   406
 apply(simp)
nipkow@68109
   407
by (fastforce simp: sorted_append intro: less_imp_le less_trans)
nipkow@59561
   408
nipkow@67399
   409
lemma bst_iff_sorted_wrt_less: "bst t \<longleftrightarrow> sorted_wrt (<) (inorder t)"
nipkow@59928
   410
apply (induction t)
nipkow@59928
   411
 apply simp
nipkow@68109
   412
apply (fastforce simp: sorted_wrt_append)
nipkow@59928
   413
done
nipkow@59928
   414
nipkow@59776
   415
wenzelm@69593
   416
subsection \<open>\<^const>\<open>heap\<close>\<close>
nipkow@60505
   417
nipkow@60505
   418
wenzelm@69593
   419
subsection \<open>\<^const>\<open>mirror\<close>\<close>
nipkow@59561
   420
nipkow@59561
   421
lemma mirror_Leaf[simp]: "mirror t = \<langle>\<rangle> \<longleftrightarrow> t = \<langle>\<rangle>"
nipkow@59561
   422
by (induction t) simp_all
nipkow@59561
   423
nipkow@65339
   424
lemma Leaf_mirror[simp]: "\<langle>\<rangle> = mirror t \<longleftrightarrow> t = \<langle>\<rangle>"
nipkow@65339
   425
using mirror_Leaf by fastforce
nipkow@65339
   426
nipkow@59561
   427
lemma size_mirror[simp]: "size(mirror t) = size t"
nipkow@59561
   428
by (induction t) simp_all
nipkow@59561
   429
nipkow@59561
   430
lemma size1_mirror[simp]: "size1(mirror t) = size1 t"
nipkow@68998
   431
by (simp add: size1_size)
nipkow@59561
   432
nipkow@60808
   433
lemma height_mirror[simp]: "height(mirror t) = height t"
nipkow@59776
   434
by (induction t) simp_all
nipkow@59776
   435
nipkow@66659
   436
lemma min_height_mirror [simp]: "min_height (mirror t) = min_height t"
nipkow@66659
   437
by (induction t) simp_all  
nipkow@66659
   438
nipkow@66659
   439
lemma ipl_mirror [simp]: "ipl (mirror t) = ipl t"
nipkow@66659
   440
by (induction t) simp_all
nipkow@66659
   441
nipkow@59776
   442
lemma inorder_mirror: "inorder(mirror t) = rev(inorder t)"
nipkow@59776
   443
by (induction t) simp_all
nipkow@59776
   444
nipkow@59776
   445
lemma map_mirror: "map_tree f (mirror t) = mirror (map_tree f t)"
nipkow@59776
   446
by (induction t) simp_all
nipkow@59776
   447
nipkow@59561
   448
lemma mirror_mirror[simp]: "mirror(mirror t) = t"
nipkow@59561
   449
by (induction t) simp_all
nipkow@59561
   450
nipkow@57250
   451
end