author  nipkow 
Mon, 23 Mar 2015 07:36:27 +0100  
changeset 59776  f54af3307334 
parent 59561  1a84beaa239b 
child 59928  b9b7f913a19a 
permissions  rwrr 
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 

59776  33 
lemma size_map_tree[simp]: "size (map_tree f t) = size t" 
34 
by (induction t) auto 

35 

36 
lemma size1_map_tree[simp]: "size1 (map_tree f t) = size1 t" 

37 
by (simp add: size1_def) 

38 

39 

40 
subsection "The depth" 

41 

42 
fun depth :: "'a tree => nat" where 

43 
"depth Leaf = 0"  

44 
"depth (Node t1 a t2) = Suc (max (depth t1) (depth t2))" 

45 

46 
lemma depth_map_tree[simp]: "depth (map_tree f t) = depth t" 

47 
by (induction t) auto 

48 

57687  49 

50 
subsection "The set of subtrees" 

51 

57250  52 
fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where 
58424  53 
"subtrees \<langle>\<rangle> = {\<langle>\<rangle>}"  
54 
"subtrees (\<langle>l, a, r\<rangle>) = insert \<langle>l, a, r\<rangle> (subtrees l \<union> subtrees r)" 

57250  55 

58424  56 
lemma set_treeE: "a \<in> set_tree t \<Longrightarrow> \<exists>l r. \<langle>l, a, r\<rangle> \<in> subtrees t" 
57 
by (induction t)(auto) 

57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset

58 

57450  59 
lemma Node_notin_subtrees_if[simp]: "a \<notin> set_tree t \<Longrightarrow> Node l a r \<notin> subtrees t" 
58424  60 
by (induction t) auto 
57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset

61 

58424  62 
lemma in_set_tree_if: "\<langle>l, a, r\<rangle> \<in> subtrees t \<Longrightarrow> a \<in> set_tree t" 
63 
by (metis Node_notin_subtrees_if) 

57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset

64 

57687  65 

59776  66 
subsection "List of entries" 
67 

68 
fun preorder :: "'a tree \<Rightarrow> 'a list" where 

69 
"preorder \<langle>\<rangle> = []"  

70 
"preorder \<langle>l, x, r\<rangle> = x # preorder l @ preorder r" 

57687  71 

57250  72 
fun inorder :: "'a tree \<Rightarrow> 'a list" where 
58424  73 
"inorder \<langle>\<rangle> = []"  
74 
"inorder \<langle>l, x, r\<rangle> = inorder l @ [x] @ inorder r" 

57250  75 

57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset

76 
lemma set_inorder[simp]: "set (inorder t) = set_tree t" 
58424  77 
by (induction t) auto 
57250  78 

59776  79 
lemma set_preorder[simp]: "set (preorder t) = set_tree t" 
80 
by (induction t) auto 

81 

82 
lemma length_preorder[simp]: "length (preorder t) = size t" 

83 
by (induction t) auto 

84 

85 
lemma length_inorder[simp]: "length (inorder t) = size t" 

86 
by (induction t) auto 

87 

88 
lemma preorder_map: "preorder (map_tree f t) = map f (preorder t)" 

89 
by (induction t) auto 

90 

91 
lemma inorder_map: "inorder (map_tree f t) = map f (inorder t)" 

92 
by (induction t) auto 

93 

57687  94 

57449
f81da03b9ebd
Library/Tree: use datatype_new, bst is an inductive predicate
hoelzl
parents:
57250
diff
changeset

95 
subsection {* Binary Search Tree predicate *} 
57250  96 

57450  97 
fun (in linorder) bst :: "'a tree \<Rightarrow> bool" where 
58424  98 
"bst \<langle>\<rangle> \<longleftrightarrow> True"  
99 
"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  100 

57450  101 
lemma (in linorder) bst_imp_sorted: "bst t \<Longrightarrow> sorted (inorder t)" 
58424  102 
by (induction t) (auto simp: sorted_append sorted_Cons intro: less_imp_le less_trans) 
57250  103 

59561  104 
text{* In case there are duplicates: *} 
105 

106 
fun (in linorder) bst_eq :: "'a tree \<Rightarrow> bool" where 

107 
"bst_eq \<langle>\<rangle> \<longleftrightarrow> True"  

108 
"bst_eq \<langle>l,a,r\<rangle> \<longleftrightarrow> 

109 
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)" 

110 

111 
lemma (in linorder) bst_eq_imp_sorted: "bst_eq t \<Longrightarrow> sorted (inorder t)" 

112 
apply (induction t) 

