| author | wenzelm | 
| Thu, 05 Nov 2015 10:39:49 +0100 | |
| changeset 61585 | a9599d3d7610 | 
| parent 60808 | fd26519b1a6a | 
| child 62160 | ff20b44b2fc8 | 
| permissions | -rw-r--r-- | 
| 57250 | 1  | 
(* Author: Tobias Nipkow *)  | 
2  | 
||
| 60500 | 3  | 
section \<open>Binary Tree\<close>  | 
| 57250 | 4  | 
|
5  | 
theory Tree  | 
|
6  | 
imports Main  | 
|
7  | 
begin  | 
|
8  | 
||
| 58424 | 9  | 
datatype 'a tree =  | 
10  | 
  Leaf ("\<langle>\<rangle>") |
 | 
|
11  | 
  Node (left: "'a tree") (val: 'a) (right: "'a tree") ("\<langle>_, _, _\<rangle>")
 | 
|
| 
57449
 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 
hoelzl 
parents: 
57250 
diff
changeset
 | 
12  | 
where  | 
| 
 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 
hoelzl 
parents: 
57250 
diff
changeset
 | 
13  | 
"left Leaf = Leaf"  | 
| 
 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 
hoelzl 
parents: 
57250 
diff
changeset
 | 
14  | 
| "right Leaf = Leaf"  | 
| 
57569
 
e20a999f7161
register tree with datatype_compat ot support QuickCheck
 
hoelzl 
parents: 
57530 
diff
changeset
 | 
15  | 
datatype_compat tree  | 
| 57250 | 16  | 
|
| 60500 | 17  | 
text\<open>Can be seen as counting the number of leaves rather than nodes:\<close>  | 
| 58438 | 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  | 
||
| 60507 | 27  | 
lemma size_0_iff_Leaf: "size t = 0 \<longleftrightarrow> t = Leaf"  | 
| 60505 | 28  | 
by(cases t) auto  | 
29  | 
||
| 58424 | 30  | 
lemma neq_Leaf_iff: "(t \<noteq> \<langle>\<rangle>) = (\<exists>l a r. t = \<langle>l, a, r\<rangle>)"  | 
31  | 
by (cases t) auto  | 
|
| 57530 | 32  | 
|
| 57687 | 33  | 
lemma finite_set_tree[simp]: "finite(set_tree t)"  | 
34  | 
by(induction t) auto  | 
|
35  | 
||
| 59776 | 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  | 
||
| 
60808
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
43  | 
subsection "The Height"  | 
| 
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
44  | 
|
| 
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
45  | 
class height = fixes height :: "'a \<Rightarrow> nat"  | 
| 
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
46  | 
|
| 
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
47  | 
instantiation tree :: (type)height  | 
| 
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
48  | 
begin  | 
| 59776 | 49  | 
|
| 
60808
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
50  | 
fun height_tree :: "'a tree => nat" where  | 
| 
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
51  | 
"height Leaf = 0" |  | 
| 
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
52  | 
"height (Node t1 a t2) = max (height t1) (height t2) + 1"  | 
| 59776 | 53  | 
|
| 
60808
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
54  | 
instance ..  | 
| 
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
55  | 
|
| 
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
56  | 
end  | 
| 
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
57  | 
|
| 
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
58  | 
lemma height_map_tree[simp]: "height (map_tree f t) = height t"  | 
| 59776 | 59  | 
by (induction t) auto  | 
60  | 
||
| 57687 | 61  | 
|
62  | 
subsection "The set of subtrees"  | 
|
63  | 
||
| 57250 | 64  | 
fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where  | 
| 
60808
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
65  | 
"subtrees \<langle>\<rangle> = {\<langle>\<rangle>}" |
 | 
| 
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
66  | 
"subtrees (\<langle>l, a, r\<rangle>) = insert \<langle>l, a, r\<rangle> (subtrees l \<union> subtrees r)"  | 
| 57250 | 67  | 
|
| 58424 | 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)  | 
|
| 
57449
 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 
hoelzl 
parents: 
57250 
diff
changeset
 | 
70  | 
|
| 57450 | 71  | 
lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t"  | 
| 58424 | 72  | 
by (induction t) auto  | 
| 
57449
 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 
hoelzl 
parents: 
57250 
diff
changeset
 | 
73  | 
|
| 58424 | 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)  | 
|
| 
57449
 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 
hoelzl 
parents: 
57250 
diff
changeset
 | 
76  | 
|
| 57687 | 77  | 
|
| 59776 | 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"  | 
|
| 57687 | 83  | 
|
| 57250 | 84  | 
fun inorder :: "'a tree \<Rightarrow> 'a list" where  | 
| 58424 | 85  | 
"inorder \<langle>\<rangle> = []" |  | 
86  | 
"inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r"  | 
|
| 57250 | 87  | 
|
| 
57449
 
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
 
hoelzl 
parents: 
57250 
diff
changeset
 | 
88  | 
lemma set_inorder[simp]: "set (inorder t) = set_tree t"  | 
| 58424 | 89  | 
by (induction t) auto  | 
| 57250 | 90  | 
|
| 59776 | 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  | 
||
| 57687 | 106  | 
|
| 60500 | 107  | 
subsection \<open>Binary Search Tree predicate\<close>  | 
| 57250 | 108  | 
|
| 57450 | 109  | 
fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where  | 
| 58424 | 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)"  | 
|
| 57250 | 112  | 
|
| 60500 | 113  | 
text\<open>In case there are duplicates:\<close>  | 
| 59561 | 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  | 
||
| 59928 | 120  | 
lemma (in linorder) bst_eq_if_bst: "bst t \<Longrightarrow> bst_eq t"  | 
121  | 
by (induction t) (auto)  | 
|
122  | 
||
| 59561 | 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  | 
||
| 59928 | 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  | 
||
| 59776 | 140  | 
|
| 60505 | 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  | 
||
| 61585 | 149  | 
subsection "Function \<open>mirror\<close>"  | 
| 59561 | 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  | 
||
| 
60808
 
fd26519b1a6a
depth -> height; removed del_rightmost (too specifi)
 
nipkow 
parents: 
60507 
diff
changeset
 | 
164  | 
lemma height_mirror[simp]: "height(mirror t) = height t"  | 
| 59776 | 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  | 
||
| 59561 | 173  | 
lemma mirror_mirror[simp]: "mirror(mirror t) = t"  | 
174  | 
by (induction t) simp_all  | 
|
175  | 
||
| 57250 | 176  | 
end  |