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