-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_minmax.cpp
120 lines (106 loc) · 2.69 KB
/
heap_minmax.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
#include "utils.cpp"
void adaugaArticol(Articol* heap, Articol articol, int i) {
// nu avem radacina
if (heap[i].id == NULL) {
heap[i] = deepCopy(articol);
return;
}
if (articol.id <= heap[i].id) {
adaugaArticol(heap, articol, 2*i+1);
}
if (articol.id > heap[i].id) {
adaugaArticol(heap, articol, 2*i+2);
}
}
Articol* citesteArticole(Articol* heap, 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);
adaugaArticol(heap, articol,i);
afiseazaArticol(articol);
}
fclose(f);
return heap;
}
void swap(Articol* heap, int x, int y) {
Articol temp = heap[x];
heap[x] = heap[y];
heap[y] = temp;
}
void maxHeapify(Articol* heap, int heapSize, int root) {
int st = 2*root + 1;
int dr = 2*root + 2;
int max = root;
if (st <= heapSize - 1 && heap[st].id > heap[root].id) {
max = st;
} else {
max = root;
}
if (dr <= heapSize - 1 && heap[dr].id > heap[max].id) {
max = dr;
}
if (max != root) {
swap(heap, root, max);
maxHeapify(heap, heapSize, max);
}
}
void minHeapify(Articol* heap, int heapSize, int root) {
int st = 2*root + 1;
int dr = 2*root + 2;
int min = root;
if (st <= heapSize - 1 && heap[st].id !=0 && heap[st].id < heap[root].id) {
min = st;
} else {
min = root;
}
if (dr <= heapSize - 1 && heap[dr].id !=0 && heap[dr].id < heap[min].id) {
min = dr;
}
if (min != root) {
swap(heap, root, min);
minHeapify(heap, heapSize, min);
}
}
int main() {
// arbore articole
char f_name[] = "data-structures.in";
// citire numar de linii din fisier
int items = fileRecords(f_name);
int heapSize = 1;
while(heapSize < items) heapSize*=2;
printf("heapSize:%d\n",heapSize);
// citire articole din arbore
Articol* heap = (Articol*)malloc(sizeof(Articol)*heapSize);
for (int i=0; i<heapSize; i++) {
heap[i].id = NULL;
heap[i].marime = NULL;
}
printf("Citire valori in heap:\n");
heap = citesteArticole(heap, f_name, items);
// afisare arbore
printf("Afisare structura arbore:\n");
afisareArbore(NULL, heap, heapSize);
// afisare MaxHeap
printf("Afisare arbore dupa MaxHeap:\n");
for (int i = heapSize/2 - 1; i>=0; i--) {
maxHeapify(heap, heapSize, i);
}
afisareArbore(NULL, heap, heapSize);
// afisare MinHeap
printf("Afisare arbore dupa MinHeap:\n");
for (int i = heapSize/2 - 1; i>=0; i--) {
minHeapify(heap, heapSize, i);
}
afisareArbore(NULL, heap, heapSize);
// cleanup
for (int i=0; i<heapSize;i++){
if (heap[i].id != NULL) {
free(heap[i].nume);
}
}
free(heap);
printf("Sfarsit demonstratie.\n");
return 0;
}