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