-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathavl.ml
More file actions
283 lines (235 loc) · 7.94 KB
/
Copy pathavl.ml
File metadata and controls
283 lines (235 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
type comparison =
| Eq
| Lt
| Gt
type compareSpecT =
| CompEqT
| CompLtT
| CompGtT
type 'a sig0 =
'a
(* singleton inductive, whose constructor was exist *)
type sumbool =
| Left
| Right
type 'a eqDec = 'a -> 'a -> sumbool
type 'a ordered = { eq_dec : 'a eqDec; compare : ('a -> 'a -> comparison); compare_spec : ('a -> 'a -> compareSpecT) }
(** val compare_spec : 'a1 ordered -> 'a1 -> 'a1 -> compareSpecT **)
let compare_spec x = x.compare_spec
type a (* AXIOM TO BE REALIZED *)
(** val ordA : a ordered **)
let ordA =
failwith "AXIOM TO BE REALIZED"
type balanceFactor =
| HiLeft
| Flat
| HiRight
| NoSubs
type avltree =
| Leaf
| Node of balanceFactor * avltree * a * avltree
type findResult =
| Found
| NotFound
(** val find : a -> avltree -> findResult **)
let rec find x = function
| Leaf -> NotFound
| Node (b, tl, d, tr) ->
(match ordA.compare_spec x d with
| CompEqT -> Found
| CompLtT -> find x tl
| CompGtT -> find x tr)
(** val bof : avltree -> balanceFactor **)
let bof = function
| Leaf -> NoSubs
| Node (b, t1, d, t2) -> b
(** val rotateRight : avltree -> a -> avltree -> avltree **)
let rotateRight tl d tr =
match tl with
| Leaf -> assert false (* absurd case *)
| Node (b, tl0, d0, tr0) ->
(match b with
| HiLeft -> Node (Flat, tl0, d0, (Node (Flat, tr0, d, tr)))
| HiRight ->
(match tr0 with
| Leaf -> assert false (* absurd case *)
| Node (b0, tl1, d1, tr1) ->
(match b0 with
| HiLeft -> Node (Flat, (Node (Flat, tl0, d0, tl1)), d1, (Node (HiRight, tr1, d, tr)))
| Flat -> Node (Flat, (Node (Flat, tl0, d0, tl1)), d1, (Node (Flat, tr1, d, tr)))
| HiRight -> Node (Flat, (Node (HiLeft, tl0, d0, tl1)), d1, (Node (Flat, tr1, d, tr)))
| NoSubs -> assert false (* absurd case *)))
| _ -> assert false (* absurd case *))
(** val rotateLeft : avltree -> a -> avltree -> avltree **)
let rotateLeft tl d = function
| Leaf -> assert false (* absurd case *)
| Node (b, tl0, d0, tr0) ->
(match b with
| HiLeft ->
(match tl0 with
| Leaf -> assert false (* absurd case *)
| Node (b0, tl1, d1, tr1) ->
(match b0 with
| HiLeft -> Node (Flat, (Node (Flat, tl, d, tl1)), d1, (Node (HiRight, tr1, d0, tr0)))
| Flat -> Node (Flat, (Node (Flat, tl, d, tl1)), d1, (Node (Flat, tr1, d0, tr0)))
| HiRight -> Node (Flat, (Node (HiLeft, tl, d, tl1)), d1, (Node (Flat, tr1, d0, tr0)))
| NoSubs -> assert false (* absurd case *)))
| HiRight -> Node (Flat, (Node (Flat, tl, d, tl0)), d0, tr0)
| _ -> assert false (* absurd case *))
(** val raised : avltree -> avltree -> bool **)
let raised ti to0 =
let s = bof ti in
(match s with
| Flat ->
let s0 = bof to0 in
(match s0 with
| HiLeft -> true
| HiRight -> true
| _ -> false)
| NoSubs ->
let s0 = bof to0 in
(match s0 with
| Flat -> true
| _ -> false)
| _ -> false)
type insertResult =
| FoundByInsert
| Inserted of avltree
(** val ifitl : balanceFactor -> avltree -> a -> avltree -> insertResult **)
let ifitl b to0 d tr =
match b with
| HiLeft -> Inserted (rotateRight to0 d tr)
| Flat -> Inserted (Node (HiLeft, to0, d, tr))
| HiRight -> Inserted (Node (Flat, to0, d, tr))
| NoSubs -> assert false (* absurd case *)
(** val ifitr : balanceFactor -> avltree -> a -> avltree -> insertResult **)
let ifitr b tl d to0 =
match b with
| HiLeft -> Inserted (Node (Flat, tl, d, to0))
| Flat -> Inserted (Node (HiRight, tl, d, to0))
| HiRight -> Inserted (rotateLeft tl d to0)
| NoSubs -> assert false (* absurd case *)
(** val insert : a -> avltree -> insertResult **)
let rec insert x = function
| Leaf -> Inserted (Node (Flat, Leaf, x, Leaf))
| Node (b, tl, d, tr) ->
(match ordA.compare_spec x d with
| CompEqT -> FoundByInsert
| CompLtT ->
(match insert x tl with
| FoundByInsert -> FoundByInsert
| Inserted to0 ->
(match raised tl to0 with
| false -> Inserted (Node (b, to0, d, tr))
| true -> ifitl b to0 d tr))
| CompGtT ->
(match insert x tr with
| FoundByInsert -> FoundByInsert
| Inserted to0 ->
(match raised tr to0 with
| false -> Inserted (Node (b, tl, d, to0))
| true -> ifitr b tl d to0)))
type delout =
avltree
(* singleton inductive, whose constructor was Delout *)
(** val lowered : avltree -> avltree -> bool **)
let lowered ti to0 =
let s = bof ti in
(match s with
| Flat ->
let s0 = bof to0 in
(match s0 with
| NoSubs -> true
| _ -> false)
| NoSubs -> false
| _ ->
let s0 = bof to0 in
(match s0 with
| Flat -> true
| _ -> false))
(** val dRotateRight : avltree -> a -> avltree -> delout **)
let dRotateRight tl m tr =
match tl with
| Leaf -> assert false (* absurd case *)
| Node (b, tl0, d, tr0) ->
(match b with
| HiLeft -> Node (Flat, tl0, d, (Node (Flat, tr0, m, tr)))
| Flat -> Node (HiRight, tl0, d, (Node (HiLeft, tr0, m, tr)))
| HiRight ->
(match tr0 with
| Leaf -> assert false (* absurd case *)
| Node (b0, tl1, d0, tr1) ->
(match b0 with
| HiLeft -> Node (Flat, (Node (Flat, tl0, d, tl1)), d0, (Node (HiRight, tr1, m, tr)))
| Flat -> Node (Flat, (Node (Flat, tl0, d, tl1)), d0, (Node (Flat, tr1, m, tr)))
| HiRight -> Node (Flat, (Node (HiLeft, tl0, d, tl1)), d0, (Node (Flat, tr1, m, tr)))
| NoSubs -> assert false (* absurd case *)))
| NoSubs -> assert false (* absurd case *))
(** val dRotateLeft : avltree -> a -> avltree -> delout **)
let dRotateLeft tl m = function
| Leaf -> assert false (* absurd case *)
| Node (b, tl0, d, tr0) ->
(match b with
| HiLeft ->
(match tl0 with
| Leaf -> assert false (* absurd case *)
| Node (b0, tl1, d0, tr1) ->
(match b0 with
| HiLeft -> Node (Flat, (Node (Flat, tl, m, tl1)), d0, (Node (HiRight, tr1, d, tr0)))
| Flat -> Node (Flat, (Node (Flat, tl, m, tl1)), d0, (Node (Flat, tr1, d, tr0)))
| HiRight -> Node (Flat, (Node (HiLeft, tl, m, tl1)), d0, (Node (Flat, tr1, d, tr0)))
| NoSubs -> assert false (* absurd case *)))
| Flat -> Node (HiLeft, (Node (HiRight, tl, m, tl0)), d, tr0)
| HiRight -> Node (Flat, (Node (Flat, tl, m, tl0)), d, tr0)
| NoSubs -> assert false (* absurd case *))
(** val dFitLeft : balanceFactor -> delout -> avltree -> a -> avltree -> delout **)
let dFitLeft b dr tl d tr =
match lowered tl dr with
| false -> Node (b, dr, d, tr)
| true ->
(match b with
| HiLeft -> Node (Flat, dr, d, tr)
| Flat -> Node (HiRight, dr, d, tr)
| HiRight -> dRotateLeft dr d tr
| NoSubs -> assert false (* absurd case *))
(** val dFitRight : balanceFactor -> delout -> avltree -> a -> avltree -> delout **)
let dFitRight b dr tl d tr =
match lowered tr dr with
| false -> Node (b, tl, d, dr)
| true ->
(match b with
| HiLeft -> dRotateRight tl d dr
| Flat -> Node (HiLeft, tl, d, dr)
| HiRight -> Node (Flat, tl, d, dr)
| NoSubs -> assert false (* absurd case *))
type delminResult =
| NoMin
| MinDeleted of a * delout
(** val delmin : avltree -> delminResult **)
let rec delmin = function
| Leaf -> NoMin
| Node (b, tl, d, tr) ->
(match delmin tl with
| NoMin -> MinDeleted (d, tr)
| MinDeleted (m, dr) -> MinDeleted (m, (dFitLeft b dr tl d tr)))
type deleteResult =
| DelNotFound
| Deleted of delout
(** val delete : a -> avltree -> deleteResult **)
let rec delete x = function
| Leaf -> DelNotFound
| Node (b, tl, d, tr) ->
(match ordA.compare_spec x d with
| CompEqT ->
let h = delmin tr in
(match h with
| NoMin -> Deleted tl
| MinDeleted (m, dr) -> Deleted (dFitRight b dr tl m tr))
| CompLtT ->
(match delete x tl with
| DelNotFound -> DelNotFound
| Deleted dr -> Deleted (dFitLeft b dr tl d tr))
| CompGtT ->
(match delete x tr with
| DelNotFound -> DelNotFound
| Deleted dr -> Deleted (dFitRight b dr tl d tr)))