author  nipkow 
Mon, 06 Apr 2015 15:23:50 +0200  
changeset 59928  b9b7f913a19a 
parent 59776  f54af3307334 
child 60500  903bb1495239 
child 60505  9e6584184315 
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 

59561  101 
text{* In case there are duplicates: *} 
102 

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

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

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

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

107 

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

110 

59561  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 

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

118 
apply simp 

119 
apply(fastforce elim: order.asym) 

120 
done 

121 

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

123 
apply (induction t) 

124 
apply simp 

125 
apply(fastforce elim: order.asym) 

126 
done 

127 

59776  128 

59561  129 
subsection "Function @{text mirror}" 
130 

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

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

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

134 

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

136 
by (induction t) simp_all 

137 

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

139 
by (induction t) simp_all 

140 

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

142 
by (simp add: size1_def) 

143 

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

146 

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

148 
by (induction t) simp_all 

149 

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

151 
by (induction t) simp_all 

152 

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

155 

57687  156 

157 
subsection "Deletion of the rightmost entry" 

158 

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

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

57687  162 

163 
lemma del_rightmost_set_tree_if_bst: 

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

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

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

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

168 
done 

169 

170 
lemma del_rightmost_set_tree: 

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

174 

175 
lemma del_rightmost_bst: 

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

179 
let ?r = "Node rl b rr" 

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

181 
by(simp split: prod.splits) 

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

183 
qed auto 

184 

185 

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

189 
case (2 l a rl b rr) 

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

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

192 
by(simp split: prod.splits) 

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

194 
by (fastforce simp add: ball_Un) 

195 
qed simp_all 

196 

197 
lemma del_rightmost_Max: 

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

57250  201 
end 