-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHeapModule.java
219 lines (185 loc) · 5.68 KB
/
HeapModule.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
package Heap;
public class HeapModule {
public static interface Heap<T> {
Heap left();
Heap right();
T data();
int rank();
boolean isEmpty();
int size();
@Override
String toString();
}
public static class NonEmptyHeap<T> implements Heap<T> {
public Heap left;
public Heap right;
public T data;
public int rank;
public NonEmptyHeap(T x, Heap a, Heap b) {
this(0, x, a, b);
}
public NonEmptyHeap(int r, T x, Heap a, Heap b) {
rank = r;
data = x;
left = a;
right = b;
}
@Override
public Heap left() {
return left;
}
@Override
public Heap right() {
return right;
}
@Override
public T data() {
return data;
}
@Override
public int rank() {
return rank;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public int size(){
return 1 + left.size() + right.size();
}
}
public static final Heap<? extends Object> EMPTY = new Heap<Object>() {
@Override
public Heap left() {
throw new UnsupportedOperationException();
}
@Override
public Heap right() {
throw new UnsupportedOperationException();
}
@Override
public Object data() {
throw new UnsupportedOperationException();
}
@Override
public int rank() {
return 0;
}
@Override
public String toString() {
return "";
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public int size(){
return 0;
}
};
/* retourn EMPTY instnace (singleton pattern) */
public static <T> Heap<T> emptyHeap() {
return (Heap) EMPTY;
}
/* build a heap v1 */
public static <T extends Comparable<? super T>> Heap<T> heap(T x, Heap<T> a, Heap<T> b) {
if (a.rank() > b.rank()) {
return new NonEmptyHeap<>(b.rank() + 1, x, a, b);
}
return new NonEmptyHeap<>(a.rank() + 1, x, b, a);
}
/* build a heap v2 */
public static <T extends Comparable<? super T>> Heap<T> heap(T x) {
return heap(x, emptyHeap(), emptyHeap());
}
/* insert an element into a heap */
public static <T extends Comparable<? super T>> Heap<T> insert(Heap<T> a, T x) {
if (a.isEmpty()) {
return heap(x);
}
if (a.data().compareTo(x) <= 0) {
return heap(a.data(), a.left(), insert(a.right(), x));
} else {
return heap(x, a.left(), insert(a.right(), a.data()));
}
}
/* create a balanced heap from an array */
public static <T extends Comparable<? super T>> Heap<T> insert(T... array) {
Heap<T> root = emptyHeap();
for (T item : array) {
root = insert(root, item);
}
return root;
}
/* merge two balanced heaps into one balanced heap*/
public static <T extends Comparable<? super T>> Heap<T> merge(Heap<T> a, Heap<T> b) {
if (a.isEmpty()) {
return b;
}
if (b.isEmpty()) {
return a;
}
if (a.data().compareTo(b.data()) <= 0) {
return heap(a.data(), b, merge(a.left(), a.right()));
}
return heap(b.data(), merge(a.data(), b.left(), b.right()), merge(a.left(), a.right()));
}
/* merge a key + two balanced heaps into one balanced heap*/
public static <T extends Comparable<? super T>> Heap<T> merge(T x, Heap<T> a, Heap<T> b) {
if (a.isEmpty()) {
return insert(a, x);
}
if (b.isEmpty()) {
return insert(b, x);
}
if ((x.compareTo(a.data()) <= 0) && (x.compareTo(b.data()) <= 0)) {
return heap(x, a, b);
}
if (a.data().compareTo(b.data()) <= 0) {
return heap(a.data(), merge(x, a.left(), a.right()), b);
}
return heap(b.data(), merge(x, b.left(), b.right()), a);
}
/* return the minimum key of a heap */
public static <T extends Comparable<? super T>> T min(Heap<T> root) {
if (root == null) {
return null;
}
return root.data();
}
/* remove the minimum key of a heap */
public static <T extends Comparable<? super T>> Heap<T> delete(Heap<T> root) {
if (root == null) {
return null;
}
return merge(root.left(), root.right());
}
/* print heap element */
public static <T> void printNice(Heap<T> root, int level) {
if (root.isEmpty()) {
return;
}
printNice(root.right(), level + 1);
if (level != 0) {
for (int i = 0; i < level - 1; i++) {
System.out.print("| ");
}
System.out.println("|---" + root.data());
} else {
System.out.println(root.data());
}
printNice(root.left(), level + 1);
}
public static void main(String[] args) {
Heap<Integer> heap1 = insert(5, 9, 3);
System.out.println("------------ Balanced Heap 1 ------------");
printNice(heap1, 1);
Heap<Integer> heap3 = insert(1, 8, 2);
System.out.println("------------ Balanced Heap 2 ------------");
printNice(heap3, 1);
System.out.println("------------ Merge Heap 1 & Heap 2 ------------");
printNice(merge(heap1, heap3), 1);
}
}