author | nipkow |
Wed, 24 Sep 2014 11:09:05 +0200 | |
changeset 58424 | cbbba613b6ab |
parent 58310 | 91ea607a34d8 |
child 58438 | 566117a31cc0 |
permissions | -rw-r--r-- |
57250 | 1 |
(* Author: Tobias Nipkow *) |
2 |
||
3 |
header {* Binary Tree *} |
|
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 |
|
58424 | 17 |
lemma neq_Leaf_iff: "(t \<noteq> \<langle>\<rangle>) = (\<exists>l a r. t = \<langle>l, a, r\<rangle>)" |
18 |
by (cases t) auto |
|
57530 | 19 |
|
57687 | 20 |
lemma finite_set_tree[simp]: "finite(set_tree t)" |
21 |
by(induction t) auto |
|
22 |
||
23 |
||
24 |
subsection "The set of subtrees" |
|
25 |
||
57250 | 26 |
fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where |
58424 | 27 |
"subtrees \<langle>\<rangle> = {\<langle>\<rangle>}" | |
28 |
"subtrees (\<langle>l, a, r\<rangle>) = insert \<langle>l, a, r\<rangle> (subtrees l \<union> subtrees r)" |
|
57250 | 29 |
|
58424 | 30 |
lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. \<langle>l, a, r\<rangle> \<in> subtrees t" |
31 |
by (induction t)(auto) |
|
57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset
|
32 |
|
57450 | 33 |
lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t" |
58424 | 34 |
by (induction t) auto |
57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset
|
35 |
|
58424 | 36 |
lemma in_set_tree_if: "\<langle>l, a, r\<rangle> \<in> subtrees t \<Longrightarrow> a \<in> set_tree t" |
37 |
by (metis Node_notin_subtrees_if) |
|
57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset
|
38 |
|
57687 | 39 |
|
40 |
subsection "Inorder list of entries" |
|
41 |
||
57250 | 42 |
fun inorder :: "'a tree \<Rightarrow> 'a list" where |
58424 | 43 |
"inorder \<langle>\<rangle> = []" | |
44 |
"inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r" |
|
57250 | 45 |
|
57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset
|
46 |
lemma set_inorder[simp]: "set (inorder t) = set_tree t" |
58424 | 47 |
by (induction t) auto |
57250 | 48 |
|
57687 | 49 |
|
57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset
|
50 |
subsection {* Binary Search Tree predicate *} |
57250 | 51 |
|
57450 | 52 |
fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where |
58424 | 53 |
"bst \<langle>\<rangle> \<longleftrightarrow> True" | |
54 |
"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 | 55 |
|
57450 | 56 |
lemma (in linorder) bst_imp_sorted: "bst t \<Longrightarrow> sorted (inorder t)" |
58424 | 57 |
by (induction t) (auto simp: sorted_append sorted_Cons intro: less_imp_le less_trans) |
57250 | 58 |
|
57687 | 59 |
|
60 |
subsection "Deletion of the rightmost entry" |
|
61 |
||
62 |
fun del_rightmost :: "'a tree \<Rightarrow> 'a tree * 'a" where |
|
58424 | 63 |
"del_rightmost \<langle>l, a, \<langle>\<rangle>\<rangle> = (l,a)" | |
64 |
"del_rightmost \<langle>l, a, r\<rangle> = (let (r',x) = del_rightmost r in (\<langle>l, a, r'\<rangle>, x))" |
|
57687 | 65 |
|
66 |
lemma del_rightmost_set_tree_if_bst: |
|
67 |
"\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> Leaf \<rbrakk> |
|
68 |
\<Longrightarrow> x \<in> set_tree t \<and> set_tree t' = set_tree t - {x}" |
|
69 |
apply(induction t arbitrary: t' rule: del_rightmost.induct) |
|
70 |
apply (fastforce simp: ball_Un split: prod.splits)+ |
|
71 |
done |
|
72 |
||
73 |
lemma del_rightmost_set_tree: |
|
58424 | 74 |
"\<lbrakk> del_rightmost t = (t',x); t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> set_tree t = insert x (set_tree t')" |
57687 | 75 |
apply(induction t arbitrary: t' rule: del_rightmost.induct) |
76 |
by (auto split: prod.splits) auto |
|
77 |
||
78 |
lemma del_rightmost_bst: |
|
58424 | 79 |
"\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> bst t'" |
57687 | 80 |
proof(induction t arbitrary: t' rule: del_rightmost.induct) |
81 |
case (2 l a rl b rr) |
|
82 |
let ?r = "Node rl b rr" |
|
83 |
from "2.prems"(1) obtain r' where 1: "del_rightmost ?r = (r',x)" and [simp]: "t' = Node l a r'" |
|
84 |
by(simp split: prod.splits) |
|
85 |
from "2.prems"(2) 1 del_rightmost_set_tree[OF 1] show ?case by(auto)(simp add: "2.IH") |
|
86 |
qed auto |
|
87 |
||
88 |
||
58424 | 89 |
lemma del_rightmost_greater: "\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> \<langle>\<rangle> \<rbrakk> |
57687 | 90 |
\<Longrightarrow> \<forall>a\<in>set_tree t'. a < x" |
91 |
proof(induction t arbitrary: t' rule: del_rightmost.induct) |
|
92 |
case (2 l a rl b rr) |
|
93 |
from "2.prems"(1) obtain r' |
|
94 |
where dm: "del_rightmost (Node rl b rr) = (r',x)" and [simp]: "t' = Node l a r'" |
|
95 |
by(simp split: prod.splits) |
|
96 |
show ?case using "2.prems"(2) "2.IH"[OF dm] del_rightmost_set_tree_if_bst[OF dm] |
|
97 |
by (fastforce simp add: ball_Un) |
|
98 |
qed simp_all |
|
99 |
||
100 |
(* should be moved but metis not available in much of Main *) |
|
101 |
lemma Max_insert1: "\<lbrakk> finite A; \<forall>a\<in>A. a \<le> x \<rbrakk> \<Longrightarrow> Max(insert x A) = x" |
|
102 |
by (metis Max_in Max_insert Max_singleton antisym max_def) |
|
103 |
||
104 |
lemma del_rightmost_Max: |
|
58424 | 105 |
"\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> x = Max(set_tree t)" |
57687 | 106 |
by (metis Max_insert1 del_rightmost_greater del_rightmost_set_tree finite_set_tree less_le_not_le) |
107 |
||
57250 | 108 |
end |