-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathab.c
More file actions
356 lines (316 loc) · 9.2 KB
/
Copy pathab.c
File metadata and controls
356 lines (316 loc) · 9.2 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
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
#include "ab.h"
const int t = 2;
TAB *Cria(int t){
/*
int i;
TAB* novo = (TAB*)malloc(sizeof(TAB));
novo->nchaves = 0;
novo->chave =(int*)malloc(sizeof(int*)*((t*2)-1));
novo->folha=1;
novo->filho = (TAB**)malloc(sizeof(TAB*)*t*2);
for(i=0; i<(t*2); i++) novo->filho[i] = NULL;
return novo;
*/
}
TAB *Libera(TAB *a){
if(a){
if(!a->folha){
int i;
for(i = 0; i <= a->nchaves; i++) Libera(a->filho[i]);
}
free(a->chave);
free(a->filho);
free(a);
return NULL;
}
}
void Imprime(TAB *a, int andar){
if(a){
int i,j;
for(i=0; i<=a->nchaves-1; i++){
Imprime(a->filho[i],andar+1);
for(j=0; j<=andar; j++) printf(" ");
printf("%d\n", a->chave[i]);
}
Imprime(a->filho[i],andar+1);
}
}
void Imprime_arq(char *nome){
ArTAB *a;
int i,j;
char aux[6];
a = carregaNo_arq(nome);
if(a){
printf("\nNo:%s",nome);
for(i = 0; i < a->nchaves; i++){
//Imprime_arq(aux);
printf(" %d", a->chave[i]);
}
for(i = 0; i<a->nfilhos;i++){
sprintf(aux,"%d",a->filhos[i]);
strcat(aux,".txt");
Imprime_arq(aux);
}
}
};
ArTAB* Busca_arq(char * nome, int ch){
int p, i;
char aux[6];
ArTAB *x,*resp;
resp = NULL;
x = carregaNo_arq(nome);
if(!x) return resp;
i = 0;
while(i < x->nchaves && ch > x->chave[i]) i++;
if(i < x->nchaves && ch == x->chave[i]) return x;
if(x->folha) return resp;
sprintf(aux,"%d",x->filhos[i]);
strcat(aux,".txt");
return Busca_arq(aux, ch);
};
TAB *Busca(TAB* x, int ch){
int i;
TAB *resp = NULL;
if(!x) return resp;
i = 0;
while(i < x->nchaves && ch > x->chave[i]) i++;
if(i < x->nchaves && ch == x->chave[i]) return x;
if(x->folha) return resp;
return Busca(x->filho[i], ch);
}
TAB *Inicializa(){
return NULL;
}
TAB *Divisao(TAB *x, int i, TAB* y, int t){
int j;
TAB *z=Cria(t);
z->nchaves= t - 1;
z->folha = y->folha;
for(j=0;j<t-1;j++) z->chave[j] = y->chave[j+t];
if(!y->folha){
for(j=0;j<t;j++){
z->filho[j] = y->filho[j+t];
y->filho[j+t] = NULL;
}
}
y->nchaves = t-1;
for(j=x->nchaves; j>=i; j--) x->filho[j+1]=x->filho[j];
x->filho[i] = z;
for(j=x->nchaves; j>=i; j--) x->chave[j] = x->chave[j-1];
x->chave[i-1] = y->chave[t-1];
x->nchaves++;
return x;
}
TAB *Insere_Nao_Completo(TAB *x, int k, int t){
int i = x->nchaves-1;
if(x->folha){
while((i>=0) && (k<x->chave[i])){
x->chave[i+1] = x->chave[i];
i--;
}
x->chave[i+1] = k;
x->nchaves++;
return x;
}
while((i>=0) && (k<x->chave[i])) i--;
i++;
if(x->filho[i]->nchaves == ((2*t)-1)){
x = Divisao(x, (i+1), x->filho[i], t);
if(k>x->chave[i]) i++;
}
x->filho[i] = Insere_Nao_Completo(x->filho[i], k, t);
return x;
}
void InsereArquivo(char *nome, int k, int t){
FILE *fp = fopen(nome, "rb+");
int p,i;
TAB aux;
if(!fp)
exit(1);
fseek(fp, p, SEEK_SET);
}
TAB *Insere(TAB *T, int k, int t){
if(Busca(T,k)) return T;
if(!T){
T=Cria(t);
T->chave[0] = k;
T->nchaves=1;
return T;
}
if(T->nchaves == (2*t)-1){
TAB *S = Cria(t);
S->nchaves=0;
S->folha = 0;
S->filho[0] = T;
S = Divisao(S,1,T,t);
S = Insere_Nao_Completo(S,k,t);
return S;
}
T = Insere_Nao_Completo(T,k,t);
return T;
}
TAB* remover(TAB* arv, int ch, int t){
TAB *y,*w,*z,*o;
int i,j,temp;
if(!arv) return arv;
printf("Removendo %d...\n", ch);
for(i = 0; i<arv->nchaves && arv->chave[i] < ch; i++);
if(i < arv->nchaves && ch == arv->chave[i]){ //CASOS 1, 2A, 2B e 2C
if(arv->folha){ //CASO 1
printf("\nCASO 1\n");
for(j=i; j<arv->nchaves-1;j++) arv->chave[j] = arv->chave[j+1];
arv->nchaves--;
return arv;
}
if(!arv->folha && arv->filho[i]->nchaves >= t){ //CASO 2A
printf("\nCASO 2A\n");
y = arv->filho[i]; //Encontrar o predecessor k' de k na árvore com raiz em y
while(!y->folha) y = y->filho[y->nchaves];
temp = y->chave[y->nchaves-1];
arv->filho[i] = remover(arv->filho[i], temp, t);
//Eliminar recursivamente K e substitua K por K' em x
arv->chave[i] = temp;
return arv;
}
if(!arv->folha && arv->filho[i+1]->nchaves >= t){ //CASO 2B
printf("\nCASO 2B\n");
w = arv->filho[i+1]; //Encontrar o sucessor k' de k na árvore com raiz em y
while(!w->folha) w = w->filho[0];
temp = w->chave[0];
w = remover(arv->filho[i+1], temp, t); //Eliminar recursivamente K e substitua K por K' em x
arv->chave[i] = temp;
return arv;
}
if(!arv->folha && arv->filho[i+1]->nchaves == t-1 && arv->filho[i]->nchaves == t-1){ //CASO 2C
printf("\nCASO 2C\n");
o = arv->filho[i];
z = arv->filho[i+1];
o->chave[o->nchaves] = ch; //colocar ch ao final de filho[i]
for(j=0; j<t-1; j++) //juntar chave[i+1] com chave[i]
o->chave[t+j] = z->chave[j];
for(j=0; j<=t; j++) //juntar filho[i+1] com filho[i]
o->filho[t+j] = z->filho[j];
o->nchaves = 2*t-1;
for(j=i; j < arv->nchaves-1; j++) //remover ch de arv
arv->chave[j] = arv->chave[j+1];
for(j=i+1; j <= arv->nchaves; j++) //remover ponteiro para filho[i+1]
arv->filho[j] = arv->filho[j+1];
arv->filho[j] = NULL; //Campello
arv->nchaves--;
arv->filho[i] = remover(arv->filho[i], ch, t);
return arv;
}
}
y = arv->filho[i], z = NULL;
if (y->nchaves == t-1){ //CASOS 3A e 3B
if((i < arv->nchaves) && (arv->filho[i+1]->nchaves >=t)){ //CASO 3A
printf("\nCASO 3A: i menor que nchaves\n");
z = arv->filho[i+1];
y->chave[t-1] = arv->chave[i]; //dar a y a chave i da arv
y->nchaves++;
arv->chave[i] = z->chave[0]; //dar a arv uma chave de z
for(j=0; j < z->nchaves-1; j++) //ajustar chaves de z
z->chave[j] = z->chave[j+1];
//z->chave[j] = 0; Rosseti
y->filho[y->nchaves] = z->filho[0]; //enviar ponteiro menor de z para o novo elemento em y
for(j=0; j < z->nchaves; j++) //ajustar filhos de z
z->filho[j] = z->filho[j+1];
z->nchaves--;
arv->filho[i] = remover(arv->filho[i], ch, t);
return arv;
}
if((i > 0) && (!z) && (arv->filho[i-1]->nchaves >=t)){ //CASO 3A
printf("\nCASO 3A: i igual a nchaves\n");
z = arv->filho[i-1];
for(j = y->nchaves; j>0; j--) //encaixar lugar da nova chave
y->chave[j] = y->chave[j-1];
for(j = y->nchaves+1; j>0; j--) //encaixar lugar dos filhos da nova chave
y->filho[j] = y->filho[j-1];
y->chave[0] = arv->chave[i-1]; //dar a y a chave i da arv
y->nchaves++;
arv->chave[i-1] = z->chave[z->nchaves-1]; //dar a arv uma chave de z
y->filho[0] = z->filho[z->nchaves]; //enviar ponteiro de z para o novo elemento em y
z->nchaves--;
arv->filho[i] = remover(y, ch, t);
return arv;
}
if(!z){ //CASO 3B
if(i < arv->nchaves && arv->filho[i+1]->nchaves == t-1){
printf("\nCASO 3B: i menor que nchaves\n");
z = arv->filho[i+1];
y->chave[t-1] = arv->chave[i]; //pegar chave [i] e coloca ao final de filho[i]
y->nchaves++;
for(j=0; j < t-1; j++){
y->chave[t+j] = z->chave[j]; //passar filho[i+1] para filho[i]
y->nchaves++;
}
if(!y->folha){
for(j=0; j<t; j++){
y->filho[t+j] = z->filho[j];
}
}
for(j=i; j < arv->nchaves-1; j++){ //limpar referências de i
arv->chave[j] = arv->chave[j+1];
arv->filho[j+1] = arv->filho[j+2];
}
arv->nchaves--;
arv = remover(arv, ch, t);
return arv;
}
if((i > 0) && (arv->filho[i-1]->nchaves == t-1)){
printf("\nCASO 3B: i igual a nchaves\n");
z = arv->filho[i-1];
if(i == arv->nchaves)
z->chave[t-1] = arv->chave[i-1]; //pegar chave[i] e poe ao final de filho[i-1]
else
z->chave[t-1] = arv->chave[i]; //pegar chave [i] e poe ao final de filho[i-1]
z->nchaves++;
for(j=0; j < t-1; j++){
z->chave[t+j] = y->chave[j]; //passar filho[i+1] para filho[i]
z->nchaves++;
}
if(!z->folha){
for(j=0; j<t; j++){
z->filho[t+j] = y->filho[j];
}
}
arv->nchaves--;
arv->filho[i-1] = z;
arv = remover(arv, ch, t);
return arv;
}
}
}
arv->filho[i] = remover(arv->filho[i], ch, t);
return arv;
}
TAB* retira(TAB* arv, int k, int t){
if(!arv || !Busca(arv, k)) return arv;
return remover(arv, k, t);
}
ArTAB* carregaNo_arq(char *nome){
int a,i;
FILE *fp;
ArTAB *r;
ArTAB no;
fp = fopen(nome,"r");
if(!fp) exit(1);
a = fscanf(fp,"%d %d %d",&no.nchaves, &no.nfilhos, &no.folha);
for(i = 0; i < no.nchaves; i++){
a = fscanf(fp, "%d", &no.chave[i]);
}
for(i = 0; i < no.nfilhos; i++){
a = fscanf(fp, "%d", &no.filhos[i]);
}
a = fscanf(fp, "%d", &no.pai);
r = (ArTAB*) malloc(sizeof(ArTAB));
r->nchaves = no.nchaves;
r->folha = no.folha;
r->nfilhos = no.nfilhos;
r->pai = no.pai;
for(i = 0; i < no.nchaves; i++)
r->chave[i] = no.chave[i];
for(i = 0; i < no.nfilhos; i++)
r->filhos[i] = no.filhos[i];
return r;
}