|
@@ -1,19 +1,14 @@
|
1
|
|
-//
|
2
|
|
-// ******************PUBLIC OPERATIONS*********************
|
3
|
|
-// void insert( x ) --> Insert x
|
4
|
|
-// Comparable deleteMin( )--> Return and remove smallest item
|
5
|
|
-// Comparable findMin( ) --> Return smallest item
|
6
|
|
-// boolean isEmpty( ) --> Return true if empty; else false
|
7
|
|
-// ******************ERRORS********************************
|
8
|
|
-// Throws RuntimeException for findMin and deleteMin when empty
|
9
|
|
-
|
10
|
1
|
package org.insa.algo.utils;
|
11
|
2
|
|
12
|
3
|
import java.util.ArrayList;
|
13
|
4
|
|
|
5
|
+
|
|
6
|
+
|
14
|
7
|
/**
|
15
|
|
- * Implements a binary heap. Note that all "matching" is based on the compareTo
|
16
|
|
- * method.
|
|
8
|
+ * Implements a binary heap containing elements of type E.
|
|
9
|
+ *
|
|
10
|
+ * Note that all comparisons are based on the compareTo method,
|
|
11
|
+ * hence E must implement Comparable
|
17
|
12
|
*
|
18
|
13
|
* @author Mark Allen Weiss
|
19
|
14
|
* @author DLB
|
|
@@ -57,6 +52,7 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
57
|
52
|
else {
|
58
|
53
|
this.array.set(index, value);
|
59
|
54
|
}
|
|
55
|
+
|
60
|
56
|
}
|
61
|
57
|
|
62
|
58
|
/**
|
|
@@ -163,41 +159,153 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
163
|
159
|
return minItem;
|
164
|
160
|
}
|
165
|
161
|
|
166
|
|
- /**
|
167
|
|
- * Prints the heap
|
|
162
|
+ /* This class is used by toString_tree.
|
|
163
|
+ * It is just a triple of strings. Could it be made simpler in Java ?
|
|
164
|
+ *
|
|
165
|
+ * Printing context, functional style.
|
168
|
166
|
*/
|
169
|
|
- public void print() {
|
170
|
|
- System.out.println();
|
171
|
|
- System.out.println("======== HEAP (size = " + this.currentSize + ") ========");
|
172
|
|
- System.out.println();
|
|
167
|
+ private class Context {
|
|
168
|
+ /* Output text */
|
|
169
|
+ public final String acu ;
|
|
170
|
+
|
|
171
|
+ /* margin: the margin to get back exactly under the current position on the next line. */
|
|
172
|
+ public final String margin ;
|
|
173
|
+
|
|
174
|
+ /* lastmargin: margin used for the last child of a node. The markers are different. */
|
|
175
|
+ public final String lastmargin ;
|
|
176
|
+
|
|
177
|
+ public Context(String a, String b, String c) {
|
|
178
|
+ this.acu = a ;
|
|
179
|
+ this.margin = b ;
|
|
180
|
+ this.lastmargin = c ;
|
|
181
|
+ }
|
|
182
|
+
|
|
183
|
+ /* Appends newlines. */
|
|
184
|
+ public Context nl(int n) {
|
|
185
|
+ if (n <= 0) { return this ; }
|
|
186
|
+ else {
|
|
187
|
+ String acu2 = this.acu + "\n" + this.margin ;
|
|
188
|
+ return (new Context(acu2, this.margin, this.lastmargin).nl(n-1)) ;
|
|
189
|
+ }
|
|
190
|
+ }
|
|
191
|
+
|
|
192
|
+ /* Adds some text */
|
|
193
|
+ public Context add(Integer count, String s) {
|
|
194
|
+ int cnt = (count==null) ? s.length() : count ;
|
|
195
|
+ String spaces = new String(new char[cnt]).replace('\0', ' ');
|
173
|
196
|
|
174
|
|
- for (int i = 0; i < this.currentSize; i++) {
|
175
|
|
- System.out.println(this.array.get(i).toString());
|
|
197
|
+ return new Context(this.acu + s, this.margin + spaces, this.lastmargin + spaces) ;
|
176
|
198
|
}
|
177
|
199
|
|
178
|
|
- System.out.println();
|
179
|
|
- System.out.println("-------- End of heap --------");
|
180
|
|
- System.out.println();
|
|
200
|
+ /* Adds a branch */
|
|
201
|
+ public Context br(Integer count, String label) {
|
|
202
|
+ Context ctxt = this.add(count, label) ;
|
|
203
|
+
|
|
204
|
+ if (count == null) {
|
|
205
|
+ return new Context(ctxt.acu + "_",
|
|
206
|
+ ctxt.margin + "|",
|
|
207
|
+ ctxt.margin + " ") ;
|
|
208
|
+ }
|
|
209
|
+ else {
|
|
210
|
+ return new Context(ctxt.acu,
|
|
211
|
+ ctxt.margin + "|",
|
|
212
|
+ ctxt.margin + " ").nl(1) ;
|
|
213
|
+
|
|
214
|
+ }
|
|
215
|
+ }
|
181
|
216
|
}
|
182
|
217
|
|
183
|
|
- /**
|
184
|
|
- * Prints the elements of the heap according to their respective order.
|
185
|
|
- */
|
186
|
|
- public void printSorted() {
|
|
218
|
+ /* Input : ready to write the current node at the current context position.
|
|
219
|
+ * Output : the last character of acu is the last character of the current node. */
|
|
220
|
+ public Context toString_loop(Context ctxt, int node, int max_depth) {
|
|
221
|
+
|
|
222
|
+ if (max_depth < 0) { return ctxt.add(null,"...") ; }
|
|
223
|
+ else {
|
|
224
|
+ E nodeval = this.array.get(node) ;
|
|
225
|
+ String nodevals = nodeval.toString() ;
|
|
226
|
+
|
|
227
|
+ ArrayList<Integer> childs = new ArrayList<Integer>() ;
|
|
228
|
+ // Add childs
|
|
229
|
+ int index_left = this.index_left(node) ;
|
|
230
|
+ int index_right = index_left + 1 ;
|
187
|
231
|
|
|
232
|
+ if (index_left < this.currentSize) { childs.add(index_left) ; }
|
|
233
|
+ if (index_right < this.currentSize) { childs.add(index_right) ; }
|
|
234
|
+
|
|
235
|
+ Context ctxt2 = childs.isEmpty() ? ctxt.add(null,nodevals) : ctxt.br(1, nodevals) ;
|
|
236
|
+
|
|
237
|
+ for (int ch = 0 ; ch < childs.size() ; ch++) {
|
|
238
|
+ boolean is_last = (ch == childs.size() - 1) ;
|
|
239
|
+ int child = childs.get(ch) ;
|
|
240
|
+
|
|
241
|
+ if (is_last) {
|
|
242
|
+ Context ctxt3 = new Context( ctxt2.acu, ctxt2.lastmargin, ctxt2.lastmargin) ;
|
|
243
|
+ ctxt2 = new Context( this.toString_loop(ctxt3.add(null, "___"),child,max_depth-1).acu,
|
|
244
|
+ ctxt2.margin,
|
|
245
|
+ ctxt2.lastmargin) ;
|
|
246
|
+ }
|
|
247
|
+ else {
|
|
248
|
+ ctxt2 = new Context(this.toString_loop(ctxt2.add(null,"___"),child,max_depth-1).acu,
|
|
249
|
+ ctxt2.margin,
|
|
250
|
+ ctxt2.lastmargin).nl(2) ;
|
|
251
|
+ }
|
|
252
|
+ }
|
|
253
|
+
|
|
254
|
+ return ctxt2 ;
|
|
255
|
+ }
|
|
256
|
+ }
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+ // Textual representation of the tree.
|
|
260
|
+ public String toString_tree(int max_depth) {
|
|
261
|
+ Context init_context = new Context(" ", " ", " ") ;
|
|
262
|
+ Context result = this.toString_loop(init_context, 0, max_depth) ;
|
|
263
|
+ return result.acu ;
|
|
264
|
+ }
|
|
265
|
+
|
|
266
|
+ // Prints the elements, sorted.
|
|
267
|
+ // max_elements: maximal number of elements printed. -1 for infinity.
|
|
268
|
+ public String toString_sorted(int max_elements) {
|
|
269
|
+ String result = "\n" ;
|
188
|
270
|
BinaryHeap<E> copy = new BinaryHeap<E>(this);
|
189
|
271
|
|
190
|
|
- System.out.println();
|
191
|
|
- System.out.println("======== Sorted HEAP (size = " + this.currentSize + ") ========");
|
192
|
|
- System.out.println();
|
|
272
|
+ int remaining = max_elements ;
|
|
273
|
+
|
|
274
|
+ String truncate = "" ;
|
|
275
|
+ if (max_elements < 0 || max_elements >= this.currentSize) {
|
|
276
|
+ truncate = "" ;
|
|
277
|
+ } else {
|
|
278
|
+ truncate = ", only " + max_elements + " elements are shown";
|
|
279
|
+ }
|
|
280
|
+
|
|
281
|
+ result += "======== Sorted HEAP (size = " + this.currentSize + truncate + ") ========\n\n" ;
|
193
|
282
|
|
194
|
|
- while (!copy.isEmpty()) {
|
195
|
|
- System.out.println(copy.deleteMin());
|
|
283
|
+ while (!copy.isEmpty() && remaining != 0) {
|
|
284
|
+ result += copy.deleteMin() + "\n" ;
|
|
285
|
+ remaining-- ;
|
196
|
286
|
}
|
197
|
287
|
|
198
|
|
- System.out.println();
|
199
|
|
- System.out.println("-------- End of heap --------");
|
200
|
|
- System.out.println();
|
|
288
|
+ result += "\n-------- End of heap --------\n\n" ;
|
|
289
|
+
|
|
290
|
+ return result ;
|
|
291
|
+ }
|
|
292
|
+
|
|
293
|
+ public String toString() {
|
|
294
|
+ return this.toString_tree(8) ;
|
201
|
295
|
}
|
202
|
296
|
|
|
297
|
+ /*
|
|
298
|
+ public static void main(String[] args) {
|
|
299
|
+ BinaryHeap<Integer> heap = new BinaryHeap<Integer>() ;
|
|
300
|
+
|
|
301
|
+ for (int i = 0 ; i < 50 ; i++) {
|
|
302
|
+ heap.insert(i) ;
|
|
303
|
+ }
|
|
304
|
+
|
|
305
|
+ System.out.println(heap.toString_tree(4)) ;
|
|
306
|
+ }
|
|
307
|
+ */
|
|
308
|
+
|
203
|
309
|
}
|
|
310
|
+
|
|
311
|
+
|