90 N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" | |
90 N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" | |
91 "n2 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4)) a5 (N1 (N1 t5)) = |
91 "n2 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4)) a5 (N1 (N1 t5)) = |
92 N2 (N1 (N2 t1 a1 t2)) a2 (N2 (N2 t3 a3 t4) a5 (N1 t5))" | |
92 N2 (N1 (N2 t1 a1 t2)) a2 (N2 (N2 t3 a3 t4) a5 (N1 t5))" | |
93 "n2 t1 a1 t2 = N2 t1 a1 t2" |
93 "n2 t1 a1 t2 = N2 t1 a1 t2" |
94 |
94 |
95 fun del_min :: "'a bro \<Rightarrow> ('a \<times> 'a bro) option" where |
95 fun split_min :: "'a bro \<Rightarrow> ('a \<times> 'a bro) option" where |
96 "del_min N0 = None" | |
96 "split_min N0 = None" | |
97 "del_min (N1 t) = |
97 "split_min (N1 t) = |
98 (case del_min t of |
98 (case split_min t of |
99 None \<Rightarrow> None | |
99 None \<Rightarrow> None | |
100 Some (a, t') \<Rightarrow> Some (a, N1 t'))" | |
100 Some (a, t') \<Rightarrow> Some (a, N1 t'))" | |
101 "del_min (N2 t1 a t2) = |
101 "split_min (N2 t1 a t2) = |
102 (case del_min t1 of |
102 (case split_min t1 of |
103 None \<Rightarrow> Some (a, N1 t2) | |
103 None \<Rightarrow> Some (a, N1 t2) | |
104 Some (b, t1') \<Rightarrow> Some (b, n2 t1' a t2))" |
104 Some (b, t1') \<Rightarrow> Some (b, n2 t1' a t2))" |
105 |
105 |
106 fun del :: "'a::linorder \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where |
106 fun del :: "'a::linorder \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where |
107 "del _ N0 = N0" | |
107 "del _ N0 = N0" | |
108 "del x (N1 t) = N1 (del x t)" | |
108 "del x (N1 t) = N1 (del x t)" | |
109 "del x (N2 l a r) = |
109 "del x (N2 l a r) = |
110 (case cmp x a of |
110 (case cmp x a of |
111 LT \<Rightarrow> n2 (del x l) a r | |
111 LT \<Rightarrow> n2 (del x l) a r | |
112 GT \<Rightarrow> n2 l a (del x r) | |
112 GT \<Rightarrow> n2 l a (del x r) | |
113 EQ \<Rightarrow> (case del_min r of |
113 EQ \<Rightarrow> (case split_min r of |
114 None \<Rightarrow> N1 l | |
114 None \<Rightarrow> N1 l | |
115 Some (b, r') \<Rightarrow> n2 l b r'))" |
115 Some (b, r') \<Rightarrow> n2 l b r'))" |
116 |
116 |
117 fun tree :: "'a bro \<Rightarrow> 'a bro" where |
117 fun tree :: "'a bro \<Rightarrow> 'a bro" where |
118 "tree (N1 t) = t" | |
118 "tree (N1 t) = t" | |
187 by(cases t) auto |
187 by(cases t) auto |
188 |
188 |
189 lemma inorder_n2: "inorder(n2 l a r) = inorder l @ a # inorder r" |
189 lemma inorder_n2: "inorder(n2 l a r) = inorder l @ a # inorder r" |
190 by(cases "(l,a,r)" rule: n2.cases) (auto) |
190 by(cases "(l,a,r)" rule: n2.cases) (auto) |
191 |
191 |
192 lemma inorder_del_min: |
192 lemma inorder_split_min: |
193 "t \<in> T h \<Longrightarrow> (del_min t = None \<longleftrightarrow> inorder t = []) \<and> |
193 "t \<in> T h \<Longrightarrow> (split_min t = None \<longleftrightarrow> inorder t = []) \<and> |
194 (del_min t = Some(a,t') \<longrightarrow> inorder t = a # inorder t')" |
194 (split_min t = Some(a,t') \<longrightarrow> inorder t = a # inorder t')" |
195 by(induction h arbitrary: t a t') (auto simp: inorder_n2 split: option.splits) |
195 by(induction h arbitrary: t a t') (auto simp: inorder_n2 split: option.splits) |
196 |
196 |
197 lemma inorder_del: |
197 lemma inorder_del: |
198 "t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(del x t) = del_list x (inorder t)" |
198 "t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(del x t) = del_list x (inorder t)" |
199 by(induction h arbitrary: t) (auto simp: del_list_simps inorder_n2 |
199 by(induction h arbitrary: t) (auto simp: del_list_simps inorder_n2 |
200 inorder_del_min[OF UnI1] inorder_del_min[OF UnI2] split: option.splits) |
200 inorder_split_min[OF UnI1] inorder_split_min[OF UnI2] split: option.splits) |
201 |
201 |
202 lemma inorder_delete: |
202 lemma inorder_delete: |
203 "t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)" |
203 "t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)" |
204 by(simp add: delete_def inorder_del inorder_tree) |
204 by(simp add: delete_def inorder_del inorder_tree) |
205 |
205 |
329 apply(cases h rule: Bp.cases) |
329 apply(cases h rule: Bp.cases) |
330 apply auto[2] |
330 apply auto[2] |
331 apply(erule exE bexE conjE imageE | simp | erule disjE)+ |
331 apply(erule exE bexE conjE imageE | simp | erule disjE)+ |
332 done |
332 done |
333 |
333 |
334 lemma del_minNoneN0: "\<lbrakk>t \<in> B h; del_min t = None\<rbrakk> \<Longrightarrow> t = N0" |
334 lemma split_minNoneN0: "\<lbrakk>t \<in> B h; split_min t = None\<rbrakk> \<Longrightarrow> t = N0" |
335 by (cases t) (auto split: option.splits) |
335 by (cases t) (auto split: option.splits) |
336 |
336 |
337 lemma del_minNoneN1 : "\<lbrakk>t \<in> U h; del_min t = None\<rbrakk> \<Longrightarrow> t = N1 N0" |
337 lemma split_minNoneN1 : "\<lbrakk>t \<in> U h; split_min t = None\<rbrakk> \<Longrightarrow> t = N1 N0" |
338 by (cases h) (auto simp: del_minNoneN0 split: option.splits) |
338 by (cases h) (auto simp: split_minNoneN0 split: option.splits) |
339 |
339 |
340 lemma del_min_type: |
340 lemma split_min_type: |
341 "t \<in> B h \<Longrightarrow> del_min t = Some (a, t') \<Longrightarrow> t' \<in> T h" |
341 "t \<in> B h \<Longrightarrow> split_min t = Some (a, t') \<Longrightarrow> t' \<in> T h" |
342 "t \<in> U h \<Longrightarrow> del_min t = Some (a, t') \<Longrightarrow> t' \<in> Um h" |
342 "t \<in> U h \<Longrightarrow> split_min t = Some (a, t') \<Longrightarrow> t' \<in> Um h" |
343 proof (induction h arbitrary: t a t') |
343 proof (induction h arbitrary: t a t') |
344 case (Suc h) |
344 case (Suc h) |
345 { case 1 |
345 { case 1 |
346 then obtain t1 a t2 where [simp]: "t = N2 t1 a t2" and |
346 then obtain t1 a t2 where [simp]: "t = N2 t1 a t2" and |
347 t12: "t1 \<in> T h" "t2 \<in> T h" "t1 \<in> B h \<or> t2 \<in> B h" |
347 t12: "t1 \<in> T h" "t2 \<in> T h" "t1 \<in> B h \<or> t2 \<in> B h" |
348 by auto |
348 by auto |
349 show ?case |
349 show ?case |
350 proof (cases "del_min t1") |
350 proof (cases "split_min t1") |
351 case None |
351 case None |
352 show ?thesis |
352 show ?thesis |
353 proof cases |
353 proof cases |
354 assume "t1 \<in> B h" |
354 assume "t1 \<in> B h" |
355 with del_minNoneN0[OF this None] 1 show ?thesis by(auto) |
355 with split_minNoneN0[OF this None] 1 show ?thesis by(auto) |
356 next |
356 next |
357 assume "t1 \<notin> B h" |
357 assume "t1 \<notin> B h" |
358 thus ?thesis using 1 None by (auto) |
358 thus ?thesis using 1 None by (auto) |
359 qed |
359 qed |
360 next |
360 next |
374 qed |
374 qed |
375 } |
375 } |
376 { case 2 |
376 { case 2 |
377 then obtain t1 where [simp]: "t = N1 t1" and t1: "t1 \<in> B h" by auto |
377 then obtain t1 where [simp]: "t = N1 t1" and t1: "t1 \<in> B h" by auto |
378 show ?case |
378 show ?case |
379 proof (cases "del_min t1") |
379 proof (cases "split_min t1") |
380 case None |
380 case None |
381 with del_minNoneN0[OF t1 None] 2 show ?thesis by(auto) |
381 with split_minNoneN0[OF t1 None] 2 show ?thesis by(auto) |
382 next |
382 next |
383 case [simp]: (Some bt') |
383 case [simp]: (Some bt') |
384 obtain b t1' where [simp]: "bt' = (b,t1')" by fastforce |
384 obtain b t1' where [simp]: "bt' = (b,t1')" by fastforce |
385 from Suc.IH(1)[OF t1] have "t1' \<in> T h" by simp |
385 from Suc.IH(1)[OF t1] have "t1' \<in> T h" by simp |
386 thus ?thesis using 2 by auto |
386 thus ?thesis using 2 by auto |
419 from n2_type2[OF this(1) Suc.IH(2)[OF this(2)]] |
419 from n2_type2[OF this(1) Suc.IH(2)[OF this(2)]] |
420 show ?thesis using \<open>x>a\<close> by(simp) |
420 show ?thesis using \<open>x>a\<close> by(simp) |
421 qed |
421 qed |
422 moreover |
422 moreover |
423 have ?case if [simp]: "x=a" |
423 have ?case if [simp]: "x=a" |
424 proof (cases "del_min r") |
424 proof (cases "split_min r") |
425 case None |
425 case None |
426 show ?thesis |
426 show ?thesis |
427 proof cases |
427 proof cases |
428 assume "r \<in> B h" |
428 assume "r \<in> B h" |
429 with del_minNoneN0[OF this None] lr show ?thesis by(simp) |
429 with split_minNoneN0[OF this None] lr show ?thesis by(simp) |
430 next |
430 next |
431 assume "r \<notin> B h" |
431 assume "r \<notin> B h" |
432 hence "r \<in> U h" using lr by auto |
432 hence "r \<in> U h" using lr by auto |
433 with del_minNoneN1[OF this None] lr(3) show ?thesis by (simp) |
433 with split_minNoneN1[OF this None] lr(3) show ?thesis by (simp) |
434 qed |
434 qed |
435 next |
435 next |
436 case [simp]: (Some br') |
436 case [simp]: (Some br') |
437 obtain b r' where [simp]: "br' = (b,r')" by fastforce |
437 obtain b r' where [simp]: "br' = (b,r')" by fastforce |
438 show ?thesis |
438 show ?thesis |
439 proof cases |
439 proof cases |
440 assume "r \<in> B h" |
440 assume "r \<in> B h" |
441 from del_min_type(1)[OF this] n2_type3[OF lr(1)] |
441 from split_min_type(1)[OF this] n2_type3[OF lr(1)] |
442 show ?thesis by simp |
442 show ?thesis by simp |
443 next |
443 next |
444 assume "r \<notin> B h" |
444 assume "r \<notin> B h" |
445 hence "l \<in> B h" and "r \<in> U h" using lr by auto |
445 hence "l \<in> B h" and "r \<in> U h" using lr by auto |
446 from del_min_type(2)[OF this(2)] n2_type2[OF this(1)] |
446 from split_min_type(2)[OF this(2)] n2_type2[OF this(1)] |
447 show ?thesis by simp |
447 show ?thesis by simp |
448 qed |
448 qed |
449 qed |
449 qed |
450 ultimately show ?case by auto |
450 ultimately show ?case by auto |
451 } |
451 } |