-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinomialHeap.java
More file actions
453 lines (399 loc) · 8.21 KB
/
Copy pathBinomialHeap.java
File metadata and controls
453 lines (399 loc) · 8.21 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* BinomialHeap
*
* An implementation of binomial heap over positive integers.
*
*/
public class BinomialHeap
{
public int size;
public HeapNode last;
public HeapNode min;
int deletedNodesDegreesSum = 0;
int linksCnt=0;
// constructors
public BinomialHeap() {
};
public BinomialHeap(int size, BinomialHeap.HeapNode last, BinomialHeap.HeapNode min) {
super();
this.size = size;
this.last = last;
this.min = min;
}
/**
* @pre x != Null and y != Null
* @pre x.next == x && y.next == y
*
*
* @return new k+1 degree binomial tree
*/
public HeapNode link(HeapNode x, HeapNode y) {
if (x.getKey() > y.getKey()) {
HeapNode temp = x;
x = y;
y = temp;
}
x.setChild(y);
return x;
}
/**
*
* pre: key > 0
*
* Insert (key,info) into the heap and return the newly generated HeapItem.
*
*/
public HeapItem insert(int key, String info) {
HeapItem item = new HeapItem(null, key, info);
HeapNode node = new HeapNode(item, null, null, null, 0);
node.next = node;
item.node = node;
BinomialHeap binHeap = new BinomialHeap(1, node, node);
this.meld(binHeap);
return item;
}
/**
*
* Delete the minimal item
*
*/
public void deleteMin() {
if (this.empty()) {
return;
}
// if min is the only node in the tree
deletedNodesDegreesSum += this.min.rank;
if (this.calculateSize() == 1) {
last = null;
min = null;
this.size = 0;
return;
}
HeapNode childOfMin = min.child;
HeapNode min = this.min;
if (this.numTrees() == 1) {
this.last = null;
this.min = null;
this.size = 0;
min.child = null;
} else {
HeapNode prev = min;
while (prev.next != min) {
prev = prev.next;
}
prev.next = min.next;
min.next = min;
min.child = null;
if (this.last == this.min) {
this.last = prev;
}
}
//min doesnt have children
if (min.rank == 0) {
min.next = min;
this.size -= 1;
updateMin();
return;
}
//get the new trees
BinomialHeap newBinHeap = new BinomialHeap();
newBinHeap.last = childOfMin;
int r = childOfMin.rank;
for (int i = 0; i <= r; i++) {
childOfMin.parent = null;
childOfMin = childOfMin.next;
}
newBinHeap.min = newBinHeap.updateMin().node;
newBinHeap.size = newBinHeap.calculateSize();
this.meld(newBinHeap);
updateMin();
}
/**
*
* Return the minimal HeapItem, null if empty.
*
*/
public HeapItem findMin() {
if (this.last == null) {
return null;
}
return this.min.item;
}
/**
* Update the minimal HeapItem Return the minimal HeapItem
*
*/
public HeapItem updateMin() {
if (this.last == null) {
return null;
}
HeapNode min = this.last;
HeapNode curr = this.last;
do {
if (curr.item.key < min.item.key) {
min = curr;
}
curr = curr.next;
} while (curr != last);
this.min = min;
return min.item;
}
/**
* pre: 0<diff<item.key
*
* Decrease the key of item by diff and fix the heap.
*/
public void decreaseKey(HeapItem item, int diff) {
item.key = item.key - diff;
//fix heap
HeapNode parent = item.node.parent;
if (parent == null) {
if (item.key < min.getKey()) {
min = item.node;
}
return;
}
while (parent != null) {
HeapItem parentItem = parent.item;
if (parentItem.key > item.key) {
parentItem.swapNodes(item);
} else {
break;
}
parent = parent.parent;
}
if (item.key < min.item.key) {
min = item.node;
}
}
/**
*
* Delete the item from the heap.
*
*/
public void delete(HeapItem item) {
decreaseKey(item, item.key+min.getKey()+1);
deleteMin();
return;
}
/**
*
* Meld the heap with heap2
*
*/
public void meld(BinomialHeap heap2) {
//one of the heaps or both is empty
if (heap2.empty()) {
return;
} else if (this.empty()) {
this.last = heap2.last;
this.size = heap2.size;
this.min = heap2.min;
return;
}
int newLen = Math.max(this.last.rank, heap2.last.rank) + 2;
HeapNode[] thisArr = new HeapNode[newLen];
HeapNode[] heap2Arr = new HeapNode[newLen];
HeapNode[] result = new HeapNode[newLen];
HeapNode curr = this.last;
do {
thisArr[curr.rank] = curr;
curr = curr.next;
} while (curr != this.last);
curr = heap2.last;
do {
heap2Arr[curr.rank] = curr;
curr = curr.next;
} while (curr != heap2.last);
// Binary
for (int i = 0; i < result.length - 1; i++) {
HeapNode t1 = thisArr[i];
HeapNode t2 = heap2Arr[i];
if (t1 != null) {
t1.next = t1;
}
if (t2 != null) {
t2.next = t2;
}
if (result[i] != null) {// carry == true
if (t1 != null && t2 != null) {
result[i + 1] = link(t1, t2);
linksCnt++;
} else if (t1 != null && t2 == null) {
result[i + 1] = link(t1, result[i]);
linksCnt++;
result[i] = null;
} else if (t1 == null && t2 != null) {
result[i + 1] = link(result[i], t2);
linksCnt++;
result[i] = null;
}
} else {// curry == false
if (t1 != null && t2 != null) {
result[i + 1] = link(t1, t2);
linksCnt++;
} else if (t1 != null && t2 == null) {
result[i] = t1;
} else if (t1 == null && t2 != null) {
result[i] = t2;
}
}
}
//build Heap from result
this.last = null;
HeapNode smallestTree = null;
HeapNode biggestTree = null;
int i = 0;
while (i < result.length) {
if (result[i] != null) {
if (smallestTree == null) {
smallestTree = result[i];
biggestTree = result[i];
}
int j = i + 1;
while (j < result.length) {
if (result[j] == null) {
j++;
} else {
break;
}
}
if (j < result.length) {// result[j] != null
biggestTree = result[j];
result[i].next = result[j];
i = j;
} else {
result[i].next = smallestTree;
break;
}
} else {
i += 1;
}
}
this.last = biggestTree;
this.min = this.updateMin().node;
this.size = this.calculateSize();
}
/**
*
* Return the number of elements in the heap
*
*/
public int size() {
return this.size;
}
/**
*
* Return the number of elements in the heap
*
*/
public int calculateSize() {
if (this.empty()) {
return 0;
}
HeapNode node = this.last;
HeapNode curr = node.next;
int size_tree = (int) Math.pow(2, node.rank);
while (curr != node) {
size_tree += Math.pow(2, curr.rank);
curr = curr.next;
}
return size_tree;
}
/**
*
* The method returns true if and only if the heap is empty.
*
*/
public boolean empty() {
return last == null;
}
/**
*
* Return the number of trees in the heap.
*
*/
public int numTrees() {
if (this.empty()) {
return 0;
}
int cnt = 1;
HeapNode node = this.last;
HeapNode next = node.next;
while (next != node) {
cnt++;
next = next.next;
}
return cnt;
}
/**
* Class implementing a node in a Binomial Heap.
*
*/
public class HeapNode {
public HeapItem item;
public HeapNode child;
public HeapNode next;
public HeapNode parent;
public int rank;
public HeapNode(HeapItem item, HeapNode child, HeapNode next, HeapNode parent, int rank) {
this.child = child;
this.item = item;
this.next = next;
this.parent = parent;
this.rank = rank;
}
/**
* @param child != Null
* @pre: pre(child).rank <= post(child).rank
*
* @post: post(rank) = pre(rank) + 1
*
**/
public void setChild(HeapNode child) {
HeapNode temp = this.child;
this.child = child;
child.parent = this;
if (temp != null) {
child.next = temp.next;
temp.next = child;
}
this.rank++;
}
public int getKey() {
return this.item.key;
}
}
/**
* Class implementing an item in a Binomial Heap.
*
*/
public static class HeapItem {
public HeapNode node;
public int key;
public String info;
//Constructor
public HeapItem(HeapNode node, int key, String info) {
this.node = node;
this.key = key;
this.info = info;
}
public HeapItem(int key, String info, HeapNode node) {this(node,key,info);}
/**
*
* swaps the node's items
*
* @pre OtherItem != null
* @pre this.node != null && otherItem.node != null
*
* @post items swapped
*/
public void swapNodes(BinomialHeap.HeapItem Otheritem) {
HeapNode temp = this.node;
this.node = Otheritem.node;
Otheritem.node = temp;
this.node.item = this;
Otheritem.node.item = Otheritem;
}
}
}