src/HOL/Library/Tree.thy
author wenzelm
Mon Dec 28 01:28:28 2015 +0100 (2015-12-28)
changeset 61945 1135b8de26c3
parent 61585 a9599d3d7610
child 62160 ff20b44b2fc8
permissions -rw-r--r--
more symbols;
nipkow@57250
     1
(* Author: Tobias Nipkow *)
nipkow@57250
     2
wenzelm@60500
     3
section \<open>Binary Tree\<close>
nipkow@57250
     4
nipkow@57250
     5
theory Tree
nipkow@57250
     6
imports Main
nipkow@57250
     7
begin
nipkow@57250
     8
nipkow@58424
     9
datatype 'a tree =
nipkow@58424
    10
  Leaf ("\<langle>\<rangle>") |
nipkow@58424
    11
  Node (left: "'a tree") (val: 'a) (right: "'a tree") ("\<langle>_, _, _\<rangle>")
hoelzl@57449
    12
  where
hoelzl@57449
    13
    "left Leaf = Leaf"
hoelzl@57449
    14
  | "right Leaf = Leaf"
hoelzl@57569
    15
datatype_compat tree
nipkow@57250
    16
wenzelm@60500
    17
text\<open>Can be seen as counting the number of leaves rather than nodes:\<close>
nipkow@58438
    18
nipkow@58438
    19
definition size1 :: "'a tree \<Rightarrow> nat" where
nipkow@58438
    20
"size1 t = size t + 1"
nipkow@58438
    21
nipkow@58438
    22
lemma size1_simps[simp]:
nipkow@58438
    23
  "size1 \<langle>\<rangle> = 1"
nipkow@58438
    24
  "size1 \<langle>l, x, r\<rangle> = size1 l + size1 r"
nipkow@58438
    25
by (simp_all add: size1_def)
nipkow@58438
    26
nipkow@60507
    27
lemma size_0_iff_Leaf: "size t = 0 \<longleftrightarrow> t = Leaf"
nipkow@60505
    28
by(cases t) auto
nipkow@60505
    29
nipkow@58424
    30
lemma neq_Leaf_iff: "(t \<noteq> \<langle>\<rangle>) = (\<exists>l a r. t = \<langle>l, a, r\<rangle>)"
nipkow@58424
    31
by (cases t) auto
nipkow@57530
    32
nipkow@57687
    33
lemma finite_set_tree[simp]: "finite(set_tree t)"
nipkow@57687
    34
by(induction t) auto
nipkow@57687
    35
nipkow@59776
    36
lemma size_map_tree[simp]: "size (map_tree f t) = size t"
nipkow@59776
    37
by (induction t) auto
nipkow@59776
    38
nipkow@59776
    39
lemma size1_map_tree[simp]: "size1 (map_tree f t) = size1 t"
nipkow@59776
    40
by (simp add: size1_def)
nipkow@59776
    41
nipkow@59776
    42
nipkow@60808
    43
subsection "The Height"
nipkow@60808
    44
nipkow@60808
    45
class height = fixes height :: "'a \<Rightarrow> nat"
nipkow@60808
    46
nipkow@60808
    47
instantiation tree :: (type)height
nipkow@60808
    48
begin
nipkow@59776
    49
nipkow@60808
    50
fun height_tree :: "'a tree => nat" where
nipkow@60808
    51
"height Leaf = 0" |
nipkow@60808
    52
"height (Node t1 a t2) = max (height t1) (height t2) + 1"
nipkow@59776
    53
nipkow@60808
    54
instance ..
nipkow@60808
    55
nipkow@60808
    56
end
nipkow@60808
    57
nipkow@60808
    58
lemma height_map_tree[simp]: "height (map_tree f t) = height t"
nipkow@59776
    59
by (induction t) auto
nipkow@59776
    60
nipkow@57687
    61
nipkow@57687
    62
subsection "The set of subtrees"
nipkow@57687
    63
nipkow@57250
    64
fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where
nipkow@60808
    65
"subtrees \<langle>\<rangle> = {\<langle>\<rangle>}" |
nipkow@60808
    66
"subtrees (\<langle>l, a, r\<rangle>) = insert \<langle>l, a, r\<rangle> (subtrees l \<union> subtrees r)"
nipkow@57250
    67
nipkow@58424
    68
lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. \<langle>l, a, r\<rangle> \<in> subtrees t"
nipkow@58424
    69
by (induction t)(auto)
hoelzl@57449
    70
hoelzl@57450
    71
lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t"
nipkow@58424
    72
by (induction t) auto
hoelzl@57449
    73
nipkow@58424
    74
lemma in_set_tree_if: "\<langle>l, a, r\<rangle> \<in> subtrees t \<Longrightarrow> a \<in> set_tree t"
nipkow@58424
    75
by (metis Node_notin_subtrees_if)
hoelzl@57449
    76
nipkow@57687
    77
nipkow@59776
    78
subsection "List of entries"
nipkow@59776
    79
nipkow@59776
    80
fun preorder :: "'a tree \<Rightarrow> 'a list" where
nipkow@59776
    81
"preorder \<langle>\<rangle> = []" |
nipkow@59776
    82
"preorder \<langle>l, x, r\<rangle> = x # preorder l @ preorder r"
nipkow@57687
    83
nipkow@57250
    84
fun inorder :: "'a tree \<Rightarrow> 'a list" where
nipkow@58424
    85
"inorder \<langle>\<rangle> = []" |
nipkow@58424
    86
"inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r"
nipkow@57250
    87
hoelzl@57449
    88
lemma set_inorder[simp]: "set (inorder t) = set_tree t"
nipkow@58424
    89
by (induction t) auto
nipkow@57250
    90
nipkow@59776
    91
lemma set_preorder[simp]: "set (preorder t) = set_tree t"
nipkow@59776
    92
by (induction t) auto
nipkow@59776
    93
nipkow@59776
    94
lemma length_preorder[simp]: "length (preorder t) = size t"
nipkow@59776
    95
by (induction t) auto
nipkow@59776
    96
nipkow@59776
    97
lemma length_inorder[simp]: "length (inorder t) = size t"
nipkow@59776
    98
by (induction t) auto
nipkow@59776
    99
nipkow@59776
   100
lemma preorder_map: "preorder (map_tree f t) = map f (preorder t)"
nipkow@59776
   101
by (induction t) auto
nipkow@59776
   102
nipkow@59776
   103
lemma inorder_map: "inorder (map_tree f t) = map f (inorder t)"
nipkow@59776
   104
by (induction t) auto
nipkow@59776
   105
nipkow@57687
   106
wenzelm@60500
   107
subsection \<open>Binary Search Tree predicate\<close>
nipkow@57250
   108
hoelzl@57450
   109
fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where
nipkow@58424
   110
"bst \<langle>\<rangle> \<longleftrightarrow> True" |
nipkow@58424
   111
"bst \<langle>l, a, r\<rangle> \<longleftrightarrow> bst l \<and> bst r \<and> (\<forall>x\<in>set_tree l. x < a) \<and> (\<forall>x\<in>set_tree r. a < x)"
nipkow@57250
   112
wenzelm@60500
   113
text\<open>In case there are duplicates:\<close>
nipkow@59561
   114
nipkow@59561
   115
fun (in linorder) bst_eq :: "'a tree \<Rightarrow> bool" where
nipkow@59561
   116
"bst_eq \<langle>\<rangle> \<longleftrightarrow> True" |
nipkow@59561
   117
"bst_eq \<langle>l,a,r\<rangle> \<longleftrightarrow>
nipkow@59561
   118
 bst_eq l \<and> bst_eq r \<and> (\<forall>x\<in>set_tree l. x \<le> a) \<and> (\<forall>x\<in>set_tree r. a \<le> x)"
nipkow@59561
   119
nipkow@59928
   120
lemma (in linorder) bst_eq_if_bst: "bst t \<Longrightarrow> bst_eq t"
nipkow@59928
   121
by (induction t) (auto)
nipkow@59928
   122
nipkow@59561
   123
lemma (in linorder) bst_eq_imp_sorted: "bst_eq t \<Longrightarrow> sorted (inorder t)"
nipkow@59561
   124
apply (induction t)
nipkow@59561
   125
 apply(simp)
nipkow@59561
   126
by (fastforce simp: sorted_append sorted_Cons intro: less_imp_le less_trans)
nipkow@59561
   127
nipkow@59928
   128
lemma (in linorder) distinct_preorder_if_bst: "bst t \<Longrightarrow> distinct (preorder t)"
nipkow@59928
   129
apply (induction t)
nipkow@59928
   130
 apply simp
nipkow@59928
   131
apply(fastforce elim: order.asym)
nipkow@59928
   132
done
nipkow@59928
   133
nipkow@59928
   134
lemma (in linorder) distinct_inorder_if_bst: "bst t \<Longrightarrow> distinct (inorder t)"
nipkow@59928
   135
apply (induction t)
nipkow@59928
   136
 apply simp
nipkow@59928
   137
apply(fastforce elim: order.asym)
nipkow@59928
   138
done
nipkow@59928
   139
nipkow@59776
   140
nipkow@60505
   141
subsection "The heap predicate"
nipkow@60505
   142
nipkow@60505
   143
fun heap :: "'a::linorder tree \<Rightarrow> bool" where
nipkow@60505
   144
"heap Leaf = True" |
nipkow@60505
   145
"heap (Node l m r) =
nipkow@60505
   146
  (heap l \<and> heap r \<and> (\<forall>x \<in> set_tree l \<union> set_tree r. m \<le> x))"
nipkow@60505
   147
nipkow@60505
   148
wenzelm@61585
   149
subsection "Function \<open>mirror\<close>"
nipkow@59561
   150
nipkow@59561
   151
fun mirror :: "'a tree \<Rightarrow> 'a tree" where
nipkow@59561
   152
"mirror \<langle>\<rangle> = Leaf" |
nipkow@59561
   153
"mirror \<langle>l,x,r\<rangle> = \<langle>mirror r, x, mirror l\<rangle>"
nipkow@59561
   154
nipkow@59561
   155
lemma mirror_Leaf[simp]: "mirror t = \<langle>\<rangle> \<longleftrightarrow> t = \<langle>\<rangle>"
nipkow@59561
   156
by (induction t) simp_all
nipkow@59561
   157
nipkow@59561
   158
lemma size_mirror[simp]: "size(mirror t) = size t"
nipkow@59561
   159
by (induction t) simp_all
nipkow@59561
   160
nipkow@59561
   161
lemma size1_mirror[simp]: "size1(mirror t) = size1 t"
nipkow@59561
   162
by (simp add: size1_def)
nipkow@59561
   163
nipkow@60808
   164
lemma height_mirror[simp]: "height(mirror t) = height t"
nipkow@59776
   165
by (induction t) simp_all
nipkow@59776
   166
nipkow@59776
   167
lemma inorder_mirror: "inorder(mirror t) = rev(inorder t)"
nipkow@59776
   168
by (induction t) simp_all
nipkow@59776
   169
nipkow@59776
   170
lemma map_mirror: "map_tree f (mirror t) = mirror (map_tree f t)"
nipkow@59776
   171
by (induction t) simp_all
nipkow@59776
   172
nipkow@59561
   173
lemma mirror_mirror[simp]: "mirror(mirror t) = t"
nipkow@59561
   174
by (induction t) simp_all
nipkow@59561
   175
nipkow@57250
   176
end