-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeMinHeap.java
531 lines (400 loc) · 13.4 KB
/
TreeMinHeap.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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package cpsc331.assignment3;
import cpsc331.collections.MinHeap;
import java.util.NoSuchElementException;
/**
*
* Provides a Tree-Based Implementation of an Unbounded MinHeap<br><br>
*
* TreeMinHeap Invariant: A finite multiset of non-values of ordered type T is
* stored in a binary MinHeap, using a tree-based representation.
* <br><br>
*
*/
public class TreeMinHeap<T extends Comparable<T>> implements MinHeap<T> {
//
// Implements a Node in a Binary MinHeap
//
// TreeNode Invariant:
// a) This node stores a non-null value from an ordered type T
// b) parent, leftChild and rightChild are (possibly null) references to other
// TreeNodes; indx is the position where the data at this TreeNode would be
// stored in an ArrayList-based implementation of the same MinHeap
class TreeNode {
// Data Fields
private T value; // Value stored at this TreeNode
private int index; // Position of this node in an array-based representation
private TreeNode parent; // The parent of this TreeNode
private TreeNode leftChild; // The left child of this TreeNode
private TreeNode rightChild; // The right child of this TreeNode
// Creates a new TreeNode with a given value, "inputValue" and a given
// index, "inputIndex"
//
// Precondition:
// a) A non-null value inputValue with type T and non-negative integer inputIndex
// are given as index.
// Postcondition:
// a) A TreeNode storing value "inputValue" and with index "inputIndex",
// for which parent, leftChild and rightChild are null, has been created.
public TreeNode (T inputValue, int inputIndex) {
value = inputValue;
index = inputIndex;
parent = null;
leftChild = null;
rightChild = null;
}
// Reports the index of this node.
//
// Precondition: The TreeNode Invariant is satisfied.
// Postcondition: The index of this TreeNode is returned as output.
public int getIndex() {
return index;
}
// Reports the value stored at this TreeNode
//
// Precondition: The TreeNode Invariant is satisfied.
// Postcondition: The value stored at this TreeNode is returned as output.
public T getValue() {
return value;
}
// Reports the parent of this TreeNode
//
// Precondition: The TreeNode Invariant is satisfied.
// Postcondition: The parent of this TreeNode is returned as output.
public TreeNode getParent () {
return parent;
}
// Reports the left child of this TreeNode
//
// Precondition: The TreeNode Invariant is satisfied.
// Postcondition: The left child of this TreeNode is returned as output.
public TreeNode getLeft () {
return leftChild;
}
// Reports the right child of this TreeNode
//
// Precondition: The TreeNode Invariant is satisfied.
//
public TreeNode getRight() {
return rightChild;
}
// Sets the value stored at this TreeNode
//
// Precondition:
// a) The TreeNode Invariant is satisfied.
// b) A non-null value inputValue, with type T, is given as input.
// Postcondition:
// a) The value stored at this TreeNode is now the given inputValue.
public void setValue(T inputValue) {
value = inputValue;
}
// Sets the parent of this TreeNode
//
// Precondition:
// a) The TreeNode Invariant is satisfied.
// b) A TreeNeed, inputParent, is given as input.
// Postcondition:
// a) The parent of this TreeNode is now the given inputParent.
public void setParent(TreeNode inputParent) {
parent = inputParent;
}
// Sets the left child of this TreeNode
//
// Precondition:
// a) The TreeNode Invariant is satisfied.
// b) A TreeNode, inputLeft, is given as input.
// Postcondition:
// a) The left child of this TreeNode is now the given inputLeft
public void setLeft(TreeNode inputLeft) {
leftChild = inputLeft;
}
// Sets the right child of this TreeNode
//
// Precondition:
// a) The TreeNode Invariant is satisfied.
// b) A TreeNode, inputRight, is given as input.
// Postcondition:
// a) The right child of this TreeNode is now the given inputRight.
public void setRight(TreeNode inputRight) {
rightChild = inputRight;
}
}
// TrreMinHeap Invariant:
// a) root is the root of a binary tree representing a MinHeap, whose nodes store
// non=null values from the ordered type T. Thus this binary tree has the shape of
// a binary heap. The value at each node is greater than or equal to the value at
// its parent, if the parent exists, and is less than the values stored at its
// left and right children, if these exist.
// b) heapSize is the current size of this binary heap.
// c) latest is the existing node that was most recently added to this MinHeap.
// Data Fields
private int heapSize; // The size of this MinHeap
private TreeNode root; // The root of the binary tree representing this MinHeap
private TreeNode latest; // The last remaining TreeNode added to this MaxHeap
/**
*
* Creates an empty MinHeap.<br><br>
*
* Precondition: None<br>
* Postcondition: The TreeMinHeap Invariant is satisfied; the MinHeap represneted
* is an empty heap, so that its heapSize is zero.
*
*/
public TreeMinHeap () {
heapSize = 0;
root = null;
latest = null;
}
// Returns the existing node most recently added before "latest" or null, if
// no such node exists.
//
// Precondition: The TreeMinHeap Invariant is satisfied, and the MinHeap represented
// is not empty.
// Postcondition: The TreeNode added most recently before latest is returned
// as output.
private TreeNode predecessor () {
// Start with the latest node
TreeNode n = this.latest;
// If n is a right child, return its sibling
if (n == n.parent.rightChild) {
// Return n's sibling
return n.parent.leftChild;
// If n is a left child...
} else {
// Go up until you reach the root or a right child
while (n != this.root && n == n.parent.leftChild) {
n = n.parent;
}
// Swap to the node's sibling... if it has one
if (n != this.root) {
n = n.parent.leftChild;
}
// Go right until a leaf is reached
while (n.rightChild != null) {
n = n.rightChild;
}
return n;
}
}
// Returns the node that should become the parent of the next node to be added.
//
// Precondition: The TreeMinHeap Invariant is satisfied, and the MinHeap represented
// is not empty.
// Postcondition: The TreeNode that should be the parent of the next node to be
// added is returned.
private TreeNode successorParent () {
// Start wih the latest node
TreeNode n = this.latest;
// If n isn't the root and n is a left child...
if (n != this.root && n == n.parent.leftChild) {
// Return n's parent
return n.parent;
// If n is root or a right child...
} else {
// Go up until you reach the root or a left child
while (n != this.root && n == n.parent.rightChild) {
n = n.parent;
}
// Swap to the node's sibling... if it has one
if (n != this.root) {
n = n.parent.rightChild;
}
// Go left until a leaf is reached
while (n.leftChild != null) {
n = n.leftChild;
}
return n;
}
}
//
// Implements the "bubbleUp" method needed to complete an insertion
//
// Precondition:
// a) root if the root of a binary tree, with the shape of a binary heap,
// storing values from an ordered type T.
// b) A non-null node x in this binary tree has been given as input.
// c) For every node y in this binary tree, except for x, if y has a parent
// then the value stored at the parent is less than or equal to the value
// stored at y. If y has a grandparent then the value at the grandparent
// is less than or equal to the value stored at y, as well.
// Postcondition:
// a) The values stored at the nodes of this binary tree have been exchanged
// between nodes but otherwise unchanged - the same multiset is represented
// by the nodes in this binary tree.
// b) This binary tree is now a representation of a binary MinHeap.
//
private void bubbleUp(TreeNode x) {
T swapValue;
int swapIndex;
// While there are still nodes in the heap...
while (x != root) {
// Swap values if x isn't root and x is less than its parent
if (x != this.root && x.value.compareTo(x.parent.value) == -1) {
// Swap values
swapValue = x.value;
swapIndex = x.index;
x.value = x.parent.value;
x.index = x.parent.index;
x.parent.value = swapValue;
x.parent.index = swapIndex;
// Continue bubbling with parent
x = x.parent;
continue;
}
// No more bubbling needed
break;
}
}
//
// Implements the "bubbleDown" method used to complete a deletion
//
// Precondition:
// a) root is the root of a binary tree, with the shape of a binary heap,
// storing non-null values from an ordered type T.
// b) A non-null node x in this binary tree, has been given as input.
// c) For every node y in this binary tree except for x, if y has a child
// then the value stored at the child is greater than or equal to the value
// stored at y, and if y has a grandchild then the value stored at the
// grandchild is greater than or equal to the value stored at y, as well.
// Postcondition:
// a) The values stored at the nodes of this binary tree have been exchanged
// between nodes but otherwise unchanged - the same multiset is represented
// by the nodes in this binary tree.
// b) This binary tree is now a representation of a binary MinHeap.
private void bubbleDown(TreeNode x) {
T swapValue;
int swapIndex;
// While x has children...
while (x.leftChild != null) {
// If x has 2 children...
if (x.rightChild != null) {
// If the left child is less than or equal to right child...
if (x.leftChild.value.compareTo(x.rightChild.value) <= 0) {
// If the left child is less than x...
if (x.leftChild.value.compareTo(x.value) == -1) {
// Swap values
swapValue = x.value;
swapIndex = x.index;
x.value = x.leftChild.value;
x.index = x.leftChild.index;
x.leftChild.value = swapValue;
x.leftChild.index = swapIndex;
// Continue bubbling with the left child
x = x.leftChild;
continue;
}
// If the right child is less than left child
} else {
if (x.rightChild.value.compareTo(x.value) == -1) {
// Swap values
swapValue = x.value;
swapIndex = x.index;
x.value = x.rightChild.value;
x.index = x.rightChild.index;
x.rightChild.value = swapValue;
x.rightChild.index = swapIndex;
// Continue bubbling with the right child
x = x.rightChild;
continue;
}
}
// If i has 1 child...
} else if (x.leftChild != null) {
// If the left child is less than i...
if (x.leftChild.value.compareTo(x.value) == -1) {
// Swap values
swapValue = x.value;
swapIndex = x.index;
x.value = x.leftChild.value;
x.index = x.leftChild.index;
x.leftChild.value = swapValue;
x.leftChild.index = swapIndex;
// Continue bubbling with the left child
x = x.leftChild;
continue;
}
}
// No more bubbling needed
break;
}
}
// Implementation of the insert method provided by a MinHeap. The preconditions
// and postcondition for this problem are the same, except that they now also
// include the fact that the "TreeMinHeap Invariant" is satisfied when execution
// of the algorithm begins and, again, when it ends.
public void insert (T v) {
// Create a new node
TreeNode n = new TreeNode(v, this.latest != null ? this.latest.index + 1 : 0);
// Assign the parent
if (this.root == null) {
this.root = n;
} else {
n.parent = successorParent();
}
// Set thisto the latest
this.latest = n;
// Set the parent's relation
if (n != root) {
if (this.latest.parent.leftChild == null) {
this.latest.parent.leftChild = this.latest;
} else {
this.latest.parent.rightChild = this.latest;
}
}
// Bubble up the node
this.heapSize += 1;
bubbleUp(this.latest);
}
// Implementation of the deleteMin method provided by a MinHeap. The precondition
// and postcondition for this problem are the same, except that they now also
// include the fact that the "TreeMinHeap Invariant" is satisfied when execution
// of the algorithm begins, and when it ends.
public T deleteMin () throws NoSuchElementException {
// If the heap is empty...
if (this.heapSize == 0) {
throw new NoSuchElementException("This heap is empty!");
// If the heap isn't empty...
} else {
// Get the latest value
T latestVal = this.latest.value;
this.heapSize -= 1;
// If that was the only node...
if (this.heapSize == 0) {
this.root = null;
this.latest = null;
return latestVal;
// If there were multiple nodes...
} else {
// Record the minimum value
T minVal = this.root.value;
// Swap the root value with the latest value
this.root.value = latestVal;
// Assign a new latest node
TreeNode l = this.latest;
this.latest = predecessor();
// Delete the previous latest node
if (l.parent.leftChild == l) {
l.parent.leftChild = null;
} else {
l.parent.rightChild = null;
}
l.parent = null;
// Bubble down the root
bubbleDown(this.root);
// Return the old root value
return minVal;
}
}
}
public int getSize() {
return heapSize;
}
// Used for Testing: Returns the root of the bree used to represent this
// binary MinHeap
TreeNode getRoot() {
return root;
}
// Used for Testing: Returns the value of latest
TreeNode getLatest() {
return latest;
}
}