-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTreapModule.java
378 lines (314 loc) · 10.4 KB
/
TreapModule.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package Heap;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class TreapModule {
public static final Random r = new Random();
public static interface Treap<T> {
Treap left();
Treap right();
T data();
int rank();
boolean isEmpty();
int size();
@Override
String toString();
public T min();
}
public static class NonEmptyTreap<T> implements Treap<T> {
public Treap left;
public Treap right;
public T data;
public int rank;
public NonEmptyTreap(T x, Treap a, Treap b) {
this(r.nextInt(20), x, a, b);
}
public NonEmptyTreap(int r, T x, Treap a, Treap b) {
rank = r;
data = x;
left = a;
right = b;
}
@Override
public Treap left() {
return left;
}
@Override
public Treap 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();
}
@Override
public T min() {
if (left().isEmpty()) {
return data();
}
return (T) left.min();
}
@Override
public String toString() {
return data + "-" + rank;
}
}
/**
*
*/
public static final Treap<? extends Object> EMPTY = new Treap<Object>() {
@Override
public Treap left() {
throw new UnsupportedOperationException();
}
@Override
public Treap 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;
}
@Override
public Object min() {
return null;
}
};
/* retourn EMPTY instnace (singleton pattern) */
public static <T> Treap emptyTreap() {
return (Treap) EMPTY;
}
/* build a Treap v1 */
public static <T> Treap<T> treap(T x, Treap<T> a, Treap<T> b) {
return new NonEmptyTreap<>(x, a, b);
}
/* build a Treap v2 */
public static <T> Treap<T> treap(int rank, T x, Treap<T> a, Treap<T> b) {
return new NonEmptyTreap<>(rank, x, a, b);
}
/* insert an element into a Treap without rotation*/
public static <T extends Comparable<? super T>> Treap<T> insertNoFix(Treap<T> a, T x) {
if (a.isEmpty()) {
return treap(x, emptyTreap(), emptyTreap());
}
if (x.compareTo(a.data()) < 0) {
return treap(a.data(), insertNoFix(a.left(), x), a.right());
}
return treap(a.data(), a.left(), insertNoFix(a.right(), x));
}
public static <T extends Comparable<? super T>> Treap<T> insertNoFix(Treap<T> a, T x, int rank) {
if (a.isEmpty()) {
return treap(rank, x, emptyTreap(), emptyTreap());
}
if (x.compareTo(a.data()) < 0) {
return treap(a.rank(), a.data(), insertNoFix(a.left(), x, rank), a.right());
}
return treap(a.rank(), a.data(), a.left(), insertNoFix(a.right(), x, rank));
}
/* insert (rotate if needed) an element into a Treap */
public static <T extends Comparable<? super T>> Treap<T> insert(Treap<T> a, T x) {
return rotate(insertNoFix(a, x));
}
public static <T extends Comparable<? super T>> Treap<T> insert(Treap<T> a, T x, int rank) {
return rotate(insertNoFix(a, x, rank));
}
/* Rotate to fix the Treap */
public static <T> Treap<T> rotate(Treap<T> root) {
if (root.isEmpty()) {
return root;
}
Treap<T> left = rotate(root.left());
Treap<T> right = rotate(root.right());
root = treap(root.rank(), root.data(), left, right);
if (!root.left().isEmpty()) {
if (root.rank() > root.left().rank()) {
return rotate(rotateRight(root));
}
}
if (!root.right().isEmpty()) {
if (root.rank() > root.right().rank()) {
return rotate(rotateLeft(root));
}
}
return treap(root.rank(), root.data(), root.left(), root.right());
}
private static <T> Treap<T> rotateRight(Treap<T> root) {
return treap(root.left().rank(),
root.left().data(),
merge(root.left().left(), root.left().right()),
treap(root.rank(), root.data(), emptyTreap(), root.right()));
}
private static <T> Treap<T> rotateLeft(Treap<T> root) {
return treap(root.right().rank(),
root.right().data(),
treap(root.rank(), root.data(), root.left(), emptyTreap()),
merge(root.right().right(), root.right().left())
);
}
public static <T extends Comparable<? super T>> boolean contains(Treap<T> root, T x) {
if (root.isEmpty()) {
return false;
}
int c = root.data().compareTo(x);
if (c == 0) {
return true;
} else if (c < 0) {
return contains(root.right(), x);
}
return contains(root.left(), x);
}
/* create a balanced Treap from an array */
public static <T extends Comparable<? super T>> Treap<T> insert(T... array) {
Treap<T> root = emptyTreap();
for (T item : array) {
root = insert(root, item);
}
return root;
}
/* merge two balanced Treaps into one balanced Treap*/
public static <T extends Comparable<? super T>> Treap<T> merge(Treap<T> a, Treap<T> b) {
if (a.isEmpty()) {
return b;
}
if (b.isEmpty()) {
return a;
}
if (a.size() > b.size()) {
return merge(insert(a, b.data(), b.rank()), merge(b.left(), b.right()));
}
return merge(insert(b, a.data(), a.rank()), merge(a.left(), a.right()));
}
/* remove the minimum key of a Treap */
public static <T extends Comparable<? super T>> Treap<T> delete(Treap<T> root, T x) {
if (!contains(root, x)) {
return root;
}
int c = root.data().compareTo(x);
if (c == 0) {
return merge(root.left(), root.right());
}
if (c > 0) {
return treap(root.rank(), root.data(), delete(root.left(), x), root.right());
}
return treap(root.rank(), root.data(), root.left(), delete(root.right(), x));
}
/* split a treap */
public static <T extends Comparable<? super T>> List<Treap<T>> split(Treap<T> a, T x, List<Treap<T>> seed) {
if (seed == null || seed.isEmpty()) {
seed = new ArrayList<>();
seed.add(emptyTreap());
seed.add(emptyTreap());
}
if(a.isEmpty())
return seed;
if (a.min().compareTo(x) > 0) {
seed.set(0, a);
return seed;
}
int c = a.data().compareTo(x);
if (c < 0) {
seed.set(0,
merge(seed.get(0), treap(a.rank(), a.data(), a.left(), emptyTreap())));
return split(a.right(), x, seed);
}
seed.set(1,
merge(seed.get(1), treap(a.rank(), a.data(), emptyTreap(), a.right())));
return split(a.left(), x, seed);
}
public static <T extends Comparable<? super T>> List<Treap<T>> split2(Treap<T> a, T x, T y) {
List<Treap<T>> seed = new ArrayList<>();
seed.add(emptyTreap());
seed.add(emptyTreap());
seed.add(emptyTreap());
List<Treap<T>> seed1 = split(a, x, null);
List<Treap<T>> seed2 = split(seed1.get(1), y, null);
seed.set(0, seed1.get(0));
seed.set(1, seed2.get(0));
seed.set(2, seed2.get(1));
return seed;
}
/* print Treap element */
public static <T> void printNice(Treap<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);
} else {
System.out.println(root);
}
printNice(root.left(), level + 1);
}
public static void main(String[] args) {
Treap<Integer> a = emptyTreap();
Treap<Integer> b = emptyTreap();
Treap<Integer> c = emptyTreap();
a = insertNoFix(a, 5, 16);
a = insertNoFix(a, 1, 12);
a = insertNoFix(a, 2, 10);
a = insertNoFix(a, 8, 8);
a = insertNoFix(a, 6, 3);
System.out.println("------------ insert ------------");
printNice(a, 1);
System.out.println("------------ rotate ------------");
b = rotate(a);
printNice(b, 1);
System.out.println("------------ contains 8 ------------");
if (contains(b, 8)) {
System.out.println("yes");
} else {
System.out.println("no");
}
System.out.println("------------ min ------------");
System.out.println(b.min());
System.out.println("------------ delete 2 ------------");
c = delete(b, 2);
printNice(c, 1);
System.out.println("------------ Split 3 ------------");
List<Treap<Integer>> list1 = split(b, 3, null);
System.out.println("list 1");
printNice(list1.get(0), 1);
System.out.println("list 2");
printNice(list1.get(1), 1);
System.out.println("------------ Split 3 & 8 ------------");
List<Treap<Integer>> list2 = split2(b, 3, 8);
System.out.println("list 1");
printNice(list2.get(0), 1);
System.out.println("list 2");
printNice(list2.get(1), 1);
System.out.println("list 3");
printNice(list2.get(2), 1);
}
}