-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl.cpp
93 lines (77 loc) · 1.87 KB
/
avl.cpp
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
#include "utils.cpp"
int inaltime;
int factorEchilibru(nod* root) {
return (inaltimeArbore(root->st) - inaltimeArbore(root->dr));
}
nod* rotatieStanga(nod* root) {
nod* pivot = root->dr;
root->dr = pivot->st;
pivot->st = root;
return pivot;
}
nod* rotatieDreapta(nod* root) {
nod* pivot = root->st;
root->st = pivot->dr;
pivot->dr = root;
return pivot;
}
nod* adaugaArticol(nod* root, Articol articol){
nod* temp = (nod*)malloc(sizeof(nod));
temp->info = deepCopy(articol);
// nu avem radacina
if (!root) {
temp->st = NULL;
temp->dr = NULL;
return temp;
}
if (articol.id <= root->info.id) {
root->st = adaugaArticol(root->st, articol);
}
if (articol.id > root->info.id) {
root->dr = adaugaArticol(root->dr, articol);
}
if (factorEchilibru(root) == 2) {
if (factorEchilibru(root->st) == -1) {
root = rotatieStanga(root->st);
} else {
root = rotatieDreapta(root);
}
}
if (factorEchilibru(root) == -2) {
if (factorEchilibru(root->dr) == 1) {
root = rotatieDreapta(root->dr);
} else {
root = rotatieStanga(root);
}
}
return root;
}
nod* citesteArticole(nod* root, char* f_name, int items) {
// citire din fisier
FILE* f = fopen(f_name,"r");
Articol articol;
for (int i=0; i<items; i++) {
articol = citesteArticol(f);
root = adaugaArticol(root, articol);
afiseazaArticol(articol);
}
fclose(f);
return root;
}
int main() {
// arbore articole
char f_name[] = "data-structures.in";
// citire numar de linii din fisier
int items = fileRecords(f_name);
// citire articole din arbore
nod* root = NULL;
printf("Citire valori in arbore:\n");
root = citesteArticole(root, f_name, items);
// afisare avl
printf("Afisare structura arbore AVL:\n");
afisareArbore(root);
// cleanup
freeTree(root);
printf("Sfarsit demonstratie.\n");
return 0;
}