|
@@ -20,27 +20,37 @@ import java.util.ArrayList;
|
20
|
20
|
*/
|
21
|
21
|
public class BinaryHeap<E extends Comparable<E>> {
|
22
|
22
|
|
23
|
|
- private int currentSize; // Number of elements in heap
|
|
23
|
+ // Number of elements in heap.
|
|
24
|
+ private int currentSize;
|
24
|
25
|
|
25
|
|
- // Java genericity does not work with arrays.
|
26
|
|
- // We have to use an ArrayList
|
27
|
|
- private ArrayList<E> array; // The heap array
|
|
26
|
+ // The heap array. Java genericity does not work with arrays so we have to use
|
|
27
|
+ // an ArrayList.
|
|
28
|
+ private ArrayList<E> array;
|
28
|
29
|
|
29
|
30
|
/**
|
30
|
|
- * Construct the binary heap.
|
|
31
|
+ * Construct a new empty binary heap.
|
31
|
32
|
*/
|
32
|
33
|
public BinaryHeap() {
|
33
|
34
|
this.currentSize = 0;
|
34
|
35
|
this.array = new ArrayList<E>();
|
35
|
36
|
}
|
36
|
37
|
|
37
|
|
- // Constructor used for debug.
|
|
38
|
+ /**
|
|
39
|
+ * Construct a copy of the given heap.
|
|
40
|
+ *
|
|
41
|
+ * @param heap Binary heap to copy.
|
|
42
|
+ */
|
38
|
43
|
public BinaryHeap(BinaryHeap<E> heap) {
|
39
|
44
|
this.currentSize = heap.currentSize;
|
40
|
45
|
this.array = new ArrayList<E>(heap.array);
|
41
|
46
|
}
|
42
|
47
|
|
43
|
|
- // Sets an element in the array
|
|
48
|
+ /**
|
|
49
|
+ * Set an element at the given index.
|
|
50
|
+ *
|
|
51
|
+ * @param index Index at which the element should be set.
|
|
52
|
+ * @param value Element to set.
|
|
53
|
+ */
|
44
|
54
|
private void arraySet(int index, E value) {
|
45
|
55
|
if (index == this.array.size()) {
|
46
|
56
|
this.array.add(value);
|
|
@@ -51,41 +61,37 @@ public class BinaryHeap<E extends Comparable<E>> {
|
51
|
61
|
}
|
52
|
62
|
|
53
|
63
|
/**
|
54
|
|
- * Test if the heap is logically empty.
|
55
|
|
- *
|
56
|
|
- * @return true if empty, false otherwise.
|
|
64
|
+ * @return true if the heap is empty, false otherwise.
|
57
|
65
|
*/
|
58
|
66
|
public boolean isEmpty() {
|
59
|
67
|
return this.currentSize == 0;
|
60
|
68
|
}
|
61
|
69
|
|
62
|
70
|
/**
|
63
|
|
- * Returns size.
|
64
|
|
- *
|
65
|
|
- * @return current size.
|
|
71
|
+ * @return Current size (number of elements) of this heap.
|
66
|
72
|
*/
|
67
|
73
|
public int size() {
|
68
|
74
|
return this.currentSize;
|
69
|
75
|
}
|
70
|
76
|
|
71
|
77
|
/**
|
72
|
|
- * Returns index of parent.
|
|
78
|
+ * @return Index of the parent of the given index.
|
73
|
79
|
*/
|
74
|
80
|
private int index_parent(int index) {
|
75
|
81
|
return (index - 1) / 2;
|
76
|
82
|
}
|
77
|
83
|
|
78
|
84
|
/**
|
79
|
|
- * Returns index of left child.
|
|
85
|
+ * @return Index of the left child of the given index.
|
80
|
86
|
*/
|
81
|
87
|
private int index_left(int index) {
|
82
|
88
|
return index * 2 + 1;
|
83
|
89
|
}
|
84
|
90
|
|
85
|
91
|
/**
|
86
|
|
- * Insert into the heap.
|
|
92
|
+ * Insert the given element into the heap.
|
87
|
93
|
*
|
88
|
|
- * @param x the item to insert.
|
|
94
|
+ * @param x Item to insert.
|
89
|
95
|
*/
|
90
|
96
|
public void insert(E x) {
|
91
|
97
|
int index = this.currentSize++;
|
|
@@ -96,7 +102,7 @@ public class BinaryHeap<E extends Comparable<E>> {
|
96
|
102
|
/**
|
97
|
103
|
* Internal method to percolate up in the heap.
|
98
|
104
|
*
|
99
|
|
- * @param index the index at which the percolate begins.
|
|
105
|
+ * @param index Index at which the percolate begins.
|
100
|
106
|
*/
|
101
|
107
|
private void percolateUp(int index) {
|
102
|
108
|
E x = this.array.get(index);
|
|
@@ -114,7 +120,7 @@ public class BinaryHeap<E extends Comparable<E>> {
|
114
|
120
|
/**
|
115
|
121
|
* Internal method to percolate down in the heap.
|
116
|
122
|
*
|
117
|
|
- * @param index the index at which the percolate begins.
|
|
123
|
+ * @param index Index at which the percolate begins.
|
118
|
124
|
*/
|
119
|
125
|
private void percolateDown(int index) {
|
120
|
126
|
int ileft = index_left(index);
|
|
@@ -148,7 +154,7 @@ public class BinaryHeap<E extends Comparable<E>> {
|
148
|
154
|
/**
|
149
|
155
|
* Find the smallest item in the heap.
|
150
|
156
|
*
|
151
|
|
- * @return the smallest item in the heap.
|
|
157
|
+ * @return The smallest item in the heap.
|
152
|
158
|
*
|
153
|
159
|
* @throws RuntimeException if this heap is empty.
|
154
|
160
|
*/
|
|
@@ -161,7 +167,7 @@ public class BinaryHeap<E extends Comparable<E>> {
|
161
|
167
|
/**
|
162
|
168
|
* Remove the smallest item from the heap.
|
163
|
169
|
*
|
164
|
|
- * @return the smallest item in the heap.
|
|
170
|
+ * @return The smallest item in the heap.
|
165
|
171
|
*
|
166
|
172
|
* @throws RuntimeException if this heap is empty.
|
167
|
173
|
*/
|