| author | blanchet |
| Thu, 05 Mar 2015 12:32:11 +0100 | |
| changeset 59607 | a93592aedce4 |
| parent 59561 | 1a84beaa239b |
| child 59776 | f54af3307334 |
| permissions | -rw-r--r-- |
| 57250 | 1 |
(* Author: Tobias Nipkow *) |
2 |
||
| 58881 | 3 |
section {* Binary Tree *}
|
| 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 |
|
| 58438 | 17 |
text{* Can be seen as counting the number of leaves rather than nodes: *}
|
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 |
||
| 58424 | 27 |
lemma neq_Leaf_iff: "(t \<noteq> \<langle>\<rangle>) = (\<exists>l a r. t = \<langle>l, a, r\<rangle>)" |
28 |
by (cases t) auto |
|
| 57530 | 29 |
|
| 57687 | 30 |
lemma finite_set_tree[simp]: "finite(set_tree t)" |
31 |
by(induction t) auto |
|
32 |
||
33 |
||
34 |
subsection "The set of subtrees" |
|
35 |
||
| 57250 | 36 |
fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where |
| 58424 | 37 |
"subtrees \<langle>\<rangle> = {\<langle>\<rangle>}" |
|
38 |
"subtrees (\<langle>l, a, r\<rangle>) = insert \<langle>l, a, r\<rangle> (subtrees l \<union> subtrees r)" |
|
| 57250 | 39 |
|
| 58424 | 40 |
lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. \<langle>l, a, r\<rangle> \<in> subtrees t" |
41 |
by (induction t)(auto) |
|
|
57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset
|
42 |
|
| 57450 | 43 |
lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t" |
| 58424 | 44 |
by (induction t) auto |
|
57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset
|
45 |
|
| 58424 | 46 |
lemma in_set_tree_if: "\<langle>l, a, r\<rangle> \<in> subtrees t \<Longrightarrow> a \<in> set_tree t" |
47 |
by (metis Node_notin_subtrees_if) |
|
|
57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset
|
48 |
|
| 57687 | 49 |
|
50 |
subsection "Inorder list of entries" |
|
51 |
||
| 57250 | 52 |
fun inorder :: "'a tree \<Rightarrow> 'a list" where |
| 58424 | 53 |
"inorder \<langle>\<rangle> = []" | |
54 |
"inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r" |
|
| 57250 | 55 |
|
|
57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset
|
56 |
lemma set_inorder[simp]: "set (inorder t) = set_tree t" |
| 58424 | 57 |
by (induction t) auto |
| 57250 | 58 |
|
| 57687 | 59 |
|
|
57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset
|
60 |
subsection {* Binary Search Tree predicate *}
|
| 57250 | 61 |
|
| 57450 | 62 |
fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where |
| 58424 | 63 |
"bst \<langle>\<rangle> \<longleftrightarrow> True" | |
64 |
"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 | 65 |
|
| 57450 | 66 |
lemma (in linorder) bst_imp_sorted: "bst t \<Longrightarrow> sorted (inorder t)" |
| 58424 | 67 |
by (induction t) (auto simp: sorted_append sorted_Cons intro: less_imp_le less_trans) |
| 57250 | 68 |
|
| 59561 | 69 |
text{* In case there are duplicates: *}
|
70 |
||
71 |
fun (in linorder) bst_eq :: "'a tree \<Rightarrow> bool" where |
|
72 |
"bst_eq \<langle>\<rangle> \<longleftrightarrow> True" | |
|
73 |
"bst_eq \<langle>l,a,r\<rangle> \<longleftrightarrow> |
|
74 |
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)" |
|
75 |
||
76 |
lemma (in linorder) bst_eq_imp_sorted: "bst_eq t \<Longrightarrow> sorted (inorder t)" |
|
77 |
apply (induction t) |
|
78 |
apply(simp) |
|
79 |
by (fastforce simp: sorted_append sorted_Cons intro: less_imp_le less_trans) |
|
80 |
||
81 |
subsection "Function @{text mirror}"
|
|
82 |
||
83 |
fun mirror :: "'a tree \<Rightarrow> 'a tree" where |
|
84 |
"mirror \<langle>\<rangle> = Leaf" | |
|
85 |
"mirror \<langle>l,x,r\<rangle> = \<langle>mirror r, x, mirror l\<rangle>" |
|
86 |
||
87 |
lemma mirror_Leaf[simp]: "mirror t = \<langle>\<rangle> \<longleftrightarrow> t = \<langle>\<rangle>" |
|
88 |
by (induction t) simp_all |
|
89 |
||
90 |
lemma size_mirror[simp]: "size(mirror t) = size t" |
|
91 |
by (induction t) simp_all |
|
92 |
||
93 |
lemma size1_mirror[simp]: "size1(mirror t) = size1 t" |
|
94 |
by (simp add: size1_def) |
|
95 |
||
96 |
lemma mirror_mirror[simp]: "mirror(mirror t) = t" |
|
97 |
by (induction t) simp_all |
|
98 |
||
| 57687 | 99 |
|
100 |
subsection "Deletion of the rightmost entry" |
|
101 |
||
102 |
fun del_rightmost :: "'a tree \<Rightarrow> 'a tree * 'a" where |
|
| 58424 | 103 |
"del_rightmost \<langle>l, a, \<langle>\<rangle>\<rangle> = (l,a)" | |
104 |
"del_rightmost \<langle>l, a, r\<rangle> = (let (r',x) = del_rightmost r in (\<langle>l, a, r'\<rangle>, x))" |
|
| 57687 | 105 |
|
106 |
lemma del_rightmost_set_tree_if_bst: |
|
107 |
"\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> Leaf \<rbrakk> |
|
108 |
\<Longrightarrow> x \<in> set_tree t \<and> set_tree t' = set_tree t - {x}"
|
|
109 |
apply(induction t arbitrary: t' rule: del_rightmost.induct) |
|
110 |
apply (fastforce simp: ball_Un split: prod.splits)+ |
|
111 |
done |
|
112 |
||
113 |
lemma del_rightmost_set_tree: |
|
| 58424 | 114 |
"\<lbrakk> del_rightmost t = (t',x); t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> set_tree t = insert x (set_tree t')" |
| 57687 | 115 |
apply(induction t arbitrary: t' rule: del_rightmost.induct) |
116 |
by (auto split: prod.splits) auto |
|
117 |
||
118 |
lemma del_rightmost_bst: |
|
| 58424 | 119 |
"\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> bst t'" |
| 57687 | 120 |
proof(induction t arbitrary: t' rule: del_rightmost.induct) |
121 |
case (2 l a rl b rr) |
|
122 |
let ?r = "Node rl b rr" |
|
123 |
from "2.prems"(1) obtain r' where 1: "del_rightmost ?r = (r',x)" and [simp]: "t' = Node l a r'" |
|
124 |
by(simp split: prod.splits) |
|
125 |
from "2.prems"(2) 1 del_rightmost_set_tree[OF 1] show ?case by(auto)(simp add: "2.IH") |
|
126 |
qed auto |
|
127 |
||
128 |
||
| 58424 | 129 |
lemma del_rightmost_greater: "\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> \<langle>\<rangle> \<rbrakk> |
| 57687 | 130 |
\<Longrightarrow> \<forall>a\<in>set_tree t'. a < x" |
131 |
proof(induction t arbitrary: t' rule: del_rightmost.induct) |
|
132 |
case (2 l a rl b rr) |
|
133 |
from "2.prems"(1) obtain r' |
|
134 |
where dm: "del_rightmost (Node rl b rr) = (r',x)" and [simp]: "t' = Node l a r'" |
|
135 |
by(simp split: prod.splits) |
|
136 |
show ?case using "2.prems"(2) "2.IH"[OF dm] del_rightmost_set_tree_if_bst[OF dm] |
|
137 |
by (fastforce simp add: ball_Un) |
|
138 |
qed simp_all |
|
139 |
||
140 |
lemma del_rightmost_Max: |
|
| 58424 | 141 |
"\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> x = Max(set_tree t)" |
| 58467 | 142 |
by (metis Max_insert2 del_rightmost_greater del_rightmost_set_tree finite_set_tree less_le_not_le) |
| 57687 | 143 |
|
| 57250 | 144 |
end |