10 begin |
10 begin |
11 |
11 |
12 fun isin :: "'a::cmp tree23 \<Rightarrow> 'a \<Rightarrow> bool" where |
12 fun isin :: "'a::cmp tree23 \<Rightarrow> 'a \<Rightarrow> bool" where |
13 "isin Leaf x = False" | |
13 "isin Leaf x = False" | |
14 "isin (Node2 l a r) x = |
14 "isin (Node2 l a r) x = |
15 (case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x)" | |
15 (case cmp x a of |
|
16 LT \<Rightarrow> isin l x | |
|
17 EQ \<Rightarrow> True | |
|
18 GT \<Rightarrow> isin r x)" | |
16 "isin (Node3 l a m b r) x = |
19 "isin (Node3 l a m b r) x = |
17 (case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> (case cmp x b of |
20 (case cmp x a of |
18 LT \<Rightarrow> isin m x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x))" |
21 LT \<Rightarrow> isin l x | |
|
22 EQ \<Rightarrow> True | |
|
23 GT \<Rightarrow> |
|
24 (case cmp x b of |
|
25 LT \<Rightarrow> isin m x | |
|
26 EQ \<Rightarrow> True | |
|
27 GT \<Rightarrow> isin r x))" |
19 |
28 |
20 datatype 'a up\<^sub>i = T\<^sub>i "'a tree23" | Up\<^sub>i "'a tree23" 'a "'a tree23" |
29 datatype 'a up\<^sub>i = T\<^sub>i "'a tree23" | Up\<^sub>i "'a tree23" 'a "'a tree23" |
21 |
30 |
22 fun tree\<^sub>i :: "'a up\<^sub>i \<Rightarrow> 'a tree23" where |
31 fun tree\<^sub>i :: "'a up\<^sub>i \<Rightarrow> 'a tree23" where |
23 "tree\<^sub>i (T\<^sub>i t) = t" | |
32 "tree\<^sub>i (T\<^sub>i t) = t" | |
25 |
34 |
26 fun ins :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>i" where |
35 fun ins :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>i" where |
27 "ins x Leaf = Up\<^sub>i Leaf x Leaf" | |
36 "ins x Leaf = Up\<^sub>i Leaf x Leaf" | |
28 "ins x (Node2 l a r) = |
37 "ins x (Node2 l a r) = |
29 (case cmp x a of |
38 (case cmp x a of |
30 LT \<Rightarrow> (case ins x l of |
39 LT \<Rightarrow> |
31 T\<^sub>i l' => T\<^sub>i (Node2 l' a r) |
40 (case ins x l of |
32 | Up\<^sub>i l1 b l2 => T\<^sub>i (Node3 l1 b l2 a r)) | |
41 T\<^sub>i l' => T\<^sub>i (Node2 l' a r) | |
|
42 Up\<^sub>i l1 b l2 => T\<^sub>i (Node3 l1 b l2 a r)) | |
33 EQ \<Rightarrow> T\<^sub>i (Node2 l x r) | |
43 EQ \<Rightarrow> T\<^sub>i (Node2 l x r) | |
34 GT \<Rightarrow> (case ins x r of |
44 GT \<Rightarrow> |
35 T\<^sub>i r' => T\<^sub>i (Node2 l a r') |
45 (case ins x r of |
36 | Up\<^sub>i r1 b r2 => T\<^sub>i (Node3 l a r1 b r2)))" | |
46 T\<^sub>i r' => T\<^sub>i (Node2 l a r') | |
|
47 Up\<^sub>i r1 b r2 => T\<^sub>i (Node3 l a r1 b r2)))" | |
37 "ins x (Node3 l a m b r) = |
48 "ins x (Node3 l a m b r) = |
38 (case cmp x a of |
49 (case cmp x a of |
39 LT \<Rightarrow> (case ins x l of |
50 LT \<Rightarrow> |
40 T\<^sub>i l' => T\<^sub>i (Node3 l' a m b r) |
51 (case ins x l of |
41 | Up\<^sub>i l1 c l2 => Up\<^sub>i (Node2 l1 c l2) a (Node2 m b r)) | |
52 T\<^sub>i l' => T\<^sub>i (Node3 l' a m b r) | |
|
53 Up\<^sub>i l1 c l2 => Up\<^sub>i (Node2 l1 c l2) a (Node2 m b r)) | |
42 EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) | |
54 EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) | |
43 GT \<Rightarrow> (case cmp x b of |
55 GT \<Rightarrow> |
44 GT \<Rightarrow> (case ins x r of |
56 (case cmp x b of |
45 T\<^sub>i r' => T\<^sub>i (Node3 l a m b r') |
57 GT \<Rightarrow> |
46 | Up\<^sub>i r1 c r2 => Up\<^sub>i (Node2 l a m) b (Node2 r1 c r2)) | |
58 (case ins x r of |
47 EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) | |
59 T\<^sub>i r' => T\<^sub>i (Node3 l a m b r') | |
48 LT \<Rightarrow> (case ins x m of |
60 Up\<^sub>i r1 c r2 => Up\<^sub>i (Node2 l a m) b (Node2 r1 c r2)) | |
49 T\<^sub>i m' => T\<^sub>i (Node3 l a m' b r) |
61 EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) | |
50 | Up\<^sub>i m1 c m2 => Up\<^sub>i (Node2 l a m1) c (Node2 m2 b r))))" |
62 LT \<Rightarrow> |
|
63 (case ins x m of |
|
64 T\<^sub>i m' => T\<^sub>i (Node3 l a m' b r) | |
|
65 Up\<^sub>i m1 c m2 => Up\<^sub>i (Node2 l a m1) c (Node2 m2 b r))))" |
51 |
66 |
52 hide_const insert |
67 hide_const insert |
53 |
68 |
54 definition insert :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where |
69 definition insert :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where |
55 "insert x t = tree\<^sub>i(ins x t)" |
70 "insert x t = tree\<^sub>i(ins x t)" |
91 "del_min (Node2 Leaf a Leaf) = (a, Up\<^sub>d Leaf)" | |
106 "del_min (Node2 Leaf a Leaf) = (a, Up\<^sub>d Leaf)" | |
92 "del_min (Node3 Leaf a Leaf b Leaf) = (a, T\<^sub>d(Node2 Leaf b Leaf))" | |
107 "del_min (Node3 Leaf a Leaf b Leaf) = (a, T\<^sub>d(Node2 Leaf b Leaf))" | |
93 "del_min (Node2 l a r) = (let (x,l') = del_min l in (x, node21 l' a r))" | |
108 "del_min (Node2 l a r) = (let (x,l') = del_min l in (x, node21 l' a r))" | |
94 "del_min (Node3 l a m b r) = (let (x,l') = del_min l in (x, node31 l' a m b r))" |
109 "del_min (Node3 l a m b r) = (let (x,l') = del_min l in (x, node31 l' a m b r))" |
95 |
110 |
96 fun del :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" |
111 fun del :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" where |
97 where |
|
98 "del x Leaf = T\<^sub>d Leaf" | |
112 "del x Leaf = T\<^sub>d Leaf" | |
99 "del x (Node2 Leaf a Leaf) = (if x = a then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf a Leaf))" | |
113 "del x (Node2 Leaf a Leaf) = |
100 "del x (Node3 Leaf a Leaf b Leaf) = T\<^sub>d(if x = a then Node2 Leaf b Leaf |
114 (if x = a then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf a Leaf))" | |
101 else if x = b then Node2 Leaf a Leaf else Node3 Leaf a Leaf b Leaf)" | |
115 "del x (Node3 Leaf a Leaf b Leaf) = |
102 "del x (Node2 l a r) = (case cmp x a of |
116 T\<^sub>d(if x = a then Node2 Leaf b Leaf else |
103 LT \<Rightarrow> node21 (del x l) a r | |
117 if x = b then Node2 Leaf a Leaf |
104 GT \<Rightarrow> node22 l a (del x r) | |
118 else Node3 Leaf a Leaf b Leaf)" | |
105 EQ \<Rightarrow> let (a',t) = del_min r in node22 l a' t)" | |
119 "del x (Node2 l a r) = |
106 "del x (Node3 l a m b r) = (case cmp x a of |
120 (case cmp x a of |
107 LT \<Rightarrow> node31 (del x l) a m b r | |
121 LT \<Rightarrow> node21 (del x l) a r | |
108 EQ \<Rightarrow> let (a',m') = del_min m in node32 l a' m' b r | |
122 GT \<Rightarrow> node22 l a (del x r) | |
109 GT \<Rightarrow> (case cmp x b of |
123 EQ \<Rightarrow> let (a',t) = del_min r in node22 l a' t)" | |
|
124 "del x (Node3 l a m b r) = |
|
125 (case cmp x a of |
|
126 LT \<Rightarrow> node31 (del x l) a m b r | |
|
127 EQ \<Rightarrow> let (a',m') = del_min m in node32 l a' m' b r | |
|
128 GT \<Rightarrow> |
|
129 (case cmp x b of |
110 LT \<Rightarrow> node32 l a (del x m) b r | |
130 LT \<Rightarrow> node32 l a (del x m) b r | |
111 EQ \<Rightarrow> let (b',r') = del_min r in node33 l a m b' r' | |
131 EQ \<Rightarrow> let (b',r') = del_min r in node33 l a m b' r' | |
112 GT \<Rightarrow> node33 l a m b (del x r)))" |
132 GT \<Rightarrow> node33 l a m b (del x r)))" |
113 |
133 |
114 definition delete :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where |
134 definition delete :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where |