You are on page 1of 7

1: //arbori binari

2: #include <iostream>//pt cout


3: #include <stdlib.h>
4:
5: using namespace std;//pt scrierea prescurtata, fara std::cout, etc
6:
7: typedef int info;
8:
9: typedef struct nod{
10: info informatie;
11: nod *ss;//subarbore stang
12: nod *sd;//subarbore drept
13: } NOD, *PNOD;//arbore de cautare
14:
15: typedef struct nodAVL{
16: int informatie;
17: int echilibru;
18: nodAVL *ss, *sd;
19: } NODAVL, *PNODAVL;//arbore de cautare echilibrat
20:
21: PNOD arboreDinVector(int *v, int dim);
22:
23: void arboreAfisareParanteze(PNOD rad);//RSD - preordine
24:
25: void stergeArbore(PNOD rad);
26:
27: int nrNoduri(PNOD rad);
28:
29: int inaltime(PNOD rad);
30:
31: PNODAVL insereazaAVL(PNODAVL rad, int nr);
32:
33: void arboreAfisareAVL(PNODAVL rad);
34:
35: PNODAVL stergeNodAVL(int nr, PNODAVL rad);
36:
37: PNODAVL stergeNodAVLechil(int nr, PNODAVL rad);
38:
39: bool cautaInformatieAVL(PNODAVL rad, int informatie);
40:
41: int main(){
42:
43: info tablou[7] = {1, -2, 3, 4, -5, -1, 2};
44: PNOD rad = NULL;
45:
46: cout << "Arbore din vector" << endl;
47: rad = arboreDinVector(tablou, 7);
48: arboreAfisareParanteze(rad); cout << endl;
49:
50: cout << "Arborele are " << nrNoduri(rad) << " noduri" << endl;
51:
52: cout << "Arborele are inaltimea " << inaltime(rad) << endl;
53:
54: cout << "Stergem arborele";
55: stergeArbore(rad); rad = NULL;
56: arboreAfisareParanteze(rad); cout << endl;
57:
58: PNODAVL radAVL = NULL;
59:
60: int tablouAVL[7] = {1, 2, 3, 4, 5, 6, 7};
61: for(int i=0; i<7; i++) radAVL = insereazaAVL(radAVL,tablouAVL[i]);
62:
63: arboreAfisareAVL(radAVL);
64: cout << endl;
65:
66: // cout << "Cauta 5" << endl << cautaInformatieAVL(radAVL,5) << endl << endl;
67: //
68: // cout << "Sterge 4" << endl;
69: // radAVL = stergeNodAVL(4, radAVL);
70: // arboreAfisareAVL(radAVL);
71: // cout << endl << endl;
72: //
73: // cout << "Sterge 5" << endl;
74: // radAVL = stergeNodAVL(5, radAVL);
75: // arboreAfisareAVL(radAVL);
76: // cout << endl << endl;
77: //
78: // cout << "Adauga 4" << endl;
79: // radAVL = insereazaAVL(radAVL, 4);
80: // arboreAfisareAVL(radAVL);
81: // cout << endl << endl;
82: //
83: // cout << "Adauga 5" << endl;
84: // radAVL = insereazaAVL(radAVL, 5);
85: // arboreAfisareAVL(radAVL);
86: // cout << endl << endl;
87:
88: cout << "Sterge 2" << endl;
89: radAVL = stergeNodAVL(2, radAVL);
90: arboreAfisareAVL(radAVL);
91: cout << endl << endl;
92:
93: cout << "Sterge 3" << endl;
94: radAVL = stergeNodAVL(3, radAVL);
95: arboreAfisareAVL(radAVL);
96: cout << endl << endl;
97:
98: cout << "Sterge 1" << endl;
99: radAVL = stergeNodAVL(1, radAVL);
100: arboreAfisareAVL(radAVL);
101: cout << endl << endl;
102:
103:
104: radAVL = insereazaAVL(radAVL, 3);
105: arboreAfisareAVL(radAVL);
106: cout << endl << endl;
107:
108: radAVL = insereazaAVL(radAVL, 2);
109: arboreAfisareAVL(radAVL);
110: cout << endl << endl;
111:
112: radAVL = insereazaAVL(radAVL, 1);
113: arboreAfisareAVL(radAVL);
114: cout << endl << endl;
115:
116:
117: cout << "Sterge 2 cu reechilibrare arbore" << endl;
118: radAVL = stergeNodAVLechil(2, radAVL);
119: arboreAfisareAVL(radAVL);
120: cout << endl << endl;
121:
122: cout << "Sterge 3 cu reechilibrare arbore" << endl;
123: radAVL = stergeNodAVLechil(3, radAVL);
124: arboreAfisareAVL(radAVL);
125: cout << endl << endl;
126:
127: cout << "Sterge 1 cu reechilibrare arbore" << endl;
128: radAVL = stergeNodAVLechil(1, radAVL);
129: arboreAfisareAVL(radAVL);
130: cout << endl << endl;
131:
132: system("PAUSE");//in stdlib
133: return 0;
134: }
135:
136:
137: PNOD arboreDinVector(int *v, int dim){
138: PNOD rad = new NOD, temp, radTemp;
139: if(!rad){
140: cout << "Nu mai este loc liber in memorie";
141: return rad;
142: }
143: rad->informatie = v[0];
144: rad->ss = NULL;
145: rad->sd = NULL;
146:
147: for(int i = 1; i < dim; i++){
148: temp = new NOD;
149: if(!temp){
150: cout << "Nu mai este loc liber in memorie";
151: return rad;
152: }
153: temp->informatie = v[i];
154: temp->ss = NULL;
155: temp->sd = NULL;
156: radTemp = rad;
157: while(radTemp){
158: if(radTemp->informatie > v[i])
159: if(radTemp->ss == NULL){
160: radTemp->ss = temp;
161: break;
162: }
163: else radTemp = radTemp->ss;
164: else
165: if(radTemp->sd == NULL){
166: radTemp->sd = temp;
167: break;
168: }
169: else radTemp = radTemp->sd;
170: }
171: }
172: return rad;
173: }
174:
175:
176: void arboreAfisareParanteze(PNOD rad){
177: if(rad){
178: cout << rad->informatie;
179: if((rad->ss!=NULL)||(rad->sd!=NULL)){
180: cout << '(';
181: arboreAfisareParanteze(rad->ss);
182: cout << ",";
183: arboreAfisareParanteze(rad->sd);
184: cout << ")";
185: }
186: }
187: else cout << "-";
188: }
189:
190:
191: void stergeArbore(PNOD rad){
192: if(rad->ss) stergeArbore(rad->ss);
193: if(rad->sd) stergeArbore(rad->sd);
194: delete rad;
195: }
196:
197: int nrNoduri(PNOD rad){
198: if ( rad == NULL ) return 0; //arbore gol
199:
200: //avem cel putin nodul radacina
201: return (1 + nrNoduri(rad->ss) + nrNoduri(rad->sd));
202: }
203:
204:
205: int inaltime(PNOD rad){
206: if ( rad == NULL ) return 0; //arbore gol
207:
208: int inaltimeSS, inaltimeSD; //avem cel putin nodul radacina
209: inaltimeSS = inaltime(rad->ss);
210: inaltimeSD = inaltime(rad->sd);
211: return (inaltimeSS>inaltimeSD)?(inaltimeSS+1):(inaltimeSD+1); // Return
the total
212: }
213:
214:
215:
216: //arbori AVL
217:
218: int inaltimeAVL(PNODAVL rad){
219: if ( rad == NULL ) return 0; //arbore gol
220: int inaltimeSS, inaltimeSD; //avem cel putin nodul radacina
221: inaltimeSS = inaltimeAVL(rad->ss);
222: inaltimeSD = inaltimeAVL(rad->sd);
223: return ((inaltimeSS>inaltimeSD)?(inaltimeSS+1):(inaltimeSD+1)); // Return
the total
224: }
225:
226: void calculEchilibru(PNODAVL rad){
227: rad->echilibru = inaltimeAVL(rad->sd) - inaltimeAVL(rad->ss);
228: }
229:
230: PNODAVL sRotireStanga(PNODAVL rad){//rotire simpla stanga
231: PNODAVL temp;
232: temp = rad->sd;
233: rad->sd = temp->ss;
234: temp->ss = rad;
235: calculEchilibru(rad);
236: calculEchilibru(temp);
237: rad = temp;
238: return rad;
239: }
240:
241: PNODAVL sRotireDreapta(PNODAVL rad){//rotire simpla dreapta
242: PNODAVL temp;
243: temp = rad->ss;
244: rad->ss = temp->sd;
245: temp->sd = rad;
246: calculEchilibru(rad);
247: calculEchilibru(temp);
248: rad = temp;
249: return rad;
250: }
251:
252: PNODAVL dRotireStanga(PNODAVL rad){//rotire dubla right-left
253: rad->sd = sRotireDreapta(rad->sd);
254: rad = sRotireStanga(rad);
255: return rad;
256: }
257:
258: PNODAVL dRotireDreapta(PNODAVL rad){//rotire dubla left-right
259: rad->ss = sRotireStanga(rad->ss);
260: rad = sRotireDreapta(rad);
261: return rad;
262: }
263:
264: PNODAVL echilibrare(PNODAVL rad){
265: PNODAVL temp;
266: calculEchilibru(rad);
267: if(rad->echilibru == -2){
268: temp = rad->ss;
269: if (temp->echilibru == 1) rad = dRotireDreapta(rad);
270: else rad = sRotireDreapta(rad);
271: }
272: else if(rad->echilibru == 2){
273: temp = rad->sd;
274: if (temp->echilibru == -1) rad = dRotireStanga(rad);
275: else rad = sRotireStanga(rad);
276: }
277: return rad;
278: }
279:
280: PNODAVL insereazaAVL(PNODAVL rad, int nr){
281: if(!rad){
282: rad = new NODAVL;
283: rad->informatie = nr;
284: rad->echilibru = 0;
285: rad->sd = NULL;
286: rad->ss = NULL;
287: return rad;
288: }
289: else if (nr < rad->informatie) rad->ss = insereazaAVL(rad->ss, nr);
290: else if (nr > rad->informatie) rad->sd = insereazaAVL(rad->sd, nr);
291: else printf("Nodul exista deja");
292: rad = echilibrare(rad);
293: return rad;
294: }
295:
296: void arboreAfisareAVL(PNODAVL rad){
297: if(rad){
298: cout << rad->informatie;
299: if((rad->ss!=NULL)||(rad->sd!=NULL)){
300: cout << "(";
301: arboreAfisareAVL(rad->ss);
302: cout << ",";
303: arboreAfisareAVL(rad->sd);
304: cout << ")";
305: }
306: }
307: else cout << "-";
308: }
309:
310: PNODAVL removeMin(PNODAVL rad){//sterge val minima din arbore
311: PNODAVL temp = rad;
312: if (!rad) cout << "arbore gol" << endl;
313: else if (rad->ss) rad->ss = removeMin(rad->ss);//parcurgem arborele pe
stanga
314: else{
315: rad = rad->sd;//leg sd si eliberez radacina
316: delete temp;
317: }
318:
319: return rad;
320: }
321:
322: PNODAVL stergeNodAVL(int nr, PNODAVL rad){//sterge nr din arbore, neconsiderand
mentinerea echilibrului arborelui
323: PNODAVL temp = rad;
324: if (!rad) cout << "nu exista acest nr in arbore" << endl;
325: else if(nr < rad->informatie) rad->ss = stergeNodAVL(nr, rad->ss);
326: else if(nr > rad->informatie) rad->sd = stergeNodAVL(nr, rad->sd);
327: else if (rad->ss != NULL && rad->sd != NULL){//am gasit nr, si are
si ss si sd
328: temp = temp->sd;//cautam val cea mai mica in sd
329: while(temp->ss) temp = temp->ss;
330: rad->informatie = temp->informatie;
331: rad->sd = removeMin(rad->sd);//si o eliminam
332: }
333: else{
334: rad = (rad->ss != NULL) ? rad->ss : rad->sd;
335: //atasam unul din copii radacinii
336: delete temp;//eliberare vechea radacina
337: }
338: return rad;
339: }
340:
341: PNODAVL reechilibrare(PNODAVL rad, int nr){
342: if(!rad) return NULL;
343: if(nr < rad->informatie) rad->ss = reechilibrare(rad->ss, nr);
344: else if (nr > rad->informatie) rad->sd = reechilibrare(rad->sd, nr);
345: rad = echilibrare(rad);
346: return rad;
347: }
348:
349: PNODAVL stergeNodAVLechil(int nr, PNODAVL rad){//sterge nr din arbore,
neconsiderand mentinerea echilibrului arborelui si echilibreaza apoi arborele
350: if((rad->ss == NULL)&&(rad->sd == NULL)) {delete rad; return NULL;}
351:
352: PNODAVL curent = rad, anterior = NULL;
353:
354: while(curent){
355: if(nr < curent->informatie) {anterior = curent; curent = curent->ss;}
356: else if(nr > curent->informatie) {anterior = curent; curent = curent-
>sd;}
357: else if(curent->ss != NULL && curent->sd != NULL){//am gasit nr, si are
si ss si sd
358: PNODAVL temp = curent->sd, anteriorTemp = NULL;//cautam val cea mai
mica in sd
359: while(temp->ss){
360: anteriorTemp = temp;
361: temp = temp->ss;
362: }
363: curent->informatie = temp->informatie;
364: curent->sd = removeMin(curent->sd);//si o eliminam
365: if(anteriorTemp) rad = reechilibrare(rad, anteriorTemp->informatie);
366: break;
367: }
368: else{
369: PNODAVL temp;
370: temp = (curent->ss != NULL) ? curent->ss : curent->sd;//atasam unul
din copii radacinii
371: if(anterior == NULL) return temp;
372: if(anterior->ss->informatie == nr) anterior->ss = temp;
373: else anterior->sd = temp;
374: rad = reechilibrare(rad, anterior->informatie);
375: delete curent;//eliberare nod
376: break;
377: }
378: }
379:
380: if (!curent) cout << "nu exista acest nr in arbore" << endl;
381:
382: return rad;
383: }
384:
385:
386: bool cautaInformatieAVL(PNODAVL rad, int informatie){
387: bool temp = false;
388: if(!rad) return temp;
389: // else cout << rad->informatie;
390: if(rad->informatie == informatie) temp = true;
391: if(!temp) temp = temp | cautaInformatieAVL(rad->ss, informatie);
392: if(!temp) temp = temp | cautaInformatieAVL(rad->sd, informatie);
393: return temp;
394: }
395:

You might also like