src/HOL/Library/Tree.thy
author nipkow
Thu, 01 Sep 2016 15:57:54 +0200
changeset 63755 182c111190e5
parent 63665 15f48ce7ec23
child 63765 e60020520b15
permissions -rw-r--r--
Renamed balanced to complete; added balanced; more about both
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
     2
(* Todo:
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
     3
 size t = 2^h - 1 \<Longrightarrow> complete t
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
     4
 (min_)height of balanced trees via floorlog
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
     5
*)
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     6
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59928
diff changeset
     7
section \<open>Binary Tree\<close>
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     8
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
     9
theory Tree
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    10
imports Main
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    11
begin
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    12
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    13
datatype 'a tree =
62160
ff20b44b2fc8 tuned layout
nipkow
parents: 61585
diff changeset
    14
  is_Leaf: Leaf ("\<langle>\<rangle>") |
ff20b44b2fc8 tuned layout
nipkow
parents: 61585
diff changeset
    15
  Node (left: "'a tree") (val: 'a) (right: "'a tree") ("(1\<langle>_,/ _,/ _\<rangle>)")
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    16
  where
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    17
    "left Leaf = Leaf"
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
    18
  | "right Leaf = Leaf"
57569
e20a999f7161 register tree with datatype_compat ot support QuickCheck
hoelzl
parents: 57530
diff changeset
    19
datatype_compat tree
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
    20
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59928
diff changeset
    21
text\<open>Can be seen as counting the number of leaves rather than nodes:\<close>
58438
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    22
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    23
definition size1 :: "'a tree \<Rightarrow> nat" where
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    24
"size1 t = size t + 1"
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    25
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    26
lemma size1_simps[simp]:
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    27
  "size1 \<langle>\<rangle> = 1"
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    28
  "size1 \<langle>l, x, r\<rangle> = size1 l + size1 r"
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    29
by (simp_all add: size1_def)
566117a31cc0 added function size1
nipkow
parents: 58424
diff changeset
    30
62650
7e6bb43e7217 added tree lemmas
nipkow
parents: 62202
diff changeset
    31
lemma size1_ge0[simp]: "0 < size1 t"
7e6bb43e7217 added tree lemmas
nipkow
parents: 62202
diff changeset
    32
by (simp add: size1_def)
7e6bb43e7217 added tree lemmas
nipkow
parents: 62202
diff changeset
    33
60507
nipkow
parents: 60506
diff changeset
    34
lemma size_0_iff_Leaf: "size t = 0 \<longleftrightarrow> t = Leaf"
60505
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
    35
by(cases t) auto
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
    36
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    37
lemma neq_Leaf_iff: "(t \<noteq> \<langle>\<rangle>) = (\<exists>l a r. t = \<langle>l, a, r\<rangle>)"
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
    38
by (cases t) auto
57530
439f881c8744 added lemma
nipkow
parents: 57450
diff changeset
    39
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    40
lemma finite_set_tree[simp]: "finite(set_tree t)"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    41
by(induction t) auto
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    42
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    43
lemma size_map_tree[simp]: "size (map_tree f t) = size t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    44
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    45
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    46
lemma size1_map_tree[simp]: "size1 (map_tree f t) = size1 t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    47
by (simp add: size1_def)
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    48
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    49
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    50
subsection "The Height"
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    51
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    52
class height = fixes height :: "'a \<Rightarrow> nat"
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    53
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    54
instantiation tree :: (type)height
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    55
begin
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    56
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    57
fun height_tree :: "'a tree => nat" where
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    58
"height Leaf = 0" |
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    59
"height (Node t1 a t2) = max (height t1) (height t2) + 1"
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    60
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    61
instance ..
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    62
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    63
end
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    64
63665
15f48ce7ec23 added lemma
nipkow
parents: 63598
diff changeset
    65
lemma height_0_iff_Leaf: "height t = 0 \<longleftrightarrow> t = Leaf"
15f48ce7ec23 added lemma
nipkow
parents: 63598
diff changeset
    66
by(cases t) auto
15f48ce7ec23 added lemma
nipkow
parents: 63598
diff changeset
    67
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
    68
lemma height_map_tree[simp]: "height (map_tree f t) = height t"
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    69
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
    70
62202
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    71
lemma size1_height: "size t + 1 \<le> 2 ^ height (t::'a tree)"
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    72
proof(induction t)
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    73
  case (Node l a r)
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    74
  show ?case
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    75
  proof (cases "height l \<le> height r")
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    76
    case True
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    77
    have "size(Node l a r) + 1 = (size l + 1) + (size r + 1)" by simp
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    78
    also have "size l + 1 \<le> 2 ^ height l" by(rule Node.IH(1))
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    79
    also have "size r + 1 \<le> 2 ^ height r" by(rule Node.IH(2))
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    80
    also have "(2::nat) ^ height l \<le> 2 ^ height r" using True by simp
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    81
    finally show ?thesis using True by (auto simp: max_def mult_2)
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    82
  next
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    83
    case False
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    84
    have "size(Node l a r) + 1 = (size l + 1) + (size r + 1)" by simp
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    85
    also have "size l + 1 \<le> 2 ^ height l" by(rule Node.IH(1))
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    86
    also have "size r + 1 \<le> 2 ^ height r" by(rule Node.IH(2))
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    87
    also have "(2::nat) ^ height r \<le> 2 ^ height l" using False by simp
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    88
    finally show ?thesis using False by (auto simp: max_def mult_2)
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    89
  qed
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    90
qed simp
e5bc7cbb0bcc added lemma
nipkow
parents: 62160
diff changeset
    91
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
    92
corollary size_height: "size t \<le> 2 ^ height (t::'a tree) - 1"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
    93
using size1_height[of t] by(arith)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
    94
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
    95
63598
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
    96
fun min_height :: "'a tree \<Rightarrow> nat" where
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
    97
"min_height Leaf = 0" |
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
    98
"min_height (Node l _ r) = min (min_height l) (min_height r) + 1"
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
    99
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   100
lemma min_hight_le_height: "min_height t \<le> height t"
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   101
by(induction t) auto
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   102
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   103
lemma min_height_map_tree[simp]: "min_height (map_tree f t) = min_height t"
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   104
by (induction t) auto
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   105
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   106
lemma min_height_le_size1: "2 ^ min_height t \<le> size t + 1"
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   107
proof(induction t)
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   108
  case (Node l a r)
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   109
  have "(2::nat) ^ min_height (Node l a r) \<le> 2 ^ min_height l + 2 ^ min_height r"
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   110
    by (simp add: min_def)
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   111
  also have "\<dots> \<le> size(Node l a r) + 1" using Node.IH by simp
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   112
  finally show ?case .
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   113
qed simp
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   114
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   115
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   116
subsection \<open>Complete\<close>
63036
1ba3aacfa4d3 added "balanced" predicate
nipkow
parents: 62650
diff changeset
   117
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   118
fun complete :: "'a tree \<Rightarrow> bool" where
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   119
"complete Leaf = True" |
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   120
"complete (Node l x r) = (complete l \<and> complete r \<and> height l = height r)"
63036
1ba3aacfa4d3 added "balanced" predicate
nipkow
parents: 62650
diff changeset
   121
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   122
lemma complete_iff_height: "complete t \<longleftrightarrow> (min_height t = height t)"
63598
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   123
apply(induction t)
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   124
 apply simp
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   125
apply (simp add: min_def max_def)
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   126
by (metis le_antisym le_trans min_hight_le_height)
025d6e52d86f added min_height
nipkow
parents: 63413
diff changeset
   127
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   128
lemma complete_size1: "complete t \<Longrightarrow> size1 t = 2 ^ height t"
63036
1ba3aacfa4d3 added "balanced" predicate
nipkow
parents: 62650
diff changeset
   129
by (induction t) auto
1ba3aacfa4d3 added "balanced" predicate
nipkow
parents: 62650
diff changeset
   130
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   131
lemma size_if_complete: "complete t \<Longrightarrow> size t = 2 ^ height t - 1"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   132
using complete_size1[simplified size1_def] by fastforce
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   133
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   134
text\<open>A better lower bound for incomplete trees:\<close>
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   135
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   136
lemma min_height_le_size_if_incomplete:
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   137
  "\<not> complete t \<Longrightarrow> 2 ^ min_height t \<le> size t"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   138
proof(induction t)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   139
  case Leaf thus ?case by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   140
next
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   141
  case (Node l a r)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   142
  show ?case (is "?l \<le> ?r")
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   143
  proof (cases "complete l")
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   144
    case l: True thus ?thesis
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   145
    proof (cases "complete r")
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   146
      case r: True
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   147
      have "height l \<noteq> height r" using Node.prems l r by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   148
      hence "?l < 2 ^ min_height l + 2 ^ min_height r"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   149
        using l r by (simp add: min_def complete_iff_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   150
      also have "\<dots> = (size l + 1) + (size r + 1)"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   151
        using l r size_if_complete[where ?'a = 'a]
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   152
        by (simp add: complete_iff_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   153
      also have "\<dots> \<le> ?r + 1" by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   154
      finally show ?thesis by arith
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   155
    next
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   156
      case r: False
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   157
      have "?l \<le> 2 ^ min_height l + 2 ^ min_height r" by (simp add: min_def)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   158
      also have "\<dots> \<le> size l + 1 + size r"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   159
        using Node.IH(2)[OF r] l size_if_complete[where ?'a = 'a]
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   160
        by (simp add: complete_iff_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   161
      also have "\<dots> = ?r" by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   162
      finally show ?thesis .
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   163
    qed
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   164
  next
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   165
    case l: False thus ?thesis
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   166
    proof (cases "complete r")
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   167
      case r: True
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   168
      have "?l \<le> 2 ^ min_height l + 2 ^ min_height r" by (simp add: min_def)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   169
      also have "\<dots> \<le> size l + (size r + 1)"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   170
        using Node.IH(1)[OF l] r size_if_complete[where ?'a = 'a]
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   171
        by (simp add: complete_iff_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   172
      also have "\<dots> = ?r" by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   173
      finally show ?thesis .
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   174
    next
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   175
      case r: False
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   176
      have "?l \<le> 2 ^ min_height l + 2 ^ min_height r"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   177
        by (simp add: min_def)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   178
      also have "\<dots> \<le> size l + size r"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   179
        using Node.IH(1)[OF l] Node.IH(2)[OF r] by (simp)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   180
      also have "\<dots> \<le> ?r" by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   181
      finally show ?thesis .
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   182
    qed
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   183
  qed
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   184
qed
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   185
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   186
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   187
subsection \<open>Balanced\<close>
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   188
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   189
abbreviation "balanced t \<equiv> (height t - min_height t \<le> 1)"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   190
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   191
text\<open>Balanced trees have optimal height:\<close>
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   192
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   193
lemma balanced_optimal:
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   194
fixes t :: "'a tree" and t' :: "'b tree"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   195
assumes "balanced t" "size t \<le> size t'" shows "height t \<le> height t'"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   196
proof (cases "complete t")
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   197
  case True
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   198
  have "(2::nat) ^ height t - 1 \<le> 2 ^ height t' - 1"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   199
  proof -
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   200
    have "(2::nat) ^ height t - 1 = size t"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   201
      using True by (simp add: complete_iff_height size_if_complete)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   202
    also note assms(2)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   203
    also have "size t' \<le> 2 ^ height t' - 1" by (rule size_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   204
    finally show ?thesis .
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   205
  qed
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   206
  thus ?thesis by (simp add: le_diff_iff)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   207
next
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   208
  case False
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   209
  have "(2::nat) ^ min_height t < 2 ^ height t'"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   210
  proof -
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   211
    have "(2::nat) ^ min_height t \<le> size t"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   212
      by(rule min_height_le_size_if_incomplete[OF False])
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   213
    also note assms(2)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   214
    also have "size t' \<le> 2 ^ height t' - 1"  by(rule size_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   215
    finally show ?thesis
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   216
      using power_eq_0_iff[of "2::nat" "height t'"] by linarith
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   217
  qed
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   218
  hence *: "min_height t < height t'" by simp
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   219
  have "min_height t + 1 = height t"
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   220
    using min_hight_le_height[of t] assms(1) False
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   221
    by (simp add: complete_iff_height)
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   222
  with * show ?thesis by arith
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   223
qed
63036
1ba3aacfa4d3 added "balanced" predicate
nipkow
parents: 62650
diff changeset
   224
1ba3aacfa4d3 added "balanced" predicate
nipkow
parents: 62650
diff changeset
   225
63413
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   226
subsection \<open>Path length\<close>
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   227
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   228
text \<open>The internal path length of a tree:\<close>
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   229
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   230
fun path_len :: "'a tree \<Rightarrow> nat" where
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   231
"path_len Leaf = 0 " |
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   232
"path_len (Node l _ r) = path_len l + size l + path_len r + size r"
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   233
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   234
lemma path_len_if_bal: "complete t
63413
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   235
  \<Longrightarrow> path_len t = (let n = height t in 2 + n*2^n - 2^(n+1))"
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   236
proof(induction t)
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   237
  case (Node l x r)
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   238
  have *: "2^(n+1) \<le> 2 + n*2^n" for n :: nat
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   239
    by(induction n) auto
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   240
  have **: "(0::nat) < 2^n" for n :: nat by simp
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   241
  let ?h = "height r"
63755
182c111190e5 Renamed balanced to complete; added balanced; more about both
nipkow
parents: 63665
diff changeset
   242
  show ?case using Node *[of ?h] **[of ?h] by (simp add: size_if_complete Let_def)
63413
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   243
qed simp
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   244
9fe2d9dc095e added path_len
nipkow
parents: 63036
diff changeset
   245
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   246
subsection "The set of subtrees"
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   247
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   248
fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
   249
"subtrees \<langle>\<rangle> = {\<langle>\<rangle>}" |
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
   250
"subtrees (\<langle>l, a, r\<rangle>) = insert \<langle>l, a, r\<rangle> (subtrees l \<union> subtrees r)"
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   251
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   252
lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. \<langle>l, a, r\<rangle> \<in> subtrees t"
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   253
by (induction t)(auto)
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
   254
57450
2baecef3207f Library/Tree: bst is preferred to be a function
hoelzl
parents: 57449
diff changeset
   255
lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t"
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   256
by (induction t) auto
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
   257
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   258
lemma in_set_tree_if: "\<langle>l, a, r\<rangle> \<in> subtrees t \<Longrightarrow> a \<in> set_tree t"
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   259
by (metis Node_notin_subtrees_if)
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
   260
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   261
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   262
subsection "List of entries"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   263
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   264
fun preorder :: "'a tree \<Rightarrow> 'a list" where
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   265
"preorder \<langle>\<rangle> = []" |
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   266
"preorder \<langle>l, x, r\<rangle> = x # preorder l @ preorder r"
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   267
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   268
fun inorder :: "'a tree \<Rightarrow> 'a list" where
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   269
"inorder \<langle>\<rangle> = []" |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   270
"inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r"
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   271
57449
f81da03b9ebd Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents: 57250
diff changeset
   272
lemma set_inorder[simp]: "set (inorder t) = set_tree t"
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   273
by (induction t) auto
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   274
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   275
lemma set_preorder[simp]: "set (preorder t) = set_tree t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   276
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   277
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   278
lemma length_preorder[simp]: "length (preorder t) = size t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   279
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   280
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   281
lemma length_inorder[simp]: "length (inorder t) = size t"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   282
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   283
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   284
lemma preorder_map: "preorder (map_tree f t) = map f (preorder t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   285
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   286
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   287
lemma inorder_map: "inorder (map_tree f t) = map f (inorder t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   288
by (induction t) auto
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   289
57687
cca7e8788481 added more functions and lemmas
nipkow
parents: 57569
diff changeset
   290
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59928
diff changeset
   291
subsection \<open>Binary Search Tree predicate\<close>
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   292
57450
2baecef3207f Library/Tree: bst is preferred to be a function
hoelzl
parents: 57449
diff changeset
   293
fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where
58424
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   294
"bst \<langle>\<rangle> \<longleftrightarrow> True" |
cbbba613b6ab added nice standard syntax
nipkow
parents: 58310
diff changeset
   295
"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)"
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   296
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59928
diff changeset
   297
text\<open>In case there are duplicates:\<close>
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   298
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   299
fun (in linorder) bst_eq :: "'a tree \<Rightarrow> bool" where
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   300
"bst_eq \<langle>\<rangle> \<longleftrightarrow> True" |
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   301
"bst_eq \<langle>l,a,r\<rangle> \<longleftrightarrow>
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   302
 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)"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   303
59928
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   304
lemma (in linorder) bst_eq_if_bst: "bst t \<Longrightarrow> bst_eq t"
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   305
by (induction t) (auto)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   306
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   307
lemma (in linorder) bst_eq_imp_sorted: "bst_eq t \<Longrightarrow> sorted (inorder t)"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   308
apply (induction t)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   309
 apply(simp)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   310
by (fastforce simp: sorted_append sorted_Cons intro: less_imp_le less_trans)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   311
59928
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   312
lemma (in linorder) distinct_preorder_if_bst: "bst t \<Longrightarrow> distinct (preorder t)"
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   313
apply (induction t)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   314
 apply simp
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   315
apply(fastforce elim: order.asym)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   316
done
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   317
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   318
lemma (in linorder) distinct_inorder_if_bst: "bst t \<Longrightarrow> distinct (inorder t)"
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   319
apply (induction t)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   320
 apply simp
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   321
apply(fastforce elim: order.asym)
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   322
done
b9b7f913a19a new theory Library/Tree_Multiset.thy
nipkow
parents: 59776
diff changeset
   323
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   324
60505
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   325
subsection "The heap predicate"
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   326
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   327
fun heap :: "'a::linorder tree \<Rightarrow> bool" where
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   328
"heap Leaf = True" |
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   329
"heap (Node l m r) =
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   330
  (heap l \<and> heap r \<and> (\<forall>x \<in> set_tree l \<union> set_tree r. m \<le> x))"
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   331
9e6584184315 added funs and lemmas
nipkow
parents: 59928
diff changeset
   332
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 60808
diff changeset
   333
subsection "Function \<open>mirror\<close>"
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   334
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   335
fun mirror :: "'a tree \<Rightarrow> 'a tree" where
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   336
"mirror \<langle>\<rangle> = Leaf" |
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   337
"mirror \<langle>l,x,r\<rangle> = \<langle>mirror r, x, mirror l\<rangle>"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   338
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   339
lemma mirror_Leaf[simp]: "mirror t = \<langle>\<rangle> \<longleftrightarrow> t = \<langle>\<rangle>"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   340
by (induction t) simp_all
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   341
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   342
lemma size_mirror[simp]: "size(mirror t) = size t"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   343
by (induction t) simp_all
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   344
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   345
lemma size1_mirror[simp]: "size1(mirror t) = size1 t"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   346
by (simp add: size1_def)
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   347
60808
fd26519b1a6a depth -> height; removed del_rightmost (too specifi)
nipkow
parents: 60507
diff changeset
   348
lemma height_mirror[simp]: "height(mirror t) = height t"
59776
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   349
by (induction t) simp_all
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   350
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   351
lemma inorder_mirror: "inorder(mirror t) = rev(inorder t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   352
by (induction t) simp_all
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   353
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   354
lemma map_mirror: "map_tree f (mirror t) = mirror (map_tree f t)"
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   355
by (induction t) simp_all
f54af3307334 added funs and lemmas
nipkow
parents: 59561
diff changeset
   356
59561
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   357
lemma mirror_mirror[simp]: "mirror(mirror t) = t"
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   358
by (induction t) simp_all
1a84beaa239b added new tree material
nipkow
parents: 58881
diff changeset
   359
57250
cddaf5b93728 new theory of binary trees
nipkow
parents:
diff changeset
   360
end