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