-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
235 lines (198 loc) · 5.31 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/******************************************************************************
*
* File Name: heap.c
* Author: João Rodrigues, Sara Vieira
* Revision: v1.0
*
* DESCRIPTION
* funções do acervo
*
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include "heap.h"
/******************************************************************************
* FixUp()
*
* Arguments: h - pointer to heap structure
* k - index of element to fixup
* Returns:
* Side-Effects: none
*
* Description: Performs fixup from index
*
*****************************************************************************/
void FixUp(Heap * h, int k)
{
data t;
while ((k > 0) && (h->less) ((h->heapdata)[(k - 1) / 2].dist, (h->heapdata)[k].dist))
{
t = (h->heapdata)[k];
(h->heapdata)[k] = (h->heapdata)[(k - 1) / 2];
(h->heapdata)[(k - 1) / 2] = t;
k = (k - 1) / 2;
}
return;
}
/******************************************************************************
* FixDown()
*
* Arguments: h - pointer to heap structure
* k - index of element to fixdown
* Returns:
* Side-Effects: none
*
* Description: performs fixdown from index
*
*****************************************************************************/
void FixDown(Heap * h, int k)
{
int j;
data t;
while ((2 * k + 1) < h->n_elements) {
j = 2 * k + 1;
if (((j + 1) < h->n_elements) && (h->less) (h->heapdata[j].dist, h->heapdata[j + 1].dist))
{
/* second offspring is greater */
j++;
}
if (!(h->less) (h->heapdata[k].dist, h->heapdata[j].dist))
{
/* Elements are in correct order. */
break;
}
/* the 2 elements are not correctly sorted, it is
necessary to exchange them */
t = (h->heapdata)[k];
(h->heapdata)[k] = (h->heapdata)[j];
(h->heapdata)[j] = t;
k = j;
}
return;
}
/******************************************************************************
* NewHeap()
*
* Arguments: size - heap size
* less - item comparison function
* Returns: Heap
* Side-Effects: none
*
* Description: allocates space for a new heap of specified size
*
*****************************************************************************/
Heap *NewHeap(int size, int (*less) (int, int))
{
Heap *h;
h = (Heap *) malloc(sizeof(Heap));
h->n_elements = 0;
h->less = less;
h->heapdata = (data *) malloc(size * sizeof(data));
return (h);
}
/******************************************************************************
* FreeHeap()
*
* Arguments: Heap
* Returns: -
* Side-Effects: none
*
* Description: frees the memory used
*
*****************************************************************************/
void FreeHeap(Heap *h)
{
free(h->heapdata);
free(h);
}
/******************************************************************************
* Insert()
*
* Arguments: h - pointer to heap
* distance, indice do vertice
* Returns: 1
* Side-Effects: none
*
* Description: add element at the end of heap and do fixup
*
*****************************************************************************/
int Insert(Heap * h, int dist, int id)
{
h->heapdata[h->n_elements].dist = dist;
h->heapdata[h->n_elements].id = id;
h->n_elements++;
FixUp(h, h->n_elements - 1);
return 1;
}
/******************************************************************************
* Modify()
*
* Arguments: h - pointer to heap
* id- id do vertice modificado
* newvalue - new element value
* Returns: void
* Side-Effects: none
*
* Description: compares new element with antecedent, if larger do a fixup
* otherwise do fixdown
*
*****************************************************************************/
void Modify(Heap * h, int id, int newvalue)
{
int index = GetIndex(h, id);
h->heapdata[index].dist = newvalue;
FixUp(h, index);
return;
}
/******************************************************************************
* RemoveMax()
*
* Arguments: h - pointer to heap
* Returns: int
* Side-Effects: none
*
* Description: exchange first and last element of heap, remove last element
* (max element) from heap and maintain heap order by performing
* a fixdown of the first element
*
*****************************************************************************/
int RemoveMax(Heap * h)
{
data t;
if (h->n_elements > 0) {
t = (h->heapdata)[0];
(h->heapdata)[0] = (h->heapdata)[h->n_elements - 1];
(h->heapdata)[h->n_elements - 1] = t;
h->n_elements--;
FixDown(h, 0);
return t.id;
}
return -1;
}
/******************************************************************************
* GetIndex()
*
* Arguments: h - pointer to heap structure
* k - index of element to fixdown
* Returns:
* Side-Effects: none
*
* Description: performs fixdown from index
*
*****************************************************************************/
int GetIndex(Heap * h, int id)
{
int j, k = 0;
/*printf("n_elements: %d, id: %d\n", h->n_elements, id);*/
if((h->heapdata)[0].id == id)
return 0;
while ((2 * k + 1) < h->n_elements) {
j = 2 * k + 1;
if (h->heapdata[j].id == id)
return j;
if (h->heapdata[j+1].id == id)
return j+1;
k++;
}
return -1;
}