-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheap.c
116 lines (91 loc) · 1.77 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
#include <stdlib.h>
#include "heap.h"
#define PAI(x) ((x - 1) >> 1)
#define ESQ(x) ((x << 1) + 1)
#define DIR(x) ((x << 1) + 2)
struct _heap {
int max;
int pos;
int (*compare)(const void*, const void*);
void **vet;
};
static void heap_troca(void** a, void** b)
{
register void* tmp = *a;
*a = *b;
*b = tmp;
}
int heap_qtd(Heap* heap)
{
if(!heap) return 0;
else return heap->pos;
}
Heap* heap_cria(int max, int (*compare)(const void*, const void*))
{
Heap *heap = (Heap*) malloc(sizeof(Heap));
if(!heap) exit(1);
heap->max = max;
heap->pos = 0;
heap->compare = compare;
heap->vet = (void**) malloc(max*sizeof(void*));
return heap;
}
void heap_libera(Heap *heap, int liberaInfo)
{
if(heap) {
if(liberaInfo) {
int i;
for(i = 0; i < heap->pos; i++) {
free(heap->vet[i]);
}
}
free(heap->vet);
free(heap);
}
}
void heap_insere(Heap* heap, void* info)
{
int i = heap->pos, p = PAI(i);
if(!heap) return;
if(!info) return;
if(heap->pos >= heap->max) return;
heap->vet[i] = info;
heap->pos++;
while(1) {
if(p < 0) break;
if(heap->compare(heap->vet[p], heap->vet[i]) > 0) break;
heap_troca(&heap->vet[p], &heap->vet[i]);
i = p;
p = PAI(i);
}
}
void* heap_remove(Heap* heap)
{
int i, e, d, aux;
void* info;
if(!heap) return NULL;
if(!heap->pos) return NULL;
info = heap->vet[0];
if(heap->pos > 1) {
heap_troca(&heap->vet[0], &heap->vet[heap->pos-1]);
}
heap->pos--;
i = aux = 0;
e = ESQ(i);
d = DIR(i);
while(1) {
if(e < heap->pos && heap->compare(heap->vet[e], heap->vet[aux]) > 0) {
aux = e;
}
if(d < heap->pos && heap->compare(heap->vet[d], heap->vet[aux]) > 0) {
aux = d;
}
if(aux != i) {
heap_troca(&heap->vet[i], &heap->vet[aux]);
} else break;
i = aux;
e = ESQ(i);
d = DIR(i);
}
return info;
}