-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTreapBST.java
229 lines (208 loc) · 5.3 KB
/
TreapBST.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
package Data_Structures;
import java.util.*;
// https://en.wikipedia.org/wiki/Treap
public class TreapBST {
static Random random = new Random();
static class Treap {
int key;
long prio;
Treap left;
Treap right;
int size;
Treap(int key) {
this.key = key;
prio = random.nextLong();
size = 1;
}
void update() {
size = 1 + getSize(left) + getSize(right);
}
}
static int getSize(Treap root) {
return root == null ? 0 : root.size;
}
static class TreapPair {
Treap left;
Treap right;
TreapPair(Treap left, Treap right) {
this.left = left;
this.right = right;
}
}
static TreapPair split(Treap root, int minRight) {
if (root == null)
return new TreapPair(null, null);
if (root.key >= minRight) {
TreapPair leftSplit = split(root.left, minRight);
root.left = leftSplit.right;
root.update();
leftSplit.right = root;
return leftSplit;
} else {
TreapPair rightSplit = split(root.right, minRight);
root.right = rightSplit.left;
root.update();
rightSplit.left = root;
return rightSplit;
}
}
static Treap merge(Treap left, Treap right) {
if (left == null)
return right;
if (right == null)
return left;
// if (random.nextInt(left.size + right.size) < left.size) {
if (left.prio > right.prio) {
left.right = merge(left.right, right);
left.update();
return left;
} else {
right.left = merge(left, right.left);
right.update();
return right;
}
}
static Treap insert(Treap root, int key) {
TreapPair t = split(root, key);
return merge(merge(t.left, new Treap(key)), t.right);
}
static Treap insert2(Treap root, int key) { // alternative implementation
return insert_(root, new Treap(key));
}
static Treap insert_(Treap root, Treap node) {
if (root == null) {
return node;
}
if (root.prio < node.prio) {
TreapPair t = split(root, node.key);
node.left = t.left;
node.right = t.right;
node.update();
return node;
}
if (node.key < root.key) {
root.left = insert_(root.left, node);
root.update();
return root;
} else {
root.right = insert_(root.right, node);
root.update();
return root;
}
}
static Treap remove(Treap root, int key) {
TreapPair t = split(root, key);
return merge(t.left, split(t.right, key + 1).right);
}
static Treap remove2(Treap root, int key) { // alternative implementation
if (root == null) {
return null;
}
if (key < root.key) {
root.left = remove(root.left, key);
root.update();
return root;
} else if (key > root.key) {
root.right = remove(root.right, key);
root.update();
return root;
} else {
return merge(root.left, root.right);
}
}
static int kth(Treap root, int k) {
if (k < getSize(root.left))
return kth(root.left, k);
else if (k > getSize(root.left))
return kth(root.right, k - getSize(root.left) - 1);
return root.key;
}
static void print(Treap root) {
if (root == null)
return;
print(root.left);
System.out.println(root.key);
print(root.right);
}
// O(n) treap creation (if not counting keys sorting)
// http://wcipeg.com/wiki/Cartesian_tree
static Treap createTreap(int[] keys) {
Treap[] nodes = Arrays.stream(keys).mapToObj(Treap::new).sorted((a, b) -> Integer.compare(a.key, b.key)).toArray(Treap[]::new);
int n = keys.length;
int[] parent = new int[n];
Arrays.fill(parent, -1);
Treap root = n > 0 ? nodes[0] : null;
for (int i = 1; i < n; i++) {
int last = i - 1;
if (nodes[last].prio >= nodes[i].prio) {
nodes[last].right = nodes[i];
parent[i] = last;
} else {
while (nodes[last].prio < nodes[i].prio && parent[last] != -1) {
last = parent[last];
}
if (nodes[last].prio < nodes[i].prio) {
nodes[i].left = nodes[last];
root = nodes[i];
} else {
nodes[i].left = nodes[last].right;
nodes[last].right = nodes[i];
parent[i] = last;
}
}
}
updateSizes(root);
return root;
}
private static void updateSizes(Treap root) {
if (root == null)
return;
updateSizes(root.left);
updateSizes(root.right);
root.update();
}
// random test
public static void main(String[] args) {
long time = System.currentTimeMillis();
Treap treap = null;
Set<Integer> set = new TreeSet<>();
for (int i = 0; i < 1000_000; i++) {
int key = random.nextInt(100_000);
if (random.nextBoolean()) {
treap = remove(treap, key);
set.remove(key);
} else if (!set.contains(key)) {
treap = insert(treap, key);
set.add(key);
}
if (set.size() != getSize(treap))
throw new RuntimeException();
}
for (int i = 0; i < getSize(treap); i++) {
if (!set.contains(kth(treap, i)))
throw new RuntimeException();
}
System.out.println(System.currentTimeMillis() - time);
for (int step = 0; step < 1000; step++) {
int n = random.nextInt(10) + 1;
int[] keys = random.ints(n, 0, 100).toArray();
Treap t = createTreap(keys);
Arrays.sort(keys);
for (int i = 0; i < n; i++) {
if (kth(t, i) != keys[i])
throw new RuntimeException();
}
checkHeapInvariant(t);
}
}
private static void checkHeapInvariant(Treap root) {
if (root == null)
return;
if (root.left != null && root.left.prio > root.prio)
throw new RuntimeException();
if (root.right != null && root.right.prio > root.prio)
throw new RuntimeException();
checkHeapInvariant(root.left);
checkHeapInvariant(root.right);
}
}