61515
|
1 |
(* Author: Tobias Nipkow *)
|
|
2 |
|
|
3 |
section \<open>A 2-3-4 Tree Implementation of Sets\<close>
|
|
4 |
|
|
5 |
theory Tree234_Set
|
|
6 |
imports
|
|
7 |
Tree234
|
|
8 |
"../Data_Structures/Set_by_Ordered"
|
|
9 |
begin
|
|
10 |
|
|
11 |
subsection \<open>Set operations on 2-3-4 trees\<close>
|
|
12 |
|
|
13 |
fun isin :: "'a::linorder tree234 \<Rightarrow> 'a \<Rightarrow> bool" where
|
|
14 |
"isin Leaf x = False" |
|
|
15 |
"isin (Node2 l a r) x = (x < a \<and> isin l x \<or> x=a \<or> isin r x)" |
|
|
16 |
"isin (Node3 l a m b r) x =
|
|
17 |
(x < a \<and> isin l x \<or> x = a \<or> x < b \<and> isin m x \<or> x = b \<or> isin r x)" |
|
|
18 |
"isin (Node4 l a m b n c r) x =
|
|
19 |
(x < b \<and> (x < a \<and> isin l x \<or> x = a \<or> isin m x) \<or> x = b \<or>
|
|
20 |
x > b \<and> (x < c \<and> isin n x \<or> x=c \<or> isin r x))"
|
|
21 |
|
|
22 |
datatype 'a up\<^sub>i = T\<^sub>i "'a tree234" | Up\<^sub>i "'a tree234" 'a "'a tree234"
|
|
23 |
|
|
24 |
fun tree\<^sub>i :: "'a up\<^sub>i \<Rightarrow> 'a tree234" where
|
|
25 |
"tree\<^sub>i (T\<^sub>i t) = t" |
|
|
26 |
"tree\<^sub>i (Up\<^sub>i l p r) = Node2 l p r"
|
|
27 |
|
|
28 |
fun ins :: "'a::linorder \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>i" where
|
|
29 |
"ins a Leaf = Up\<^sub>i Leaf a Leaf" |
|
|
30 |
"ins a (Node2 l x r) =
|
|
31 |
(if a < x then
|
|
32 |
(case ins a l of
|
|
33 |
T\<^sub>i l' => T\<^sub>i (Node2 l' x r)
|
|
34 |
| Up\<^sub>i l1 q l2 => T\<^sub>i (Node3 l1 q l2 x r))
|
|
35 |
else if a=x then T\<^sub>i (Node2 l x r)
|
|
36 |
else
|
|
37 |
(case ins a r of
|
|
38 |
T\<^sub>i r' => T\<^sub>i (Node2 l x r')
|
|
39 |
| Up\<^sub>i r1 q r2 => T\<^sub>i (Node3 l x r1 q r2)))" |
|
|
40 |
"ins a (Node3 l x1 m x2 r) =
|
|
41 |
(if a < x1 then
|
|
42 |
(case ins a l of
|
|
43 |
T\<^sub>i l' => T\<^sub>i (Node3 l' x1 m x2 r)
|
|
44 |
| Up\<^sub>i l1 q l2 => T\<^sub>i (Node4 l1 q l2 x1 m x2 r))
|
|
45 |
else if a=x1 then T\<^sub>i (Node3 l x1 m x2 r)
|
|
46 |
else if a < x2 then
|
|
47 |
(case ins a m of
|
|
48 |
T\<^sub>i m' => T\<^sub>i (Node3 l x1 m' x2 r)
|
|
49 |
| Up\<^sub>i m1 q m2 => T\<^sub>i (Node4 l x1 m1 q m2 x2 r))
|
|
50 |
else if a=x2 then T\<^sub>i (Node3 l x1 m x2 r)
|
|
51 |
else
|
|
52 |
(case ins a r of
|
|
53 |
T\<^sub>i r' => T\<^sub>i (Node3 l x1 m x2 r')
|
|
54 |
| Up\<^sub>i r1 q r2 => T\<^sub>i (Node4 l x1 m x2 r1 q r2)))" |
|
|
55 |
"ins a (Node4 l x1 m x2 n x3 r) =
|
|
56 |
(if a < x2 then
|
|
57 |
if a < x1 then
|
|
58 |
(case ins a l of
|
|
59 |
T\<^sub>i l' => T\<^sub>i (Node4 l' x1 m x2 n x3 r)
|
|
60 |
| Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) x1 (Node3 m x2 n x3 r))
|
|
61 |
else if a=x1 then T\<^sub>i (Node4 l x1 m x2 n x3 r)
|
|
62 |
else (case ins a m of
|
|
63 |
T\<^sub>i m' => T\<^sub>i (Node4 l x1 m' x2 n x3 r)
|
|
64 |
| Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l x1 m1) q (Node3 m2 x2 n x3 r))
|
|
65 |
else if a=x2 then T\<^sub>i (Node4 l x1 m x2 n x3 r)
|
|
66 |
else if a < x3 then
|
|
67 |
(case ins a n of
|
|
68 |
T\<^sub>i n' => T\<^sub>i (Node4 l x1 m x2 n' x3 r)
|
|
69 |
| Up\<^sub>i n1 q n2 => Up\<^sub>i (Node2 l x1 m) x2 (Node3 n1 q n2 x3 r))
|
|
70 |
else if a=x3 then T\<^sub>i (Node4 l x1 m x2 n x3 r)
|
|
71 |
else (case ins a r of
|
|
72 |
T\<^sub>i r' => T\<^sub>i (Node4 l x1 m x2 n x3 r')
|
|
73 |
| Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l x1 m) x2 (Node3 n x3 r1 q r2))
|
|
74 |
)"
|
|
75 |
|
|
76 |
hide_const insert
|
|
77 |
|
|
78 |
definition insert :: "'a::linorder \<Rightarrow> 'a tree234 \<Rightarrow> 'a tree234" where
|
|
79 |
"insert a t = tree\<^sub>i(ins a t)"
|
|
80 |
|
|
81 |
datatype 'a up\<^sub>d = T\<^sub>d "'a tree234" | Up\<^sub>d "'a tree234"
|
|
82 |
|
|
83 |
fun tree\<^sub>d :: "'a up\<^sub>d \<Rightarrow> 'a tree234" where
|
|
84 |
"tree\<^sub>d (T\<^sub>d x) = x" |
|
|
85 |
"tree\<^sub>d (Up\<^sub>d x) = x"
|
|
86 |
|
|
87 |
fun node21 :: "'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
|
|
88 |
"node21 (T\<^sub>d l) a r = T\<^sub>d(Node2 l a r)" |
|
|
89 |
"node21 (Up\<^sub>d l) a (Node2 lr b rr) = Up\<^sub>d(Node3 l a lr b rr)" |
|
|
90 |
"node21 (Up\<^sub>d l) a (Node3 lr b mr c rr) = T\<^sub>d(Node2 (Node2 l a lr) b (Node2 mr c rr))" |
|
|
91 |
"node21 (Up\<^sub>d t1) a (Node4 t2 b t3 c t4 d t5) = T\<^sub>d(Node2 (Node2 t1 a t2) b (Node3 t3 c t4 d t5))"
|
|
92 |
|
|
93 |
fun node22 :: "'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a up\<^sub>d" where
|
|
94 |
"node22 l a (T\<^sub>d r) = T\<^sub>d(Node2 l a r)" |
|
|
95 |
"node22 (Node2 ll b rl) a (Up\<^sub>d r) = Up\<^sub>d(Node3 ll b rl a r)" |
|
|
96 |
"node22 (Node3 ll b ml c rl) a (Up\<^sub>d r) = T\<^sub>d(Node2 (Node2 ll b ml) c (Node2 rl a r))" |
|
|
97 |
"node22 (Node4 t1 a t2 b t3 c t4) d (Up\<^sub>d t5) = T\<^sub>d(Node2 (Node2 t1 a t2) b (Node3 t3 c t4 d t5))"
|
|
98 |
|
|
99 |
fun node31 :: "'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
|
|
100 |
"node31 (T\<^sub>d t1) a t2 b t3 = T\<^sub>d(Node3 t1 a t2 b t3)" |
|
|
101 |
"node31 (Up\<^sub>d t1) a (Node2 t2 b t3) c t4 = T\<^sub>d(Node2 (Node3 t1 a t2 b t3) c t4)" |
|
|
102 |
"node31 (Up\<^sub>d t1) a (Node3 t2 b t3 c t4) d t5 = T\<^sub>d(Node3 (Node2 t1 a t2) b (Node2 t3 c t4) d t5)" |
|
|
103 |
"node31 (Up\<^sub>d t1) a (Node4 t2 b t3 c t4 d t5) e t6 = T\<^sub>d(Node3 (Node2 t1 a t2) b (Node3 t3 c t4 d t5) e t6)"
|
|
104 |
|
|
105 |
fun node32 :: "'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
|
|
106 |
"node32 t1 a (T\<^sub>d t2) b t3 = T\<^sub>d(Node3 t1 a t2 b t3)" |
|
|
107 |
"node32 t1 a (Up\<^sub>d t2) b (Node2 t3 c t4) = T\<^sub>d(Node2 t1 a (Node3 t2 b t3 c t4))" |
|
|
108 |
"node32 t1 a (Up\<^sub>d t2) b (Node3 t3 c t4 d t5) = T\<^sub>d(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))" |
|
|
109 |
"node32 t1 a (Up\<^sub>d t2) b (Node4 t3 c t4 d t5 e t6) = T\<^sub>d(Node3 t1 a (Node2 t2 b t3) c (Node3 t4 d t5 e t6))"
|
|
110 |
|
|
111 |
fun node33 :: "'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a up\<^sub>d" where
|
|
112 |
"node33 l a m b (T\<^sub>d r) = T\<^sub>d(Node3 l a m b r)" |
|
|
113 |
"node33 t1 a (Node2 t2 b t3) c (Up\<^sub>d t4) = T\<^sub>d(Node2 t1 a (Node3 t2 b t3 c t4))" |
|
|
114 |
"node33 t1 a (Node3 t2 b t3 c t4) d (Up\<^sub>d t5) = T\<^sub>d(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))" |
|
|
115 |
"node33 t1 a (Node4 t2 b t3 c t4 d t5) e (Up\<^sub>d t6) = T\<^sub>d(Node3 t1 a (Node2 t2 b t3) c (Node3 t4 d t5 e t6))"
|
|
116 |
|
|
117 |
fun node41 :: "'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
|
|
118 |
"node41 (T\<^sub>d t1) a t2 b t3 c t4 = T\<^sub>d(Node4 t1 a t2 b t3 c t4)" |
|
|
119 |
"node41 (Up\<^sub>d t1) a (Node2 t2 b t3) c t4 d t5 = T\<^sub>d(Node3 (Node3 t1 a t2 b t3) c t4 d t5)" |
|
|
120 |
"node41 (Up\<^sub>d t1) a (Node3 t2 b t3 c t4) d t5 e t6 = T\<^sub>d(Node4 (Node2 t1 a t2) b (Node2 t3 c t4) d t5 e t6)" |
|
|
121 |
"node41 (Up\<^sub>d t1) a (Node4 t2 b t3 c t4 d t5) e t6 f t7 = T\<^sub>d(Node4 (Node2 t1 a t2) b (Node3 t3 c t4 d t5) e t6 f t7)"
|
|
122 |
|
|
123 |
fun node42 :: "'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
|
|
124 |
"node42 t1 a (T\<^sub>d t2) b t3 c t4 = T\<^sub>d(Node4 t1 a t2 b t3 c t4)" |
|
|
125 |
"node42 (Node2 t1 a t2) b (Up\<^sub>d t3) c t4 d t5 = T\<^sub>d(Node3 (Node3 t1 a t2 b t3) c t4 d t5)" |
|
|
126 |
"node42 (Node3 t1 a t2 b t3) c (Up\<^sub>d t4) d t5 e t6 = T\<^sub>d(Node4 (Node2 t1 a t2) b (Node2 t3 c t4) d t5 e t6)" |
|
|
127 |
"node42 (Node4 t1 a t2 b t3 c t4) d (Up\<^sub>d t5) e t6 f t7 = T\<^sub>d(Node4 (Node2 t1 a t2) b (Node3 t3 c t4 d t5) e t6 f t7)"
|
|
128 |
|
|
129 |
fun node43 :: "'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
|
|
130 |
"node43 t1 a t2 b (T\<^sub>d t3) c t4 = T\<^sub>d(Node4 t1 a t2 b t3 c t4)" |
|
|
131 |
"node43 t1 a (Node2 t2 b t3) c (Up\<^sub>d t4) d t5 = T\<^sub>d(Node3 t1 a (Node3 t2 b t3 c t4) d t5)" |
|
|
132 |
"node43 t1 a (Node3 t2 b t3 c t4) d (Up\<^sub>d t5) e t6 = T\<^sub>d(Node4 t1 a (Node2 t2 b t3) c (Node2 t4 d t5) e t6)" |
|
|
133 |
"node43 t1 a (Node4 t2 b t3 c t4 d t5) e (Up\<^sub>d t6) f t7 = T\<^sub>d(Node4 t1 a (Node2 t2 b t3) c (Node3 t4 d t5 e t6) f t7)"
|
|
134 |
|
|
135 |
fun node44 :: "'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a tree234 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a up\<^sub>d" where
|
|
136 |
"node44 t1 a t2 b t3 c (T\<^sub>d t4) = T\<^sub>d(Node4 t1 a t2 b t3 c t4)" |
|
|
137 |
"node44 t1 a t2 b (Node2 t3 c t4) d (Up\<^sub>d t5) = T\<^sub>d(Node3 t1 a t2 b (Node3 t3 c t4 d t5))" |
|
|
138 |
"node44 t1 a t2 b (Node3 t3 c t4 d t5) e (Up\<^sub>d t6) = T\<^sub>d(Node4 t1 a t2 b (Node2 t3 c t4) d (Node2 t5 e t6))" |
|
|
139 |
"node44 t1 a t2 b (Node4 t3 c t4 d t5 e t6) f (Up\<^sub>d t7) = T\<^sub>d(Node4 t1 a t2 b (Node2 t3 c t4) d (Node3 t5 e t6 f t7))"
|
|
140 |
|
|
141 |
fun del_min :: "'a tree234 \<Rightarrow> 'a * 'a up\<^sub>d" where
|
|
142 |
"del_min (Node2 Leaf a Leaf) = (a, Up\<^sub>d Leaf)" |
|
|
143 |
"del_min (Node3 Leaf a Leaf b Leaf) = (a, T\<^sub>d(Node2 Leaf b Leaf))" |
|
|
144 |
"del_min (Node4 Leaf a Leaf b Leaf c Leaf) = (a, T\<^sub>d(Node3 Leaf b Leaf c Leaf))" |
|
|
145 |
"del_min (Node2 l a r) = (let (x,l') = del_min l in (x, node21 l' a r))" |
|
|
146 |
"del_min (Node3 l a m b r) = (let (x,l') = del_min l in (x, node31 l' a m b r))" |
|
|
147 |
"del_min (Node4 l a m b n c r) = (let (x,l') = del_min l in (x, node41 l' a m b n c r))"
|
|
148 |
|
|
149 |
fun del :: "'a::linorder \<Rightarrow> 'a tree234 \<Rightarrow> 'a up\<^sub>d" where
|
|
150 |
"del k Leaf = T\<^sub>d Leaf" |
|
|
151 |
"del k (Node2 Leaf p Leaf) = (if k=p then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf p Leaf))" |
|
|
152 |
"del k (Node3 Leaf p Leaf q Leaf) = T\<^sub>d(if k=p then Node2 Leaf q Leaf
|
|
153 |
else if k=q then Node2 Leaf p Leaf else Node3 Leaf p Leaf q Leaf)" |
|
|
154 |
"del k (Node4 Leaf a Leaf b Leaf c Leaf) =
|
|
155 |
T\<^sub>d(if k=a then Node3 Leaf b Leaf c Leaf else
|
|
156 |
if k=b then Node3 Leaf a Leaf c Leaf else
|
|
157 |
if k=c then Node3 Leaf a Leaf b Leaf
|
|
158 |
else Node4 Leaf a Leaf b Leaf c Leaf)" |
|
|
159 |
"del k (Node2 l a r) = (if k<a then node21 (del k l) a r else
|
|
160 |
if k > a then node22 l a (del k r) else
|
|
161 |
let (a',t) = del_min r in node22 l a' t)" |
|
|
162 |
"del k (Node3 l a m b r) = (if k<a then node31 (del k l) a m b r else
|
|
163 |
if k = a then let (a',m') = del_min m in node32 l a' m' b r else
|
|
164 |
if k < b then node32 l a (del k m) b r else
|
|
165 |
if k = b then let (b',r') = del_min r in node33 l a m b' r'
|
|
166 |
else node33 l a m b (del k r))" |
|
|
167 |
"del k (Node4 l a m b n c r) =
|
|
168 |
(if k < b then
|
|
169 |
if k < a then node41 (del k l) a m b n c r else
|
|
170 |
if k = a then let (a',m') = del_min m in node42 l a' m' b n c r
|
|
171 |
else node42 l a (del k m) b n c r
|
|
172 |
else
|
|
173 |
if k = b then let (b',n') = del_min n in node43 l a m b' n' c r else
|
|
174 |
if k < c then node43 l a m b (del k n) c r else
|
|
175 |
if k = c then let (c',r') = del_min r in node44 l a m b n c' r'
|
|
176 |
else node44 l a m b n c (del k r))"
|
|
177 |
|
|
178 |
definition delete :: "'a::linorder \<Rightarrow> 'a tree234 \<Rightarrow> 'a tree234" where
|
|
179 |
"delete k t = tree\<^sub>d(del k t)"
|
|
180 |
|
|
181 |
|
|
182 |
subsection "Functional correctness"
|
|
183 |
|
|
184 |
|
|
185 |
subsubsection \<open>Functional correctness of isin:\<close>
|
|
186 |
|
|
187 |
lemma "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
|
|
188 |
by (induction t) (auto simp: elems_simps1)
|
|
189 |
|
|
190 |
lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))"
|
|
191 |
by (induction t) (auto simp: elems_simps2)
|
|
192 |
|
|
193 |
|
|
194 |
subsubsection \<open>Functional correctness of insert:\<close>
|
|
195 |
|
|
196 |
lemma inorder_ins:
|
|
197 |
"sorted(inorder t) \<Longrightarrow> inorder(tree\<^sub>i(ins x t)) = ins_list x (inorder t)"
|
|
198 |
by(induction t) (auto, auto simp: ins_list_simps split: up\<^sub>i.splits)
|
|
199 |
|
|
200 |
lemma inorder_insert:
|
|
201 |
"sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
|
|
202 |
by(simp add: insert_def inorder_ins)
|
|
203 |
|
|
204 |
|
|
205 |
subsubsection \<open>Functional correctness of delete\<close>
|
|
206 |
|
|
207 |
lemma inorder_node21: "height r > 0 \<Longrightarrow>
|
|
208 |
inorder (tree\<^sub>d (node21 l' a r)) = inorder (tree\<^sub>d l') @ a # inorder r"
|
|
209 |
by(induct l' a r rule: node21.induct) auto
|
|
210 |
|
|
211 |
lemma inorder_node22: "height l > 0 \<Longrightarrow>
|
|
212 |
inorder (tree\<^sub>d (node22 l a r')) = inorder l @ a # inorder (tree\<^sub>d r')"
|
|
213 |
by(induct l a r' rule: node22.induct) auto
|
|
214 |
|
|
215 |
lemma inorder_node31: "height m > 0 \<Longrightarrow>
|
|
216 |
inorder (tree\<^sub>d (node31 l' a m b r)) = inorder (tree\<^sub>d l') @ a # inorder m @ b # inorder r"
|
|
217 |
by(induct l' a m b r rule: node31.induct) auto
|
|
218 |
|
|
219 |
lemma inorder_node32: "height r > 0 \<Longrightarrow>
|
|
220 |
inorder (tree\<^sub>d (node32 l a m' b r)) = inorder l @ a # inorder (tree\<^sub>d m') @ b # inorder r"
|
|
221 |
by(induct l a m' b r rule: node32.induct) auto
|
|
222 |
|
|
223 |
lemma inorder_node33: "height m > 0 \<Longrightarrow>
|
|
224 |
inorder (tree\<^sub>d (node33 l a m b r')) = inorder l @ a # inorder m @ b # inorder (tree\<^sub>d r')"
|
|
225 |
by(induct l a m b r' rule: node33.induct) auto
|
|
226 |
|
|
227 |
lemma inorder_node41: "height m > 0 \<Longrightarrow>
|
|
228 |
inorder (tree\<^sub>d (node41 l' a m b n c r)) = inorder (tree\<^sub>d l') @ a # inorder m @ b # inorder n @ c # inorder r"
|
|
229 |
by(induct l' a m b n c r rule: node41.induct) auto
|
|
230 |
|
|
231 |
lemma inorder_node42: "height l > 0 \<Longrightarrow>
|
|
232 |
inorder (tree\<^sub>d (node42 l a m b n c r)) = inorder l @ a # inorder (tree\<^sub>d m) @ b # inorder n @ c # inorder r"
|
|
233 |
by(induct l a m b n c r rule: node42.induct) auto
|
|
234 |
|
|
235 |
lemma inorder_node43: "height m > 0 \<Longrightarrow>
|
|
236 |
inorder (tree\<^sub>d (node43 l a m b n c r)) = inorder l @ a # inorder m @ b # inorder(tree\<^sub>d n) @ c # inorder r"
|
|
237 |
by(induct l a m b n c r rule: node43.induct) auto
|
|
238 |
|
|
239 |
lemma inorder_node44: "height n > 0 \<Longrightarrow>
|
|
240 |
inorder (tree\<^sub>d (node44 l a m b n c r)) = inorder l @ a # inorder m @ b # inorder n @ c # inorder (tree\<^sub>d r)"
|
|
241 |
by(induct l a m b n c r rule: node44.induct) auto
|
|
242 |
|
|
243 |
lemmas inorder_nodes = inorder_node21 inorder_node22
|
|
244 |
inorder_node31 inorder_node32 inorder_node33
|
|
245 |
inorder_node41 inorder_node42 inorder_node43 inorder_node44
|
|
246 |
|
|
247 |
lemma del_minD:
|
|
248 |
"del_min t = (x,t') \<Longrightarrow> bal t \<Longrightarrow> height t > 0 \<Longrightarrow>
|
|
249 |
x # inorder(tree\<^sub>d t') = inorder t"
|
|
250 |
by(induction t arbitrary: t' rule: del_min.induct)
|
|
251 |
(auto simp: inorder_nodes split: prod.splits)
|
|
252 |
|
|
253 |
lemma inorder_del: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow>
|
|
254 |
inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)"
|
|
255 |
apply(induction t rule: del.induct)
|
|
256 |
apply(simp_all add: del_list_simps inorder_nodes)
|
|
257 |
apply(auto simp: del_list_simps;
|
|
258 |
auto simp: inorder_nodes del_list_simps del_minD split: prod.splits)+
|
|
259 |
(* takes 285 s (2015); the last line alone would do it but takes hours *)
|
|
260 |
done
|
|
261 |
|
|
262 |
lemma inorder_delete: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow>
|
|
263 |
inorder(delete x t) = del_list x (inorder t)"
|
|
264 |
by(simp add: delete_def inorder_del)
|
|
265 |
|
|
266 |
|
|
267 |
subsection \<open>Balancedness\<close>
|
|
268 |
|
|
269 |
subsubsection "Proofs for insert"
|
|
270 |
|
|
271 |
text{* First a standard proof that @{const ins} preserves @{const bal}. *}
|
|
272 |
|
|
273 |
instantiation up\<^sub>i :: (type)height
|
|
274 |
begin
|
|
275 |
|
|
276 |
fun height_up\<^sub>i :: "'a up\<^sub>i \<Rightarrow> nat" where
|
|
277 |
"height (T\<^sub>i t) = height t" |
|
|
278 |
"height (Up\<^sub>i l a r) = height l"
|
|
279 |
|
|
280 |
instance ..
|
|
281 |
|
|
282 |
end
|
|
283 |
|
|
284 |
lemma bal_ins: "bal t \<Longrightarrow> bal (tree\<^sub>i(ins a t)) \<and> height(ins a t) = height t"
|
|
285 |
by (induct t) (auto, auto split: up\<^sub>i.split) (* 29 secs (2015) *)
|
|
286 |
|
|
287 |
|
|
288 |
text{* Now an alternative proof (by Brian Huffman) that runs faster because
|
|
289 |
two properties (balance and height) are combined in one predicate. *}
|
|
290 |
|
|
291 |
inductive full :: "nat \<Rightarrow> 'a tree234 \<Rightarrow> bool" where
|
|
292 |
"full 0 Leaf" |
|
|
293 |
"\<lbrakk>full n l; full n r\<rbrakk> \<Longrightarrow> full (Suc n) (Node2 l p r)" |
|
|
294 |
"\<lbrakk>full n l; full n m; full n r\<rbrakk> \<Longrightarrow> full (Suc n) (Node3 l p m q r)" |
|
|
295 |
"\<lbrakk>full n l; full n m; full n m'; full n r\<rbrakk> \<Longrightarrow> full (Suc n) (Node4 l p m q m' q' r)"
|
|
296 |
|
|
297 |
inductive_cases full_elims:
|
|
298 |
"full n Leaf"
|
|
299 |
"full n (Node2 l p r)"
|
|
300 |
"full n (Node3 l p m q r)"
|
|
301 |
"full n (Node4 l p m q m' q' r)"
|
|
302 |
|
|
303 |
inductive_cases full_0_elim: "full 0 t"
|
|
304 |
inductive_cases full_Suc_elim: "full (Suc n) t"
|
|
305 |
|
|
306 |
lemma full_0_iff [simp]: "full 0 t \<longleftrightarrow> t = Leaf"
|
|
307 |
by (auto elim: full_0_elim intro: full.intros)
|
|
308 |
|
|
309 |
lemma full_Leaf_iff [simp]: "full n Leaf \<longleftrightarrow> n = 0"
|
|
310 |
by (auto elim: full_elims intro: full.intros)
|
|
311 |
|
|
312 |
lemma full_Suc_Node2_iff [simp]:
|
|
313 |
"full (Suc n) (Node2 l p r) \<longleftrightarrow> full n l \<and> full n r"
|
|
314 |
by (auto elim: full_elims intro: full.intros)
|
|
315 |
|
|
316 |
lemma full_Suc_Node3_iff [simp]:
|
|
317 |
"full (Suc n) (Node3 l p m q r) \<longleftrightarrow> full n l \<and> full n m \<and> full n r"
|
|
318 |
by (auto elim: full_elims intro: full.intros)
|
|
319 |
|
|
320 |
lemma full_Suc_Node4_iff [simp]:
|
|
321 |
"full (Suc n) (Node4 l p m q m' q' r) \<longleftrightarrow> full n l \<and> full n m \<and> full n m' \<and> full n r"
|
|
322 |
by (auto elim: full_elims intro: full.intros)
|
|
323 |
|
|
324 |
lemma full_imp_height: "full n t \<Longrightarrow> height t = n"
|
|
325 |
by (induct set: full, simp_all)
|
|
326 |
|
|
327 |
lemma full_imp_bal: "full n t \<Longrightarrow> bal t"
|
|
328 |
by (induct set: full, auto dest: full_imp_height)
|
|
329 |
|
|
330 |
lemma bal_imp_full: "bal t \<Longrightarrow> full (height t) t"
|
|
331 |
by (induct t, simp_all)
|
|
332 |
|
|
333 |
lemma bal_iff_full: "bal t \<longleftrightarrow> (\<exists>n. full n t)"
|
|
334 |
by (auto elim!: bal_imp_full full_imp_bal)
|
|
335 |
|
|
336 |
text {* The @{const "insert"} function either preserves the height of the
|
|
337 |
tree, or increases it by one. The constructor returned by the @{term
|
|
338 |
"insert"} function determines which: A return value of the form @{term
|
|
339 |
"T\<^sub>i t"} indicates that the height will be the same. A value of the
|
|
340 |
form @{term "Up\<^sub>i l p r"} indicates an increase in height. *}
|
|
341 |
|
|
342 |
primrec full\<^sub>i :: "nat \<Rightarrow> 'a up\<^sub>i \<Rightarrow> bool" where
|
|
343 |
"full\<^sub>i n (T\<^sub>i t) \<longleftrightarrow> full n t" |
|
|
344 |
"full\<^sub>i n (Up\<^sub>i l p r) \<longleftrightarrow> full n l \<and> full n r"
|
|
345 |
|
|
346 |
lemma full\<^sub>i_ins: "full n t \<Longrightarrow> full\<^sub>i n (ins a t)"
|
|
347 |
apply (induct rule: full.induct)
|
|
348 |
apply (auto, auto split: up\<^sub>i.split)
|
|
349 |
done
|
|
350 |
|
|
351 |
text {* The @{const insert} operation preserves balance. *}
|
|
352 |
|
|
353 |
lemma bal_insert: "bal t \<Longrightarrow> bal (insert a t)"
|
|
354 |
unfolding bal_iff_full insert_def
|
|
355 |
apply (erule exE)
|
|
356 |
apply (drule full\<^sub>i_ins [of _ _ a])
|
|
357 |
apply (cases "ins a t")
|
|
358 |
apply (auto intro: full.intros)
|
|
359 |
done
|
|
360 |
|
|
361 |
|
|
362 |
subsubsection "Proofs for delete"
|
|
363 |
|
|
364 |
instantiation up\<^sub>d :: (type)height
|
|
365 |
begin
|
|
366 |
|
|
367 |
fun height_up\<^sub>d :: "'a up\<^sub>d \<Rightarrow> nat" where
|
|
368 |
"height (T\<^sub>d t) = height t" |
|
|
369 |
"height (Up\<^sub>d t) = height t + 1"
|
|
370 |
|
|
371 |
instance ..
|
|
372 |
|
|
373 |
end
|
|
374 |
|
|
375 |
lemma bal_tree\<^sub>d_node21:
|
|
376 |
"\<lbrakk>bal r; bal (tree\<^sub>d l); height r = height l \<rbrakk> \<Longrightarrow> bal (tree\<^sub>d (node21 l a r))"
|
|
377 |
by(induct l a r rule: node21.induct) auto
|
|
378 |
|
|
379 |
lemma bal_tree\<^sub>d_node22:
|
|
380 |
"\<lbrakk>bal(tree\<^sub>d r); bal l; height r = height l \<rbrakk> \<Longrightarrow> bal (tree\<^sub>d (node22 l a r))"
|
|
381 |
by(induct l a r rule: node22.induct) auto
|
|
382 |
|
|
383 |
lemma bal_tree\<^sub>d_node31:
|
|
384 |
"\<lbrakk> bal (tree\<^sub>d l); bal m; bal r; height l = height r; height m = height r \<rbrakk>
|
|
385 |
\<Longrightarrow> bal (tree\<^sub>d (node31 l a m b r))"
|
|
386 |
by(induct l a m b r rule: node31.induct) auto
|
|
387 |
|
|
388 |
lemma bal_tree\<^sub>d_node32:
|
|
389 |
"\<lbrakk> bal l; bal (tree\<^sub>d m); bal r; height l = height r; height m = height r \<rbrakk>
|
|
390 |
\<Longrightarrow> bal (tree\<^sub>d (node32 l a m b r))"
|
|
391 |
by(induct l a m b r rule: node32.induct) auto
|
|
392 |
|
|
393 |
lemma bal_tree\<^sub>d_node33:
|
|
394 |
"\<lbrakk> bal l; bal m; bal(tree\<^sub>d r); height l = height r; height m = height r \<rbrakk>
|
|
395 |
\<Longrightarrow> bal (tree\<^sub>d (node33 l a m b r))"
|
|
396 |
by(induct l a m b r rule: node33.induct) auto
|
|
397 |
|
|
398 |
lemma bal_tree\<^sub>d_node41:
|
|
399 |
"\<lbrakk> bal (tree\<^sub>d l); bal m; bal n; bal r; height l = height r; height m = height r; height n = height r \<rbrakk>
|
|
400 |
\<Longrightarrow> bal (tree\<^sub>d (node41 l a m b n c r))"
|
|
401 |
by(induct l a m b n c r rule: node41.induct) auto
|
|
402 |
|
|
403 |
lemma bal_tree\<^sub>d_node42:
|
|
404 |
"\<lbrakk> bal l; bal (tree\<^sub>d m); bal n; bal r; height l = height r; height m = height r; height n = height r \<rbrakk>
|
|
405 |
\<Longrightarrow> bal (tree\<^sub>d (node42 l a m b n c r))"
|
|
406 |
by(induct l a m b n c r rule: node42.induct) auto
|
|
407 |
|
|
408 |
lemma bal_tree\<^sub>d_node43:
|
|
409 |
"\<lbrakk> bal l; bal m; bal (tree\<^sub>d n); bal r; height l = height r; height m = height r; height n = height r \<rbrakk>
|
|
410 |
\<Longrightarrow> bal (tree\<^sub>d (node43 l a m b n c r))"
|
|
411 |
by(induct l a m b n c r rule: node43.induct) auto
|
|
412 |
|
|
413 |
lemma bal_tree\<^sub>d_node44:
|
|
414 |
"\<lbrakk> bal l; bal m; bal n; bal (tree\<^sub>d r); height l = height r; height m = height r; height n = height r \<rbrakk>
|
|
415 |
\<Longrightarrow> bal (tree\<^sub>d (node44 l a m b n c r))"
|
|
416 |
by(induct l a m b n c r rule: node44.induct) auto
|
|
417 |
|
|
418 |
lemmas bals = bal_tree\<^sub>d_node21 bal_tree\<^sub>d_node22
|
|
419 |
bal_tree\<^sub>d_node31 bal_tree\<^sub>d_node32 bal_tree\<^sub>d_node33
|
|
420 |
bal_tree\<^sub>d_node41 bal_tree\<^sub>d_node42 bal_tree\<^sub>d_node43 bal_tree\<^sub>d_node44
|
|
421 |
|
|
422 |
lemma height_node21:
|
|
423 |
"height r > 0 \<Longrightarrow> height(node21 l a r) = max (height l) (height r) + 1"
|
|
424 |
by(induct l a r rule: node21.induct)(simp_all add: max.assoc)
|
|
425 |
|
|
426 |
lemma height_node22:
|
|
427 |
"height l > 0 \<Longrightarrow> height(node22 l a r) = max (height l) (height r) + 1"
|
|
428 |
by(induct l a r rule: node22.induct)(simp_all add: max.assoc)
|
|
429 |
|
|
430 |
lemma height_node31:
|
|
431 |
"height m > 0 \<Longrightarrow> height(node31 l a m b r) =
|
|
432 |
max (height l) (max (height m) (height r)) + 1"
|
|
433 |
by(induct l a m b r rule: node31.induct)(simp_all add: max_def)
|
|
434 |
|
|
435 |
lemma height_node32:
|
|
436 |
"height r > 0 \<Longrightarrow> height(node32 l a m b r) =
|
|
437 |
max (height l) (max (height m) (height r)) + 1"
|
|
438 |
by(induct l a m b r rule: node32.induct)(simp_all add: max_def)
|
|
439 |
|
|
440 |
lemma height_node33:
|
|
441 |
"height m > 0 \<Longrightarrow> height(node33 l a m b r) =
|
|
442 |
max (height l) (max (height m) (height r)) + 1"
|
|
443 |
by(induct l a m b r rule: node33.induct)(simp_all add: max_def)
|
|
444 |
|
|
445 |
lemma height_node41:
|
|
446 |
"height m > 0 \<Longrightarrow> height(node41 l a m b n c r) =
|
|
447 |
max (height l) (max (height m) (max (height n) (height r))) + 1"
|
|
448 |
by(induct l a m b n c r rule: node41.induct)(simp_all add: max_def)
|
|
449 |
|
|
450 |
lemma height_node42:
|
|
451 |
"height l > 0 \<Longrightarrow> height(node42 l a m b n c r) =
|
|
452 |
max (height l) (max (height m) (max (height n) (height r))) + 1"
|
|
453 |
by(induct l a m b n c r rule: node42.induct)(simp_all add: max_def)
|
|
454 |
|
|
455 |
lemma height_node43:
|
|
456 |
"height m > 0 \<Longrightarrow> height(node43 l a m b n c r) =
|
|
457 |
max (height l) (max (height m) (max (height n) (height r))) + 1"
|
|
458 |
by(induct l a m b n c r rule: node43.induct)(simp_all add: max_def)
|
|
459 |
|
|
460 |
lemma height_node44:
|
|
461 |
"height n > 0 \<Longrightarrow> height(node44 l a m b n c r) =
|
|
462 |
max (height l) (max (height m) (max (height n) (height r))) + 1"
|
|
463 |
by(induct l a m b n c r rule: node44.induct)(simp_all add: max_def)
|
|
464 |
|
|
465 |
lemmas heights = height_node21 height_node22
|
|
466 |
height_node31 height_node32 height_node33
|
|
467 |
height_node41 height_node42 height_node43 height_node44
|
|
468 |
|
|
469 |
lemma height_del_min:
|
|
470 |
"del_min t = (x, t') \<Longrightarrow> height t > 0 \<Longrightarrow> bal t \<Longrightarrow> height t' = height t"
|
|
471 |
by(induct t arbitrary: x t' rule: del_min.induct)
|
|
472 |
(auto simp: heights split: prod.splits)
|
|
473 |
|
|
474 |
lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t"
|
|
475 |
by(induction x t rule: del.induct)
|
|
476 |
(auto simp add: heights height_del_min split: prod.split)
|
|
477 |
|
|
478 |
lemma bal_del_min:
|
|
479 |
"\<lbrakk> del_min t = (x, t'); bal t; height t > 0 \<rbrakk> \<Longrightarrow> bal (tree\<^sub>d t')"
|
|
480 |
by(induct t arbitrary: x t' rule: del_min.induct)
|
|
481 |
(auto simp: heights height_del_min bals split: prod.splits)
|
|
482 |
|
|
483 |
lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))"
|
|
484 |
by(induction x t rule: del.induct)
|
|
485 |
((auto simp: bals bal_del_min height_del height_del_min split: prod.split)[1])+
|
|
486 |
(* 64 secs (2015) *)
|
|
487 |
|
|
488 |
corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)"
|
|
489 |
by(simp add: delete_def bal_tree\<^sub>d_del)
|
|
490 |
|
|
491 |
|
|
492 |
subsection \<open>Overall Correctness\<close>
|
|
493 |
|
|
494 |
interpretation Set_by_Ordered
|
|
495 |
where empty = Leaf and isin = isin and insert = insert and delete = delete
|
|
496 |
and inorder = inorder and wf = bal
|
|
497 |
proof (standard, goal_cases)
|
|
498 |
case 2 thus ?case by(simp add: isin_set)
|
|
499 |
next
|
|
500 |
case 3 thus ?case by(simp add: inorder_insert)
|
|
501 |
next
|
|
502 |
case 4 thus ?case by(simp add: inorder_delete)
|
|
503 |
next
|
|
504 |
case 6 thus ?case by(simp add: bal_insert)
|
|
505 |
next
|
|
506 |
case 7 thus ?case by(simp add: bal_delete)
|
|
507 |
qed simp+
|
|
508 |
|
|
509 |
end
|