113 
apply(simp) 

114 
by (fastforce simp: sorted_append sorted_Cons intro: less_imp_le less_trans) 

115 

59776  116 

59561  117 
subsection "Function @{text mirror}" 
118 

119 
fun mirror :: "'a tree \<Rightarrow> 'a tree" where 

120 
"mirror \<langle>\<rangle> = Leaf"  

121 
"mirror \<langle>l,x,r\<rangle> = \<langle>mirror r, x, mirror l\<rangle>" 

122 

123 
lemma mirror_Leaf[simp]: "mirror t = \<langle>\<rangle> \<longleftrightarrow> t = \<langle>\<rangle>" 

124 
by (induction t) simp_all 

125 

126 
lemma size_mirror[simp]: "size(mirror t) = size t" 

127 
by (induction t) simp_all 

128 

129 
lemma size1_mirror[simp]: "size1(mirror t) = size1 t" 

130 
by (simp add: size1_def) 

131 

59776  132 
lemma depth_mirror[simp]: "depth(mirror t) = depth t" 
133 
by (induction t) simp_all 

134 

135 
lemma inorder_mirror: "inorder(mirror t) = rev(inorder t)" 

136 
by (induction t) simp_all 

137 

138 
lemma map_mirror: "map_tree f (mirror t) = mirror (map_tree f t)" 

139 
by (induction t) simp_all 

140 

59561  141 
lemma mirror_mirror[simp]: "mirror(mirror t) = t" 
142 
by (induction t) simp_all 

143 

57687  144 

145 
subsection "Deletion of the rightmost entry" 

146 

147 
fun del_rightmost :: "'a tree \<Rightarrow> 'a tree * 'a" where 

58424  148 
"del_rightmost \<langle>l, a, \<langle>\<rangle>\<rangle> = (l,a)"  
149 
"del_rightmost \<langle>l, a, r\<rangle> = (let (r',x) = del_rightmost r in (\<langle>l, a, r'\<rangle>, x))" 

57687  150 

151 
lemma del_rightmost_set_tree_if_bst: 

152 
"\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> Leaf \<rbrakk> 

153 
\<Longrightarrow> x \<in> set_tree t \<and> set_tree t' = set_tree t  {x}" 

154 
apply(induction t arbitrary: t' rule: del_rightmost.induct) 

155 
apply (fastforce simp: ball_Un split: prod.splits)+ 

156 
done 

157 

158 
lemma del_rightmost_set_tree: 

58424  159 
"\<lbrakk> del_rightmost t = (t',x); t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> set_tree t = insert x (set_tree t')" 
57687  160 
apply(induction t arbitrary: t' rule: del_rightmost.induct) 
161 
by (auto split: prod.splits) auto 

162 

163 
lemma del_rightmost_bst: 

58424  164 
"\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> bst t'" 
57687  165 
proof(induction t arbitrary: t' rule: del_rightmost.induct) 
166 
case (2 l a rl b rr) 

167 
let ?r = "Node rl b rr" 

168 
from "2.prems"(1) obtain r' where 1: "del_rightmost ?r = (r',x)" and [simp]: "t' = Node l a r'" 

169 
by(simp split: prod.splits) 

170 
from "2.prems"(2) 1 del_rightmost_set_tree[OF 1] show ?case by(auto)(simp add: "2.IH") 

171 
qed auto 

172 

173 

58424  174 
lemma del_rightmost_greater: "\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> \<langle>\<rangle> \<rbrakk> 
57687  175 
\<Longrightarrow> \<forall>a\<in>set_tree t'. a < x" 
176 
proof(induction t arbitrary: t' rule: del_rightmost.induct) 

177 
case (2 l a rl b rr) 

178 
from "2.prems"(1) obtain r' 

179 
where dm: "del_rightmost (Node rl b rr) = (r',x)" and [simp]: "t' = Node l a r'" 

180 
by(simp split: prod.splits) 

181 
show ?case using "2.prems"(2) "2.IH"[OF dm] del_rightmost_set_tree_if_bst[OF dm] 

182 
by (fastforce simp add: ball_Un) 

183 
qed simp_all 

184 

185 
lemma del_rightmost_Max: 

58424  186 
"\<lbrakk> del_rightmost t = (t',x); bst t; t \<noteq> \<langle>\<rangle> \<rbrakk> \<Longrightarrow> x = Max(set_tree t)" 
58467  187 
by (metis Max_insert2 del_rightmost_greater del_rightmost_set_tree finite_set_tree less_le_not_le) 
57687  188 

57250  189 
end 