-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestHeapMain.java
359 lines (297 loc) · 6.15 KB
/
TestHeapMain.java
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import java.util.logging.*;
/**
* @author Vaikunth Sridharan Heap class & TestHeapMain class
*/
class Heap {
private int heap[];
private int costs[];
private int length = 0, minNode = 0;
private int leftChildNode = 0;
private int rightChildNode = 0;
private int temp = 0;
private int nodeParent = 0;
/**
* Heap Class constructor with field that initializes our heap & costs with
* the given size
*
* This heap will be heapified up/down & extracted according to the cost and
* not the regular heap way.
*
*
* @param
*/
public Heap(int iniSize) {
setHeap(new int[iniSize]);
setCosts(new int[iniSize]);
for (int i = 1; i < iniSize; i++) {
getCosts()[i] = Integer.MAX_VALUE;
}
}
/**
*
* This function will initialize the Heap with set of values (iterated over
* the passed value)
*
* @param return type : Integer
*/
public void initializeHeap(int value) {
for (int iHeap = 0; iHeap < value; iHeap++)
getHeap()[iHeap] = iHeap;
setLength(value);
}
/**
* Insert individual values into the heap. Note : this is used only for
* testing Heap operations
*
* @param
*
*/
public void insert(int value) {
if (getLength() == 0) {
getHeap()[getLength()] = value;
setLength(getLength() + 1);
} else {
getHeap()[getLength()] = value;
heapifyUp(getLength());
setLength(getLength() + 1);
}
}
/**
* Displaying the heap elements
*
*
*/
public void printHeap() {
for (int m = 0; m < getLength(); m++) {
System.out.print(getHeap()[m] + " ");
}
System.out.println();
}
/**
* Printing the costs
*
* @param
*/
public int printcosts(int i) {
if (getCosts()[i] != Integer.MAX_VALUE) {
return getCosts()[i];
} else {
return -1;
}
}
/**
*
*
* The algorithm mentioned in Kleinberg and Tardos Textbook (Chapter 2 Pg.
* 61)
*
*
* @param
*/
public void heapifyUp(int i) {
if (i >= 1) {
nodeParent = (i - 1) / 2;
if (getCosts()[getHeap()[i]] < getCosts()[getHeap()[nodeParent]]) {
temp = getHeap()[nodeParent];
getHeap()[nodeParent] = getHeap()[i];
getHeap()[i] = temp;
heapifyUp(nodeParent);
}
}
}
/**
*
* Checking whether the heap is empty or not, this is homologous to List
* isEmpty() function
*
* @param
*
*/
public Boolean isEmpty() {
if (getLength() == 0) {
return true;
} else
return false;
}
/**
* Function to clean the heap
*
* @param
*/
public void heapClear() {
setLength(0);
}
/**
* Function that returns me the first index, just a sample one which was
* used for testing my heap operation
*
* @param
*/
public int firstIndex() {
if (getHeap()[0] != -1)
return getHeap()[0];
else
return 0;
}
/**
* Extracting the root node of the heap which is the minimum in case of
* Min-Heap
*
* @param
*/
public int extractMin() {
if (getLength() != 0) {
setLength(getLength() - 1);
minNode = getHeap()[0];
getHeap()[0] = getHeap()[getLength()];
getHeap()[getLength()] = -1;
if (getHeap()[1] != -1) {
if (((getHeap()[2] != -1))) {
if ((getCosts()[getHeap()[0]] < getCosts()[getHeap()[2]]))
heapifyDown(0);
}
if (((getHeap()[1] != -1))) {
if (getCosts()[getHeap()[0]] < getCosts()[getHeap()[1]]) {
heapifyDown(0);
}
}
}
} else
return -1;
return minNode;
}
/**
* An important operation of Heap used by Greedy Algorithms, we change the
* key and apply heapifyDown appropriately
*
* @param w
* - weight
* @param nextNode
* - adjacent node(s)
* @param u
* - current node
* @return
*/
public int changeKey(int w, int nextNode, int u) {
int returnvalue = -2;
if (w < getCosts()[nextNode]) {
getCosts()[nextNode] = w;
returnvalue = u;
}
if (getHeap()[nextNode] != -1)
heapifyUp(nextNode);
else
heapifyDown(0);
return returnvalue;
}
/**
*
* The algorithm mentioned in Kleinberg and Tardos Textbook (Chapter 2
* Pg.63)
*
*
* @param iPointer
*/
public void heapifyDown(int iPointer) {
int iTemporary = 0;
leftChildNode = 2 * iPointer + 1;
rightChildNode = 2 * (iPointer + 1);
if (iPointer * 2 < getLength()) {
if (getHeap()[leftChildNode] != -1) {
if (getHeap()[rightChildNode] == -1) {
iTemporary = leftChildNode;
} else if (getCosts()[getHeap()[leftChildNode]] < getCosts()[getHeap()[rightChildNode]]) {
iTemporary = leftChildNode;
} else {
iTemporary = rightChildNode;
}
if (getCosts()[getHeap()[iTemporary]] < getCosts()[getHeap()[iPointer]]) {
temp = getHeap()[iTemporary];
heapifyDown(iTemporary);
getHeap()[iTemporary] = getHeap()[iPointer];
getHeap()[iPointer] = temp;
}
}
}
}
/**
* Getter method for Heap
*
* @return
*/
public int[] getHeap() {
return heap;
}
/**
* Setter method for Heap
*
* @param heap
*/
public void setHeap(int heap[]) {
this.heap = heap;
}
/**
* Getter method for Heap Length
*
* @return
*/
public int getLength() {
return length;
}
/**
* Setter method for Heap Length
*
* @param length
*/
public void setLength(int length) {
this.length = length;
}
/**
* Getter method for Heap-Cost
*
* @return
*/
public int[] getCosts() {
return costs;
}
/**
* Setter method for Heap-Cost
*
* @param costs
*/
public void setCosts(int costs[]) {
this.costs = costs;
}
/**
* Getter method for getting the cost Length
*
* @return
*/
public int getCostLength() {
return costs.length;
}
}
/**
* @author Vaikunth Sridharan
*
*/
public class TestHeapMain {
public static void main(String[] args) {
// Calling constructor with size
Heap h = new Heap(4);
// Initialize the Heap with few sample numbers
h.insert(0);
h.insert(1);
h.insert(3);
h.insert(2);
Logger loggin = Logger.getLogger("MyLog");
loggin.log(Level.WARNING,
"THIS IS JUST TO TEST HEAP!\nDO NOT EXECUTE WHEN YOU WANT TO RUN PRIMS...");
h.printHeap();
System.out.println("\n|----> " + h.extractMin());
System.out.println("Our new Heap is ");
for (int i = 0; i < h.getLength(); i++) {
System.out.print(h.getHeap()[i] + " ");
}
}
}