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