-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
180 lines (150 loc) · 3.65 KB
/
utils.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
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
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <math.h>
/** ToDo
MaxHeap, MinHeap
tabele de dispersie
algoritmi sortare
algoritmi stergere
**/
struct Articol {
int id;
char* nume;
float marime;
};
struct nod {
Articol info;
nod* st;
nod* dr;
};
// deepCopy articol
Articol deepCopy(Articol articol);
// afisare articol
void afiseazaArticol(Articol articol);
// initializarea unui articol nou
Articol newArticol(int id, char* nume, float marime);
// citirea unui articol din fisierul de date
Articol citesteArticol(FILE* f);
// numarul inregistrarilor din fisierul de date
int fileRecords(char* f_name);
// inaltimea unui arbore
int inaltimeArbore(nod* root);
// afisare arbore grafic
void afisareArbore(nod* root, Articol* heap, int heapSize);
// arbore stocat intr-o matrice breadthFirst
void breadthFirst(nod* root, int* arbore, int index);
// dezalocare spatiu ocupat arbore
void freeTree(nod* root);
Articol deepCopy(Articol articol) {
Articol temp;
temp.id = articol.id;
temp.nume = (char*)malloc(sizeof(char)*strlen(articol.nume) + 1);
strcpy(temp.nume, articol.nume);
temp.marime = articol.marime;
return temp;
}
void afiseazaArticol(Articol articol) {
printf("%03d %s %05.2f\n", articol.id, articol.nume, articol.marime);
}
Articol newArticol(int id, char* nume, float marime) {
Articol articol;
articol.id = id;
articol.nume = (char*)malloc(sizeof(char)*strlen(nume) + 1);
strcpy(articol.nume, nume);
articol.marime = marime;
return articol;
}
Articol citesteArticol(FILE* f) {
Articol articol;
if (!f) return articol;
int id;
char nume_buffer[32];
float marime;
fscanf(f,"%d\t%s\t%f",&id,nume_buffer,&marime);
articol = newArticol(id, nume_buffer, marime);
return articol;
}
int fileRecords(char* f_name) {
FILE* f = fopen(f_name, "r");
int f_lines = 0;
char buffer[1024];
while (fgets(buffer,sizeof(buffer),f)) f_lines++;
fclose(f);
return f_lines;
}
int inaltimeArbore(nod* root) {
if (root) {
int st = inaltimeArbore(root->st);
int dr = inaltimeArbore(root->dr);
return (st>dr ? st : dr) + 1;
}
return 0;
}
void breadthFirst(nod* root, int* arbore, int index=0) {
if(root) {
arbore[index]=root->info.id;
if(root->st) {
breadthFirst(root->st, arbore, 2 * index + 1);
}
if(root->dr) {
breadthFirst(root->dr, arbore, 2 * index + 2);
}
}
}
void afisareArbore(nod* root = NULL, Articol* heap = NULL, int heapSize = 0) {
if (!heap && !root) {
printf("Nicio structura de afisat!\n");
return;
}
int inaltime = 0;
int elemente = 0;
if (root && !heap) {
inaltime = inaltimeArbore(root);
elemente = pow(2,inaltime);
}
if (heap && !root) {
elemente = heapSize;
inaltime = (log2(elemente));
}
int* arbore = (int*)malloc(sizeof(int)*elemente);
for (int i=0; i<elemente; i++) {
arbore[i] = 0;
}
if (root && !heap) {
breadthFirst(root, arbore);
}
if (heap && !root) {
for (int i=0; i<elemente; i++) {
arbore[i] = heap[i].id;
}
}
// afisare structura arbore
char spacer[]=" ";
for (int i=0; i<inaltime;i++) {
for (int k=1;k<pow(2,inaltime-i)/2;k++) printf("%s",spacer);
for (int j=pow(2,i)-1; j<pow(2,i+1)-1; j++) {
if (arbore[j] != 0) {
printf("%d",arbore[j]);
} else {
printf("%s",spacer);
}
for (int k=1;k<pow(2,inaltime-i);k++) printf("%s",spacer);
}
printf("\n");
}
free(arbore);
}
void freeTree(nod* root) {
if (root) {
if (root->st) {
freeTree (root->st);
}
if (root->dr) {
freeTree (root->dr);
}
free(root->info.nume);
root->info.id = 0;
root->info.marime = 0;
}
}