author  nipkow 
Wed, 17 Jun 2015 20:21:40 +0200  
changeset 60505  9e6584184315 
parent 59928  b9b7f913a19a 
child 60506  83231b558ce4 
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 

60505  27 
lemma size_0_iff_Leaf[simp]: "size t = 0 \<longleftrightarrow> t = Leaf" 
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 

43 
subsection "The depth" 

44 

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

46 
"depth Leaf = 0"  

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

48 

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

50 
by (induction t) auto 

51 

57687  52 

53 
subsection "The set of subtrees" 

54 

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

57250  58 

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

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

61 

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

64 

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

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

67 

57687  68 

59776  69 
subsection "List of entries" 
70 

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

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

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

57687  74 

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

57250  78 

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

79 
lemma set_inorder[simp]: "set (inorder t) = set_tree t" 
58424  80 
by (induction t) auto 
57250  81 

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

84 

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

86 
by (induction t) auto 

87 

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

89 
by (induction t) auto 

90 

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

92 
by (induction t) auto 

93 

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

95 
by (induction t) auto 

96 

57687  97 

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

98 
subsection {* Binary Search Tree predicate *} 
57250  99 

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

59928  111 
lemma (in linorder) bst_eq_if_bst: "bst t \<Longrightarrow> bst_eq t" 
112 
by (induction t) (auto) 

113 

59561  114 
lemma (in linorder) bst_eq_imp_sorted: "bst_eq t \<Longrightarrow> sorted (inorder t)" 
115 
apply (induction t) 

116 
apply(simp) 

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

118 

59928  119 
lemma (in linorder) distinct_preorder_if_bst: "bst t \<Longrightarrow> distinct (preorder t)" 
120 
apply (induction t) 

121 
apply simp 

122 
apply(fastforce elim: order.asym) 

123 
done 

124 

125 
lemma (in linorder) distinct_inorder_if_bst: "bst t \<Longrightarrow> distinct (inorder t)" 

126 
apply (induction t) 

127 
apply simp 

128 
apply(fastforce elim: order.asym) 

129 
done 

130 

59776  131 

60505  132 
subsection "The heap predicate" 
133 

134 
fun heap :: "'a::linorder tree \<Rightarrow> bool" where 

135 
"heap Leaf = True"  

136 
"heap (Node l m r) = 

137 
(heap l \<and> heap r \<and> (\<forall>x \<in> set_tree l \<union> set_tree r. m \<le> x))" 

138 

139 

59561  140 
subsection "Function @{text mirror}" 
141 

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

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

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

145 

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

147 
by (induction t) simp_all 

148 

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

150 
by (induction t) simp_all 

151 

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

153 
by (simp add: size1_def) 

154 

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

157 

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

159 
by (induction t) simp_all 

160 

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

162 
by (induction t) simp_all 

163 

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

166 

57687  167 

168 
subsection "Deletion of the rightmost entry" 

169 

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

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

57687  173 

174 
lemma del_rightmost_set_tree_if_bst: 

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

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

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

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

179 
done 

180 

181 
lemma del_rightmost_set_tree: 

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

185 

186 
lemma del_rightmost_bst: 

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

190 
let ?r = "Node rl b rr" 

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

192 
by(simp split: prod.splits) 

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

194 
qed auto 

195 

196 

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

200 
case (2 l a rl b rr) 

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

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

203 
by(simp split: prod.splits) 

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

205 
by (fastforce simp add: ball_Un) 

206 
qed simp_all 

207 

208 
lemma del_rightmost_Max: 

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

57250  212 
end 