-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARNE.c
359 lines (288 loc) · 7.16 KB
/
ARNE.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Much of this code is adapted from "Algorithms in C, Third Edition,"
* by Robert Sedgewick, Addison Wesley Longman, 1998, and from
* Sedgewick's own Java code for left-leaning red-black trees.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Item.h"
#include "ARNE.h"
/*
Constantes
*/
const char no_externo[] = "\\Tn";
const char no_interno[] = "\\Tc*{3pt}";
const char tree_sep[] = "3mm";
const char level_sep[] = "5mm";
#define YES 1
#define NO 0
typedef int BOOL;
/*
* Back to Sedgewick stuff
*/
static link z;
static Item *conflict;
Item **getConflict() {
return &conflict;
}
link NEW(Item *item, link l, link r, int N, int red) {
link x = malloc(sizeof *x);
x->item = item;
x->l = l;
x->r = r;
x->N = N;
x->red = red;
return x;
}
link initTree() {
link head = z;
return head;
}
void STinit() {
z = NEW(getNULLitem(), 0, 0, 0, 0);
conflict = NULL;
}
int STcount(link head) {
return head->N;
}
#define hl (h->l)
#define hr (h->r)
#define hrl (h->r->l)
#define hll (h->l->l)
Item *searchR(link h, Key v) {
Key t = key(h->item);
if (h == z) return getNULLitem();
if (eq(v, t)) return (h->item);
if (less(v, t)) return searchR(hl, v);
else return searchR(hr, v);
}
Item *STsearch(Key v, link head) {
return searchR(head, v);
}
link fixNr(link h) {
h->N = hl->N + hr->N +1;
return h;
}
link rotL(link h) {
link x = hr; hr = x->l; x->l = fixNr(h);
x->red = x->l->red; x->l->red = 1;
return fixNr(x);
}
link rotR(link h) {
link x = hl; hl = x->r; x->r = fixNr(h);
x->red = x->r->red; x->r->red = 1;
return fixNr(x);
}
void colorFlip(link h) {
h->red = 1 - h->red;
hl->red = 1 - hl->red;
hr->red = 1 - hr->red;
}
link mvRedL(link h) {
colorFlip(h);
if (hrl->red) { hr = rotR(hr); h = rotL(h); }
return h;
}
link mvRedR(link h) {
colorFlip(h);
if (hll->red) { h = rotR(h); }
return h;
}
link balance(link h) {
if (hr->red) h = rotL(h);
if (hl->red && hll->red) h = rotR(h);
if (hl->red && hr->red) colorFlip(h);
return fixNr(h);
}
/*
* Insertions
*/
link LLRBinsert(link h, Item *item) {
Key v = key(item);
/* Insert a new node at the bottom*/
conflict = NULL;
if (h == z) {
return NEW(item, z, z, 1, 1);
}
if (less(v, key(h->item)))
hl = LLRBinsert(hl, item);
else if (eq(v, key(h->item))) { /* If the object we are trying to insert is already there,
we opt to return a pointer to the existing item, so that
the user may choose what to do (i.e. create a list of items) */
conflict = h->item;
}
else
hr = LLRBinsert(hr, item);
/* Enforce left-leaning condition */
if (hr->red && !hl->red) h = rotL(h);
if (hl->red && hll->red) h = rotR(h);
if (hl->red && hr->red) colorFlip(h);
return fixNr(h);
}
link STinsert(link head, Item *item) {
head = LLRBinsert(head, item);
head->red = 0;
return head;
}
/*
* Select and sort
*/
Item selectR(link h, int r) {
int t = hl->N;
if (t > r) return selectR(hl, r);
if (t < r) return selectR(hr, r-t-1);
return *(h->item);
}
Item STselect(link head, int r) {
return selectR(head, r);
}
void sortR(link h, void(*visit)(Item)) {
if (h!=z) {
sortR(hl, visit);
visit(*(h->item));
sortR(hr, visit);
}
}
void STsort(link head, void(*visit)(Item)) {
sortR(head, visit);
}
/*
* Print a range
*/
void print_rangeR(link h, Key lo, Key hi) {
if (h == z) return;
if (less(lo, key(h->item)))
print_rangeR(hl, lo, hi);
if ((less(lo, key(h->item)) || eq(lo, key(h->item)))
&& (less(key(h->item), hi) || eq(key(h->item), hi)))
ITEMshow(*(h->item));
if (less(key(h->item), hi))
print_rangeR(hr, lo, hi);
}
void STprint_range(link head, Key lo, Key hi) {
print_rangeR(head, lo, hi);
}
/*
* Needed for deletion
*/
Item *getMin(link h) {
if (hl == z) return h->item;
else return getMin(hl);
}
/*
* Deletions
*/
link deleteMin(link h) {
if (hl == z) { free(h); return z; }
if (!hl->red && !hll->red) h = mvRedL(h);
hl = deleteMin(hl);
return balance(h);
}
void STdeleteMin(link head) {
if (STcount(head) == 0)
{ printf("Underflow"); exit(1); }
if (!head->l->red && !head->r->red) head->red = 1;
head = deleteMin(head);
if (STcount(head) > 0) head->red = 0;
}
link deleteMax(link h) {
if (hl->red) h = rotR(h);
if (hr == z) { free(h); return z; }
if (!hr->red && !hrl->red) h = mvRedR(h);
hr = deleteMax(hr);
return balance(h);
}
void STdeleteMax(link head) {
if (STcount(head) == 0)
printf("Underflow"); exit(1);
if (!head->l->red && !head->r->red) head->red = 1;
head = deleteMax(head);
if (STcount(head) > 0) head->red = 0;
}
link deleteR(link h, Key v) {
Key t = key(h->item);
if (less(v,t)) {
if (!hl->red && !hll->red) h = mvRedL(h);
hl = deleteR(hl, v);
}
else {
if (hl->red) h = rotR(h);
if (eq(v,key(h->item)) && hr == z)
free(h); return z;
if (!hr->red && !hrl->red) h = mvRedR(h);
if (eq(v,key(h->item))) {
h->item = getMin(hr);
hr = deleteMin(hr);
}
else hr = deleteR(hr, v);
}
return balance(h);
}
void STdelete(link head, Key v) {
if (!head->l->red && !head->r->red) head->red = 1;
head = deleteR(head, v);
if (STcount(head) > 0) head->red = 0;
}
/*
* Integrity check
int size(link h) { return h->N; }
int height(link h)
{ int tl, tr;
if (h==z) return 0;
tl = height(hl);
tr = height(hr);
return 1 + tl > tr ? tl : tr;
}
int STheight() { return height(head); }
int check()
{
if (!isBST()) printf("Fails the property of BST.\n");
if (!sizeConsistent()) printf("Fails the size consistency.\n");
if (!rankConsistent()) printf("Fails the rank consistency.\n");
if (!is23()) printf("Not a 2-3 tree.\n");
if (!balanced()) printf("Not balanced.\n");
return
isBST() && sizeConsistent() && rankConsistent()
&& is23() && balanced();
}
*/
/* Drawing? */
/* faz_desenho(): deveria chamar faca_desenho();
acho que nao deveria devolver valor nenhum...
*/
int faz_desenho(link h, int nivel) {
int d1, d2;
if (h->N == 0) {
printf("%s\n", no_externo);
return 1;
}
if (nivel == 0)
printf("\\pstree[levelsep=%s, treesep=%s]{%s}{\n", level_sep,
tree_sep, no_interno);
else
printf("\\pstree{%s}{\n", no_interno);
d1 = faz_desenho(h->l, nivel+1);
d2 = faz_desenho(h->r, nivel+1);
printf("}\n");
return d1 + d2 + 1;
}
void STdraw(link head) {
printf("\\rput{90}{");
faz_desenho(head, 0);
printf("}\n");
}
void printR(link h, int ind) {
int i;
if (h != z) {
for (i=0; i<ind; i++) putchar(' ');
printf("%s%c\n", key(h->item), h->red?'*':' ');
printR(hl, ind+2);
printR(hr, ind+2);
}
}
void STprint(link head) {
printf("\n**** %d keys ****\n", head->N);
printR(head, 0);
printf("**** ****** ****\n");
}