-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.c
184 lines (141 loc) · 3.66 KB
/
heap.c
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
181
182
183
184
/*****************************************************************/
/* Fila de prioridade / min-heap | PROG2 | MIEEC | 2018/19 */
/*****************************************************************/
#include "heap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define RAIZ (1)
#define PAI(x) (x/2)
#define FILHO_ESQ(x) (x*2)
#define FILHO_DIR(x) (x*2+1)
int menor_que(elemento * e1, elemento * e2);
heap* heap_nova(int capacidade)
{
heap * h = (heap *) malloc(sizeof(heap));
if (!h)
return NULL;
h->tamanho = 0;
h->capacidade = capacidade;
h->elementos = (elemento **) calloc(capacidade+1, sizeof(elemento *));
if (!h->elementos) {
free(h);
return NULL;
}
return h;
}
void heap_apaga(heap *h)
{
int i;
if(!h)
return;
/* apaga todos os elementos e respetivas strings */
for(i=RAIZ; i<=h->tamanho; i++)
{
free(h->elementos[i]->valor);
free(h->elementos[i]);
h->elementos[i]=NULL;
}
free(h->elementos);
free(h);
}
elemento * elemento_novo(int prioridade, const char * valor)
{
elemento * elem = (elemento *) malloc(sizeof(elemento));
if (!elem)
return NULL;
elem->valor = (char*)calloc(strlen(valor)+1, sizeof(char));
if (!elem->valor)
{
free(elem);
return NULL;
}
strcpy(elem->valor, valor);
elem->prioridade = prioridade;
return elem;
}
int heap_insere(heap * h, const char * texto, int prioridade)
{
elemento * aux, * elem;
int i;
/* se heap esta' cheia, nao insere elemento */
if (h->tamanho >= h->capacidade)
return 0;
elem = elemento_novo(prioridade, texto);
if (!elem)
return 0;
/* coloca elemento no fim da heap */
h->tamanho++;
i = h->tamanho;
h->elementos[i] = elem;
/* enquanto elemento for mais prioritario do que o respetivo pai, troca-os */
while (i != RAIZ && menor_que(h->elementos[PAI(i)], h->elementos[i]))
{
aux = h->elementos[PAI(i)];
h->elementos[PAI(i)] = h->elementos[i];
h->elementos[i] = aux;
i = PAI(i);
}
return 1;
}
char * heap_remove(heap * h)
{
int i, filho_maior;
elemento * aux;
char * ret;
/* se heap estiver vazia, nao remove elemento */
if (!h || h->tamanho <= 0)
return NULL;
ret = h->elementos[RAIZ]->valor;
free(h->elementos[RAIZ]);
/* coloca ultimo elemento da heap na raiz */
h->elementos[RAIZ] = h->elementos[h->tamanho];
h->elementos[h->tamanho] = NULL;
h->tamanho--;
i = RAIZ;
/* enquanto nao chegar 'a base da heap */
while(FILHO_ESQ(i) <= h->tamanho)
{
filho_maior = FILHO_ESQ(i);
/* verifica se existe filho 'a direita e se este e' mais prioritario do que 'a esquerda */
if (FILHO_DIR(i) <= h->tamanho && menor_que(h->elementos[FILHO_ESQ(i)], h->elementos[FILHO_DIR(i)]))
filho_maior = FILHO_DIR(i);
/* enquanto elemento for mais prioritario do que o respetivo pai, troca-os */
if (menor_que(h->elementos[i], h->elementos[filho_maior]))
{
aux = h->elementos[filho_maior];
h->elementos[filho_maior] = h->elementos[i];
h->elementos[i] = aux;
i = filho_maior;
}
else
break;
}
return ret;
}
void heap_imprime(heap *h, int indice)
{
int i, nivel = 0;
if (indice <= h->tamanho)
{
i = indice;
while(i > 1)
{
i = i/2;
nivel++;
}
heap_imprime(h, indice*2);
for(i = 0; i < 3 * nivel; i++)
printf(" ");
printf("%s (%d)\n",h->elementos[indice]->valor, h->elementos[indice]->prioridade);
heap_imprime(h, indice*2+1);
}
}
int menor_que(elemento * e1, elemento * e2)
{
if (e1 == NULL || e2 == NULL)
{
return 0;
}
return e1->prioridade < e2->prioridade;
}