author  nipkow 
Fri, 18 Mar 2016 10:14:56 +0100  
changeset 62650  7e6bb43e7217 
parent 62202  e5bc7cbb0bcc 
child 63036  1ba3aacfa4d3 
permissions  rwrr 
57250  1 
(* Author: Tobias Nipkow *) 
2 

60500  3 
section \<open>Binary Tree\<close> 
57250  4 

5 
theory Tree 

6 
imports Main 

7 
begin 

8 

58424  9 
datatype 'a tree = 
62160  10 
is_Leaf: Leaf ("\<langle>\<rangle>")  
11 
Node (left: "'a tree") (val: 'a) (right: "'a tree") ("(1\<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 

60500  17 
text\<open>Can be seen as counting the number of leaves rather than nodes:\<close> 
58438  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 

62650  27 
lemma size1_ge0[simp]: "0 < size1 t" 
28 
by (simp add: size1_def) 

29 

60507  30 
lemma size_0_iff_Leaf: "size t = 0 \<longleftrightarrow> t = Leaf" 
60505  31 
by(cases t) auto 
32 

58424  33 
lemma neq_Leaf_iff: "(t \<noteq> \<langle>\<rangle>) = (\<exists>l a r. t = \<langle>l, a, r\<rangle>)" 
34 
by (cases t) auto 

57530  35 

57687  36 
lemma finite_set_tree[simp]: "finite(set_tree t)" 
37 
by(induction t) auto 

38 

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

41 

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

43 
by (simp add: size1_def) 

44 

45 

60808
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

46 
subsection "The Height" 
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

47 

fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

48 
class height = fixes height :: "'a \<Rightarrow> nat" 
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

49 

fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

50 
instantiation tree :: (type)height 
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

51 
begin 
59776  52 

60808
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

53 
fun height_tree :: "'a tree => nat" where 
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

54 
"height Leaf = 0"  
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

55 
"height (Node t1 a t2) = max (height t1) (height t2) + 1" 
59776  56 

60808
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

57 
instance .. 
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

58 

fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

59 
end 
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

60 

fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

61 
lemma height_map_tree[simp]: "height (map_tree f t) = height t" 
59776  62 
by (induction t) auto 
63 

62202  64 
lemma size1_height: "size t + 1 \<le> 2 ^ height (t::'a tree)" 
65 
proof(induction t) 

66 
case (Node l a r) 

67 
show ?case 

68 
proof (cases "height l \<le> height r") 

69 
case True 

70 
have "size(Node l a r) + 1 = (size l + 1) + (size r + 1)" by simp 

71 
also have "size l + 1 \<le> 2 ^ height l" by(rule Node.IH(1)) 

72 
also have "size r + 1 \<le> 2 ^ height r" by(rule Node.IH(2)) 

73 
also have "(2::nat) ^ height l \<le> 2 ^ height r" using True by simp 

74 
finally show ?thesis using True by (auto simp: max_def mult_2) 

75 
next 

76 
case False 

77 
have "size(Node l a r) + 1 = (size l + 1) + (size r + 1)" by simp 

78 
also have "size l + 1 \<le> 2 ^ height l" by(rule Node.IH(1)) 

79 
also have "size r + 1 \<le> 2 ^ height r" by(rule Node.IH(2)) 

80 
also have "(2::nat) ^ height r \<le> 2 ^ height l" using False by simp 

81 
finally show ?thesis using False by (auto simp: max_def mult_2) 

82 
qed 

83 
qed simp 

84 

57687  85 

86 
subsection "The set of subtrees" 

87 

57250  88 
fun subtrees :: "'a tree \<Rightarrow> 'a tree set" where 
60808
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

89 
"subtrees \<langle>\<rangle> = {\<langle>\<rangle>}"  
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

90 
"subtrees (\<langle>l, a, r\<rangle>) = insert \<langle>l, a, r\<rangle> (subtrees l \<union> subtrees r)" 
57250  91 

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

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

94 

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

97 

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

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

100 

57687  101 

59776  102 
subsection "List of entries" 
103 

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

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

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

57687  107 

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

57250  111 

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

112 
lemma set_inorder[simp]: "set (inorder t) = set_tree t" 
58424  113 
by (induction t) auto 
57250  114 

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

117 

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

119 
by (induction t) auto 

120 

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

122 
by (induction t) auto 

123 

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

125 
by (induction t) auto 

126 

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

128 
by (induction t) auto 

129 

57687  130 

60500  131 
subsection \<open>Binary Search Tree predicate\<close> 
57250  132 

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

60500  137 
text\<open>In case there are duplicates:\<close> 
59561  138 

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

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

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

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

143 

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

146 

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

149 
apply(simp) 

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

151 

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

154 
apply simp 

155 
apply(fastforce elim: order.asym) 

156 
done 

157 

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

159 
apply (induction t) 

160 
apply simp 

161 
apply(fastforce elim: order.asym) 

162 
done 

163 

59776  164 

60505  165 
subsection "The heap predicate" 
166 

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

168 
"heap Leaf = True"  

169 
"heap (Node l m r) = 

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

171 

172 

61585  173 
subsection "Function \<open>mirror\<close>" 
59561  174 

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

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

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

178 

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

180 
by (induction t) simp_all 

181 

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

183 
by (induction t) simp_all 

184 

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

186 
by (simp add: size1_def) 

187 

60808
fd26519b1a6a
depth > height; removed del_rightmost (too specifi)
nipkow
parents:
60507
diff
changeset

188 
lemma height_mirror[simp]: "height(mirror t) = height t" 
59776  189 
by (induction t) simp_all 
190 

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

192 
by (induction t) simp_all 

193 

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

195 
by (induction t) simp_all 

196 

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

199 

57250  200 
end 