-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_monticulo.c
More file actions
147 lines (125 loc) · 4.25 KB
/
Copy pathmax_monticulo.c
File metadata and controls
147 lines (125 loc) · 4.25 KB
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
#include "max_monticulo.h"
#include <stdio.h>
#include <stdlib.h>
void nuevoMaxMonticulo(tipoMaxMonticulo* mm, int numel) {
mm->pos = -1;
mm->numEl = numel;
mm->array = (tipoElementoMaxMonticulo*)malloc(numel*sizeof(tipoElementoMaxMonticulo));
}
void swap(tipoMaxMonticulo* mm1, int pos1, int pos2) {
tipoElementoMaxMonticulo aux;
aux = mm1->array[pos1];
mm1->array[pos1] = mm1->array[pos2];
mm1->array[pos2] = aux;
}
int padre(int pos) {
return (pos-1)/2;
}
int hijoizq(int pos) {
return pos*2+1;
}
int hijoder(int pos) {
return pos*2+2;
}
void insertarMaxMonticulo(tipoMaxMonticulo* mm, Registro reg, float distancia) {
if (estaLleno(*mm)) {
printf("\nError. No se puede añadir elementos a un maxmonticulo lleno.");
} else {
int auxpos;
tipoElementoMaxMonticulo elem;
elem.distancia = distancia;
elem.reg = reg;
mm->pos += 1;
auxpos = mm->pos;
mm->array[auxpos] = elem;
while (auxpos > 0 && elem.distancia > mm->array[padre(auxpos)].distancia) {
swap(mm, auxpos, padre(auxpos));
auxpos = padre(auxpos);
}
}
}
bool compararRegistrosIguales(Registro reg1, Registro reg2) {
return (reg1.gender == reg2.gender &&
reg1.age == reg2.age &&
reg1.hypertension == reg2.hypertension &&
reg1.heart_disease == reg2.heart_disease &&
reg1.smoking_history == reg2.smoking_history &&
reg1.bmi == reg2.bmi &&
reg1.HbA1c_level == reg2.HbA1c_level &&
reg1.blood_glucose_level == reg2.blood_glucose_level &&
reg1.diabetes == reg2.diabetes);
}
bool compararElemsIguales(tipoElementoMaxMonticulo elem, tipoElementoMaxMonticulo elem2) {
return (elem.distancia == elem2.distancia && compararRegistrosIguales(elem.reg, elem2.reg));
}
void eliminarElementoIndice(tipoMaxMonticulo* mm, int pos) {
eliminarElemento(mm, mm->array[pos]);
}
int obtenerMayor(tipoMaxMonticulo mm, int pos1, int pos2) {
return (mm.array[pos1].distancia>mm.array[pos2].distancia ? pos1 : pos2);
}
void eliminarElemento(tipoMaxMonticulo* mm, tipoElementoMaxMonticulo elem) {
if (esVacio(*mm)) {
printf("\nError. No se puede eliminar elementos de un maxmonticulo vacío.");
} else {
int auxpos, i = 0;
while (i <= mm->pos && !compararElemsIguales(mm->array[i], elem)) {
i += 1;
}
if (i > mm->pos) {
printf("\nError. El elemento no existe.");
} else {
int mayor;
auxpos = i;
mm->array[auxpos] = mm->array[mm->pos];
mm->pos -= 1;
while ( hijoder(auxpos) <= mm->pos &&
(mm->array[auxpos].distancia < mm->array[hijoizq(auxpos)].distancia ||
mm->array[auxpos].distancia < mm->array[hijoder(auxpos)].distancia)) {
mayor = obtenerMayor(*mm, hijoizq(auxpos), hijoder(auxpos));
swap(mm, auxpos, mayor);
}
}
}
}
void eliminarRaiz(tipoMaxMonticulo* mm) {
if (esVacio(*mm)) {
printf("\nError. No se puede eliminar la raiz de un maxmonticulo vacío.\n");
} else {
eliminarElemento(mm, devolverRaiz(*mm));
}
}
tipoElementoMaxMonticulo devolverRaiz(tipoMaxMonticulo mm) {
if (!esVacio(mm)) {
return (mm.array[0]);
} else {
printf("\nError. No se puede devolver la raiz de un maxmonticulo vacio.");
exit(-1);
}
}
void mostrarAnchura(tipoMaxMonticulo mm) {
for (int i = 0; i <= mm.pos; i++) {
printf("%f ", mm.array[i].distancia);
}
}
bool esVacio(tipoMaxMonticulo mm) {
return (mm.pos == -1);
}
bool estaLleno(tipoMaxMonticulo mm) {
return (mm.pos == mm.numEl-1);
}
void reemplazarRaiz(tipoMaxMonticulo* mm, tipoElementoMaxMonticulo elem) {
mm->array[0] = elem;
int auxpos = 0, mayor = 0;
while (hijoder(auxpos) <= mm->pos &&
(mm->array[auxpos].distancia < mm->array[hijoizq(auxpos)].distancia ||
mm->array[auxpos].distancia < mm->array[hijoder(auxpos)].distancia)) {
mayor = obtenerMayor(*mm, hijoizq(auxpos), hijoder(auxpos));
swap(mm, auxpos, mayor);
}
}
void vaciarMaxMonticulo(tipoMaxMonticulo* mm) {
while (!esVacio(*mm)) {
eliminarRaiz(mm);
}
}