-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharbore.cpp
84 lines (74 loc) · 1.92 KB
/
arbore.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
#include "utils.cpp"
int inaltime;
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);
}
return root;
}
Articol * cautaArticol(nod* root, int value) {
inaltime++;
if (!root) return 0;
if (value == root->info.id) return &root->info;
if (value <= root->info.id) return cautaArticol(root->st, value);
if (value > root->info.id) return cautaArticol(root->dr, value);
return NULL;
}
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);
// cautare articol in arbore
while (true) {
printf("Introduceti id-ul de cautat:");
int id = 0;
inaltime = 0;
scanf("%d",&id);
// iesire daca id = 0
if (id == 0) break;
Articol * p_articol = cautaArticol(root, id);
printf("Inaltime nod cautat:%d\n",inaltime);
Articol articol;
if (p_articol) {
articol = * p_articol;
afiseazaArticol(articol);
} else {
printf("Nodul nu a fost gasit!\n");
}
}
// afisare arbore
printf("Afisare structura arbore:\n");
afisareArbore(root);
// cleanup
freeTree(root);
printf("Sfarsit demonstratie.\n");
return 0;
}