8 "../Data_Structures/Map_by_Ordered" |
8 "../Data_Structures/Map_by_Ordered" |
9 begin |
9 begin |
10 |
10 |
11 subsection \<open>Map operations on 2-3-4 trees\<close> |
11 subsection \<open>Map operations on 2-3-4 trees\<close> |
12 |
12 |
13 fun lookup :: "('a::linorder * 'b) tree234 \<Rightarrow> 'a \<Rightarrow> 'b option" where |
13 fun lookup :: "('a::cmp * 'b) tree234 \<Rightarrow> 'a \<Rightarrow> 'b option" where |
14 "lookup Leaf x = None" | |
14 "lookup Leaf x = None" | |
15 "lookup (Node2 l (a,b) r) x = |
15 "lookup (Node2 l (a,b) r) x = (case cmp x a of |
16 (if x < a then lookup l x else |
16 LT \<Rightarrow> lookup l x | |
17 if a < x then lookup r x else Some b)" | |
17 GT \<Rightarrow> lookup r x | |
18 "lookup (Node3 l (a1,b1) m (a2,b2) r) x = |
18 EQ \<Rightarrow> Some b)" | |
19 (if x < a1 then lookup l x else |
19 "lookup (Node3 l (a1,b1) m (a2,b2) r) x = (case cmp x a1 of |
20 if x = a1 then Some b1 else |
20 LT \<Rightarrow> lookup l x | |
21 if x < a2 then lookup m x else |
21 EQ \<Rightarrow> Some b1 | |
22 if x = a2 then Some b2 |
22 GT \<Rightarrow> (case cmp x a2 of |
23 else lookup r x)" | |
23 LT \<Rightarrow> lookup m x | |
24 "lookup (Node4 l (a1,b1) m (a2,b2) n (a3,b3) r) x = |
24 EQ \<Rightarrow> Some b2 | |
25 (if x < a2 then |
25 GT \<Rightarrow> lookup r x))" | |
26 if x = a1 then Some b1 else |
26 "lookup (Node4 t1 (a1,b1) t2 (a2,b2) t3 (a3,b3) t4) x = (case cmp x a2 of |
27 if x < a1 then lookup l x else lookup m x |
27 LT \<Rightarrow> (case cmp x a1 of |
28 else |
28 LT \<Rightarrow> lookup t1 x | EQ \<Rightarrow> Some b1 | GT \<Rightarrow> lookup t2 x) | |
29 if x = a2 then Some b2 else |
29 EQ \<Rightarrow> Some b2 | |
30 if x = a3 then Some b3 else |
30 GT \<Rightarrow> (case cmp x a3 of |
31 if x < a3 then lookup n x |
31 LT \<Rightarrow> lookup t3 x | EQ \<Rightarrow> Some b3 | GT \<Rightarrow> lookup t4 x))" |
32 else lookup r x)" |
|
33 |
32 |
34 fun upd :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>i" where |
33 fun upd :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>i" where |
35 "upd x y Leaf = Up\<^sub>i Leaf (x,y) Leaf" | |
34 "upd x y Leaf = Up\<^sub>i Leaf (x,y) Leaf" | |
36 "upd x y (Node2 l ab r) = |
35 "upd x y (Node2 l ab r) = (case cmp x (fst ab) of |
37 (if x < fst ab then |
36 LT \<Rightarrow> (case upd x y l of |
38 (case upd x y l of |
|
39 T\<^sub>i l' => T\<^sub>i (Node2 l' ab r) |
37 T\<^sub>i l' => T\<^sub>i (Node2 l' ab r) |
40 | Up\<^sub>i l1 q l2 => T\<^sub>i (Node3 l1 q l2 ab r)) |
38 | Up\<^sub>i l1 ab' l2 => T\<^sub>i (Node3 l1 ab' l2 ab r)) | |
41 else if x = fst ab then T\<^sub>i (Node2 l (x,y) r) |
39 EQ \<Rightarrow> T\<^sub>i (Node2 l (x,y) r) | |
42 else |
40 GT \<Rightarrow> (case upd x y r of |
43 (case upd x y r of |
|
44 T\<^sub>i r' => T\<^sub>i (Node2 l ab r') |
41 T\<^sub>i r' => T\<^sub>i (Node2 l ab r') |
45 | Up\<^sub>i r1 q r2 => T\<^sub>i (Node3 l ab r1 q r2)))" | |
42 | Up\<^sub>i r1 ab' r2 => T\<^sub>i (Node3 l ab r1 ab' r2)))" | |
46 "upd x y (Node3 l ab1 m ab2 r) = |
43 "upd x y (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of |
47 (if x < fst ab1 then |
44 LT \<Rightarrow> (case upd x y l of |
48 (case upd x y l of |
|
49 T\<^sub>i l' => T\<^sub>i (Node3 l' ab1 m ab2 r) |
45 T\<^sub>i l' => T\<^sub>i (Node3 l' ab1 m ab2 r) |
50 | Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) ab1 (Node2 m ab2 r)) |
46 | Up\<^sub>i l1 ab' l2 => Up\<^sub>i (Node2 l1 ab' l2) ab1 (Node2 m ab2 r)) | |
51 else if x = fst ab1 then T\<^sub>i (Node3 l (x,y) m ab2 r) |
47 EQ \<Rightarrow> T\<^sub>i (Node3 l (x,y) m ab2 r) | |
52 else if x < fst ab2 then |
48 GT \<Rightarrow> (case cmp x (fst ab2) of |
53 (case upd x y m of |
49 LT \<Rightarrow> (case upd x y m of |
54 T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r) |
50 T\<^sub>i m' => T\<^sub>i (Node3 l ab1 m' ab2 r) |
55 | Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l ab1 m1) q (Node2 m2 ab2 r)) |
51 | Up\<^sub>i m1 ab' m2 => Up\<^sub>i (Node2 l ab1 m1) ab' (Node2 m2 ab2 r)) | |
56 else if x = fst ab2 then T\<^sub>i (Node3 l ab1 m (x,y) r) |
52 EQ \<Rightarrow> T\<^sub>i (Node3 l ab1 m (x,y) r) | |
57 else |
53 GT \<Rightarrow> (case upd x y r of |
58 (case upd x y r of |
54 T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r') |
59 T\<^sub>i r' => T\<^sub>i (Node3 l ab1 m ab2 r') |
55 | Up\<^sub>i r1 ab' r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 ab' r2))))" | |
60 | Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node2 r1 q r2)))" | |
56 "upd x y (Node4 t1 ab1 t2 ab2 t3 ab3 t4) = (case cmp x (fst ab2) of |
61 "upd x y (Node4 l ab1 m ab2 n ab3 r) = |
57 LT \<Rightarrow> (case cmp x (fst ab1) of |
62 (if x < fst ab2 then |
58 LT \<Rightarrow> (case upd x y t1 of |
63 if x < fst ab1 then |
59 T\<^sub>i t1' => T\<^sub>i (Node4 t1' ab1 t2 ab2 t3 ab3 t4) |
64 (case upd x y l of |
60 | Up\<^sub>i t11 q t12 => Up\<^sub>i (Node2 t11 q t12) ab1 (Node3 t2 ab2 t3 ab3 t4)) | |
65 T\<^sub>i l' => T\<^sub>i (Node4 l' ab1 m ab2 n ab3 r) |
61 EQ \<Rightarrow> T\<^sub>i (Node4 t1 (x,y) t2 ab2 t3 ab3 t4) | |
66 | Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) ab1 (Node3 m ab2 n ab3 r)) |
62 GT \<Rightarrow> (case upd x y t2 of |
67 else |
63 T\<^sub>i t2' => T\<^sub>i (Node4 t1 ab1 t2' ab2 t3 ab3 t4) |
68 if x = fst ab1 then T\<^sub>i (Node4 l (x,y) m ab2 n ab3 r) |
64 | Up\<^sub>i t21 q t22 => Up\<^sub>i (Node2 t1 ab1 t21) q (Node3 t22 ab2 t3 ab3 t4))) | |
69 else |
65 EQ \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 (x,y) t3 ab3 t4) | |
70 (case upd x y m of |
66 GT \<Rightarrow> (case cmp x (fst ab3) of |
71 T\<^sub>i m' => T\<^sub>i (Node4 l ab1 m' ab2 n ab3 r) |
67 LT \<Rightarrow> (case upd x y t3 of |
72 | Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l ab1 m1) q (Node3 m2 ab2 n ab3 r)) |
68 T\<^sub>i t3' \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 ab2 t3' ab3 t4) |
73 else |
69 | Up\<^sub>i t31 q t32 => Up\<^sub>i (Node2 t1 ab1 t2) ab2(*q*) (Node3 t31 q t32 ab3 t4)) | |
74 if x = fst ab2 then T\<^sub>i (Node4 l ab1 m (x,y) n ab3 r) else |
70 EQ \<Rightarrow> T\<^sub>i (Node4 t1 ab1 t2 ab2 t3 (x,y) t4) | |
75 if x < fst ab3 then |
71 GT \<Rightarrow> (case upd x y t4 of |
76 (case upd x y n of |
72 T\<^sub>i t4' => T\<^sub>i (Node4 t1 ab1 t2 ab2 t3 ab3 t4') |
77 T\<^sub>i n' => T\<^sub>i (Node4 l ab1 m ab2 n' ab3 r) |
73 | Up\<^sub>i t41 q t42 => Up\<^sub>i (Node2 t1 ab1 t2) ab2 (Node3 t3 ab3 t41 q t42))))" |
78 | Up\<^sub>i n1 q n2 => Up\<^sub>i (Node2 l ab1 m) ab2(*q*) (Node3 n1 q n2 ab3 r)) |
|
79 else |
|
80 if x = fst ab3 then T\<^sub>i (Node4 l ab1 m ab2 n (x,y) r) |
|
81 else |
|
82 (case upd x y r of |
|
83 T\<^sub>i r' => T\<^sub>i (Node4 l ab1 m ab2 n ab3 r') |
|
84 | Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l ab1 m) ab2 (Node3 n ab3 r1 q r2)))" |
|
85 |
74 |
86 definition update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where |
75 definition update :: "'a::cmp \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where |
87 "update a b t = tree\<^sub>i(upd a b t)" |
76 "update x y t = tree\<^sub>i(upd x y t)" |
88 |
77 |
89 fun del :: "'a::linorder \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>d" |
78 fun del :: "'a::cmp \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) up\<^sub>d" where |
90 where |
79 "del x Leaf = T\<^sub>d Leaf" | |
91 "del k Leaf = T\<^sub>d Leaf" | |
80 "del x (Node2 Leaf ab1 Leaf) = (if x=fst ab1 then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf ab1 Leaf))" | |
92 "del k (Node2 Leaf p Leaf) = (if k=fst p then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf p Leaf))" | |
81 "del x (Node3 Leaf ab1 Leaf ab2 Leaf) = T\<^sub>d(if x=fst ab1 then Node2 Leaf ab2 Leaf |
93 "del k (Node3 Leaf p Leaf q Leaf) = |
82 else if x=fst ab2 then Node2 Leaf ab1 Leaf else Node3 Leaf ab1 Leaf ab2 Leaf)" | |
94 T\<^sub>d(if k=fst p then Node2 Leaf q Leaf else |
83 "del x (Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf) = |
95 if k=fst q then Node2 Leaf p Leaf |
84 T\<^sub>d(if x = fst ab1 then Node3 Leaf ab2 Leaf ab3 Leaf else |
96 else Node3 Leaf p Leaf q Leaf)" | |
85 if x = fst ab2 then Node3 Leaf ab1 Leaf ab3 Leaf else |
97 "del k (Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf) = |
86 if x = fst ab3 then Node3 Leaf ab1 Leaf ab2 Leaf |
98 T\<^sub>d(if k=fst ab1 then Node3 Leaf ab2 Leaf ab3 Leaf else |
|
99 if k=fst ab2 then Node3 Leaf ab1 Leaf ab3 Leaf else |
|
100 if k=fst ab3 then Node3 Leaf ab1 Leaf ab2 Leaf |
|
101 else Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf)" | |
87 else Node4 Leaf ab1 Leaf ab2 Leaf ab3 Leaf)" | |
102 "del k (Node2 l a r) = |
88 "del x (Node2 l ab1 r) = (case cmp x (fst ab1) of |
103 (if k<fst a then node21 (del k l) a r else |
89 LT \<Rightarrow> node21 (del x l) ab1 r | |
104 if k > fst a then node22 l a (del k r) |
90 GT \<Rightarrow> node22 l ab1 (del x r) | |
105 else let (a',t) = del_min r in node22 l a' t)" | |
91 EQ \<Rightarrow> let (ab1',t) = del_min r in node22 l ab1' t)" | |
106 "del k (Node3 l a m b r) = |
92 "del x (Node3 l ab1 m ab2 r) = (case cmp x (fst ab1) of |
107 (if k<fst a then node31 (del k l) a m b r else |
93 LT \<Rightarrow> node31 (del x l) ab1 m ab2 r | |
108 if k = fst a then let (a',m') = del_min m in node32 l a' m' b r else |
94 EQ \<Rightarrow> let (ab1',m') = del_min m in node32 l ab1' m' ab2 r | |
109 if k < fst b then node32 l a (del k m) b r else |
95 GT \<Rightarrow> (case cmp x (fst ab2) of |
110 if k = fst b then let (b',r') = del_min r in node33 l a m b' r' |
96 LT \<Rightarrow> node32 l ab1 (del x m) ab2 r | |
111 else node33 l a m b (del k r))" | |
97 EQ \<Rightarrow> let (ab2',r') = del_min r in node33 l ab1 m ab2' r' | |
112 "del x (Node4 l ab1 m ab2 n ab3 r) = |
98 GT \<Rightarrow> node33 l ab1 m ab2 (del x r)))" | |
113 (if x < fst ab2 then |
99 "del x (Node4 t1 ab1 t2 ab2 t3 ab3 t4) = (case cmp x (fst ab2) of |
114 if x < fst ab1 then node41 (del x l) ab1 m ab2 n ab3 r else |
100 LT \<Rightarrow> (case cmp x (fst ab1) of |
115 if x = fst ab1 then let (ab',m') = del_min m in node42 l ab' m' ab2 n ab3 r |
101 LT \<Rightarrow> node41 (del x t1) ab1 t2 ab2 t3 ab3 t4 | |
116 else node42 l ab1 (del x m) ab2 n ab3 r |
102 EQ \<Rightarrow> let (ab',t2') = del_min t2 in node42 t1 ab' t2' ab2 t3 ab3 t4 | |
117 else |
103 GT \<Rightarrow> node42 t1 ab1 (del x t2) ab2 t3 ab3 t4) | |
118 if x = fst ab2 then let (ab',n') = del_min n in node43 l ab1 m ab' n' ab3 r else |
104 EQ \<Rightarrow> let (ab',t3') = del_min t3 in node43 t1 ab1 t2 ab' t3' ab3 t4 | |
119 if x < fst ab3 then node43 l ab1 m ab2 (del x n) ab3 r else |
105 GT \<Rightarrow> (case cmp x (fst ab3) of |
120 if x = fst ab3 then let (ab',r') = del_min r in node44 l ab1 m ab2 n ab' r' |
106 LT \<Rightarrow> node43 t1 ab1 t2 ab2 (del x t3) ab3 t4 | |
121 else node44 l ab1 m ab2 n ab3 (del x r))" |
107 EQ \<Rightarrow> let (ab',t4') = del_min t4 in node44 t1 ab1 t2 ab2 t3 ab' t4' | |
|
108 GT \<Rightarrow> node44 t1 ab1 t2 ab2 t3 ab3 (del x t4)))" |
122 |
109 |
123 definition delete :: "'a::linorder \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where |
110 definition delete :: "'a::cmp \<Rightarrow> ('a*'b) tree234 \<Rightarrow> ('a*'b) tree234" where |
124 "delete k t = tree\<^sub>d(del k t)" |
111 "delete x t = tree\<^sub>d(del x t)" |
125 |
112 |
126 |
113 |
127 subsection "Functional correctness" |
114 subsection "Functional correctness" |
128 |
115 |
129 lemma lookup: "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x" |
116 lemma lookup: "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x" |
142 |
129 |
143 lemma inorder_del: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow> |
130 lemma inorder_del: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow> |
144 inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)" |
131 inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)" |
145 by(induction t rule: del.induct) |
132 by(induction t rule: del.induct) |
146 ((auto simp: del_list_simps inorder_nodes del_minD split: prod.splits)[1])+ |
133 ((auto simp: del_list_simps inorder_nodes del_minD split: prod.splits)[1])+ |
147 (* 290 secs (2015) *) |
134 (* 200 secs (2015) *) |
148 |
135 |
149 lemma inorder_delete: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow> |
136 lemma inorder_delete: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow> |
150 inorder(delete x t) = del_list x (inorder t)" |
137 inorder(delete x t) = del_list x (inorder t)" |
151 by(simp add: delete_def inorder_del) |
138 by(simp add: delete_def inorder_del) |
152 |
139 |
153 |
140 |
154 subsection \<open>Balancedness\<close> |
141 subsection \<open>Balancedness\<close> |
155 |
142 |
156 lemma bal_upd: "bal t \<Longrightarrow> bal (tree\<^sub>i(upd x y t)) \<and> height(upd x y t) = height t" |
143 lemma bal_upd: "bal t \<Longrightarrow> bal (tree\<^sub>i(upd x y t)) \<and> height(upd x y t) = height t" |
157 by (induct t) (auto, auto split: up\<^sub>i.split) (* 33 secs (2015) *) |
144 by (induct t) (auto, auto split: up\<^sub>i.split) (* 20 secs (2015) *) |
158 |
145 |
159 lemma bal_update: "bal t \<Longrightarrow> bal (update x y t)" |
146 lemma bal_update: "bal t \<Longrightarrow> bal (update x y t)" |
160 by (simp add: update_def bal_upd) |
147 by (simp add: update_def bal_upd) |
161 |
148 |
162 |
149 |
163 lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t" |
150 lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t" |
164 by(induction x t rule: del.induct) |
151 by(induction x t rule: del.induct) |
165 (auto simp add: heights height_del_min split: prod.split) |
152 (auto simp add: heights height_del_min split: prod.split) |
|
153 (* 20 secs (2015) *) |
166 |
154 |
167 lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))" |
155 lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))" |
168 by(induction x t rule: del.induct) |
156 by(induction x t rule: del.induct) |
169 (auto simp: bals bal_del_min height_del height_del_min split: prod.split) |
157 (auto simp: bals bal_del_min height_del height_del_min split: prod.split) |
170 (* 110 secs (2015) *) |
158 (* 100 secs (2015) *) |
171 |
159 |
172 corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)" |
160 corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)" |
173 by(simp add: delete_def bal_tree\<^sub>d_del) |
161 by(simp add: delete_def bal_tree\<^sub>d_del) |
174 |
162 |
175 |
163 |