forked from bxt/Ludus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap.java
executable file
·207 lines (184 loc) · 4.61 KB
/
Heap.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
package bxt.unilectures.informationsuebertragung.fun.entropy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EmptyStackException;
import java.util.List;
/**
* A generic max/min-heap priority Queue.
* @author Bernhard Häussner
*/
public class Heap<T extends Comparable<T>> {
/**
* Index of top heap element
*/
private static int TOP = 0;
/**
* To select min or max heap
*/
public static enum Ordering {
/**
* Maxheap, i.e. big elements on top
*/
MAX(1),
/**
* Minheap, i.e. small elements on top
*/
MIN(-1);
/**
* Will be matched against the return value of
* {@link Comparable#compareTo(Object)}
*/
private final int compareSign;
private Ordering(int cS) {
compareSign=cS;
}
}
/**
* Use maxheap if no ordering is selected
*/
private static Ordering defaultOrdering = Ordering.MAX;
/**
* The actual heap data
*/
private final List<T> a=new ArrayList<T>();
/**
* Is this heap a min or max heap
*/
private final Ordering ordering;
/**
* @see Heap#Heap(Comparable[], Ordering)
* @param as
*/
public Heap(final T[] as) {
this(as,defaultOrdering);
}
/**
* @see Heap#Heap(Collection, Ordering)
* @param as
*/
public Heap(final Collection<T> as) {
this(as,defaultOrdering);
}
/**
* Construct using an array
* @param a Array to build heap from
* @return A fresh Instance working an a copied max-heaped array
*/
public Heap(final T[] as, Ordering ordering) {
this.ordering=ordering;
Collections.addAll(a, as);
for(int i=a.size()-1;i>=0;i--) {
reHeapify(i);
}
}
/**
* Construct using another Collection
* @param a Array to build heap from
* @return A fresh Instance working an a copied max-heaped array
*/
public Heap(final Collection<T> as, Ordering ordering) {
this.ordering=ordering;
a.addAll(as);
for(int i=a.size()-1;i>=0;i--) {
reHeapify(i);
}
}
/**
* Get and remove topmost element, preserve heap property.
* @return The former greatest element
*/
public T pop() {
if(a.size()<1) throw new EmptyStackException();
T maxvalue=a.get(TOP);
a.set(TOP, a.get(a.size()-1));
a.remove(a.size()-1);
if(a.size()>0) reHeapify(TOP);
return maxvalue;
}
/**
* Add another element, preserve heap property.
* @param newElement Element that will be on the heap afterwards
*/
public void push(T newElement) {
int i=a.size();
a.add(newElement);
while(i>0 && a.get(i).compareTo(a.get((i-1)/2))==ordering.compareSign) {
T tmp=a.get(i);
a.set(i, a.get((i-1)/2));
a.set((i-1)/2, tmp);
i=(i-1)/2;
}
}
/**
* Get the number of elements in currently the heap
* @return element count
*/
public int size() {
return a.size();
}
/**
* Fix a part of the heap bottom up from a too small element downwards.
* @param i Offending parent element index
*/
private void reHeapify(final int i) {
final int l=i*2+1, r=i*2+2, s=a.size();
int largest;
if(l<s && (a.get(l).compareTo(a.get(i))*ordering.compareSign)>0 )
largest=l;
else largest=i;
if(r<s && (a.get(r).compareTo(a.get(largest))*ordering.compareSign)>0 )
largest=r;
if(largest!=i) {
T tmp=a.get(i);
a.set(i, a.get(largest));
a.set(largest, tmp);
reHeapify(largest);
}
}
/**
* Make a neat ASCII tree from the heap.
*/
public String toString() {
final int s=a.size();
final StringBuffer stringBuffer = new StringBuffer();
int breakIndex=0,breakStep=1,heapWidth=1;
while (heapWidth*2<=s) heapWidth=heapWidth*2;
int elemWidth=heapWidth;
for(int i=0;i<s;i++) {
stringBuffer.append(strRepeat(" ",elemWidth*2));
stringBuffer.append(" "+a.get(i).toString());
stringBuffer.append(strRepeat(" ",(elemWidth-1)*2));
if(i==breakIndex) {
stringBuffer.append("\n");
//stringBuffer.append("\n");
breakStep=2*breakStep;
breakIndex+=breakStep;
elemWidth=elemWidth/2;
}
}
stringBuffer.append("\n");
return stringBuffer.toString();
}
/**
* Concatenate n string copys.
* @param string String to multiply
* @param times Number of times duplicated
* @return The repeated strings
*/
private static String strRepeat(final String string,final int times) {
final StringBuffer stringBuffer = new StringBuffer();
for (int i=0;i<times;i++)
stringBuffer.append(string);
return stringBuffer.toString();
}
/**
* A method to test the heap. Will arrange the numbers from 1-9 into a
* maxheap and print it.
* @param args
*/
public static void main(String[] args) {
Heap<Integer> heap = new Heap<Integer>(new Integer[]{1,2,3,4,5,6,7,8,9}, Ordering.MAX);
System.out.println(heap);
}
}