-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryHeap.java
More file actions
288 lines (251 loc) · 5.09 KB
/
Copy pathBinaryHeap.java
File metadata and controls
288 lines (251 loc) · 5.09 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
import java.lang.reflect.Array;
import java.util.AbstractQueue;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Random;
/**
* This class implements a priority queue as a class binary heap
* stored implicitly in an array
* @author morin
*
* @param <T>
*/
public class BinaryHeap<T> extends AbstractQueue<T> {
Comparator<T> c;
/**
* Our backing array
*/
protected T[] a;
/**
* The number of elements in the priority queue
*/
protected int n;
/**
* Create a new empty binary heap
* @param c
*/
public BinaryHeap() {
this(new DefaultComparator<T>());
}
@SuppressWarnings("unchecked")
public BinaryHeap(Comparator<T> c0) {
c = c0;
a = (T[]) new Object[1];
//f = new Factory<T>(clz);
//a = f.newArray(1);
n = 0;
}
@SuppressWarnings("unchecked")
public void clear() {
a = (T[]) new Object[1];
//a = f.newArray(1);
n = 0;
}
/**
* Create a new binary heap by heapifying a
* @param a
*/
public BinaryHeap(T[] a) {
this(a, new DefaultComparator<T>());
}
/**
* Create a new binary heap by heapifying a
* @param a
*/
public BinaryHeap(T[] a, Comparator<T> c) {
this.c = c;
this.a = a;
n = a.length;
for (int i = n/2-1; i >= 0; i--) {
trickleDown(i);
}
}
@SuppressWarnings("unchecked")
protected void resize() {
T[] b = (T[]) new Object[Math.max(n * 2, 1)];
//T[] b = f.newArray(Math.max(2*n,1));
System.arraycopy(a, 0, b, 0, n);
a = b;
}
/**
* @param i
* @return the index of the left child of the value at index i
*/
protected int left(int i) {
return 2*i + 1;
}
/**
* @param i
* @return the index of the left child of the value at index i
*/
protected int right(int i) {
return 2*i + 2;
}
/**
* @param i
* @return the index of the parent of the value at index i
*/
protected int parent(int i) {
return (i-1)/2;
}
public int size() {
return n;
}
/**
* Swap the two values a[i] and a[j]
* @param i
* @param j
*/
final protected void swap(int i, int j) {
T x = a[i];
a[i] = a[j];
a[j] = x;
}
public boolean offer(T x) {
return add(x);
}
public boolean add(T x) {
if (n + 1 > a.length) resize();
a[n++] = x;
bubbleUp(n-1);
return true;
}
/**
* Run the bubbleUp routine at position i
* @param i
*/
protected void bubbleUp(int i) {
int p = parent(i);
while (i > 0 && c.compare(a[i], a[p]) < 0) {
swap(i,p);
i = p;
p = parent(i);
}
}
public T peek() {
return a[0];
}
public T poll() {
return remove();
}
public T remove() {
T x = a[0];
a[0] = a[--n];
trickleDown(0);
if (3*n < a.length) resize();
return x;
}
/**
* Move element i down in the heap until the heap
* property is restored
* @param i
*/
protected void trickleDown(int i) {
do {
int j = -1;
int r = right(i);
if (r < n && c.compare(a[r], a[i]) < 0) {
int l = left(i);
if (c.compare(a[l], a[r]) < 0) {
j = l;
} else {
j = r;
}
} else {
int l = left(i);
if (l < n && c.compare(a[l], a[i]) < 0) {
j = l;
}
}
if (j >= 0) swap(i, j);
i = j;
} while (i >= 0);
}
public Iterator<T> iterator() {
class PQI implements Iterator<T> {
int i;
public PQI() {
i = 0;
}
public boolean hasNext() {
return i < n;
}
public T next() {
return a[i++];
}
public void remove() {
throw new UnsupportedOperationException();
}
}
return new PQI();
}
/**
* An implementation of the heapsort algorithm
* @param <T>
* @param a
*/
public static <T> void sort(T[] a, Comparator<T> c) {
BinaryHeap<T> h = new BinaryHeap<T>(a, c);
while (h.n > 1) {
h.swap(--h.n, 0);
h.trickleDown(0);
}
Collections.reverse(Arrays.asList(a));
}
public static <T extends Comparable<T>> void sort(T[] a) {
sort(a, new DefaultComparator<T>());
}
/**
* @param args
*/
public static void main(String[] args) {
BinaryHeap<Integer> h = new BinaryHeap<Integer>();
Random r = new Random();
int n = 20;
for (int i = 0; i < n; i++) {
h.add(r.nextInt(2500));
}
System.out.println(h);
while (!h.isEmpty()) {
System.out.print("" + h.remove() + ",");
}
System.out.println("");
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) {
a[i] = r.nextInt(2500);
}
BinaryHeap.sort(a);
for (Integer x : a) {
System.out.print("" + x + ",");
}
System.out.println("");
n = 100000;
long start, stop;
double elapsed;
System.out.print("performing " + n + " adds...");
start = System.nanoTime();
for (int i = 0; i < n; i++) {
h.add(r.nextInt());
}
stop = System.nanoTime();
elapsed = 1e-9*(stop-start);
System.out.println("(" + elapsed + "s ["
+ (int)(((double)n)/elapsed) + "ops/sec])");
n *= 10;
System.out.print("performing " + n + " add/removes...");
start = System.nanoTime();
for (int i = 0; i < n; i++) {
if (r.nextBoolean()) {
h.add(r.nextInt());
} else {
h.remove();
}
}
stop = System.nanoTime();
elapsed = 1e-9*(stop-start);
System.out.println("(" + elapsed + "s ["
+ (int)(((double)n)/elapsed) + "ops/sec])");
}
}