-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
Treap.java
357 lines (311 loc) · 8.9 KB
/
Treap.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
package com.thealgorithms.datastructures.trees;
import java.util.Random;
/**
* Treap -> Tree + Heap
* Also called as cartesian tree
*
* @see
* <a href = "https://cp-algorithms.com/data_structures/treap.html" />
*/
public class Treap {
public static class TreapNode {
/**
* TreapNode class defines the individual nodes in the Treap
*
* value -> holds the value of the node.
* Binary Search Tree is built based on value.
*
* priority -> holds the priority of the node.
* Heaps are maintained based on priority.
* It is randomly assigned
*
* size -> holds the size of the subtree with current node as root
*
* left -> holds the left subtree
* right -> holds the right subtree
*/
public int value;
private int priority;
private int size;
public TreapNode left;
public TreapNode right;
public TreapNode(int valueParam, int priorityParam) {
value = valueParam;
priority = priorityParam;
size = 1;
left = null;
right = null;
}
/**
* updateSize -> updates the subtree size of the current node
*/
private void updateSize() {
size = 1;
if (left != null) {
size += left.size;
}
if (right != null) {
size += right.size;
}
}
}
/**
* root -> holds the root node in the Treap
* random -> to generate random priority for the nodes in the Treap
*/
private TreapNode root;
private Random random = new Random();
/**
* Constructors
*
* Treap() -> create an empty Treap
* Treap(int[] nodeValues) -> add the elements given in the array to the Treap
*/
public Treap() {
root = null;
}
/**
* merges two Treaps left and right into a single Treap
*
* @param left left Treap
* @param right right Treap
* @return root of merged Treap
*/
private TreapNode merge(TreapNode left, TreapNode right) {
if (left == null) {
return right;
}
if (right == null) {
return left;
}
if (left.priority > right.priority) {
left.right = merge(left.right, right);
left.updateSize();
return left;
} else {
right.left = merge(left, right.left);
right.updateSize();
return right;
}
}
/**
* split the Treap into two Treaps where left Treap has nodes <= key and right Treap has nodes > key
*
* @param node root node to be split
* @param key key to compare the nodes
* @return TreapNode array of size 2.
* TreapNode[0] contains the root of left Treap after split
* TreapNode[1] contains the root of right Treap after split
*/
private TreapNode[] split(TreapNode node, int key) {
if (node == null) {
return new TreapNode[] {null, null};
}
TreapNode[] result;
if (node.value <= key) {
result = split(node.right, key);
node.right = result[0];
node.updateSize();
result[0] = node;
} else {
result = split(node.left, key);
node.left = result[1];
node.updateSize();
result[1] = node;
}
return result;
}
/**
* insert a node into the Treap
*
* @param value value to be inserted into the Treap
* @return root of the Treap where the value is inserted
*/
public TreapNode insert(int value) {
if (root == null) {
root = new TreapNode(value, random.nextInt());
return root;
}
TreapNode[] splitted = split(root, value);
TreapNode node = new TreapNode(value, random.nextInt());
TreapNode tempMerged = merge(splitted[0], node);
tempMerged.updateSize();
TreapNode merged = merge(tempMerged, splitted[1]);
merged.updateSize();
root = merged;
return root;
}
/**
* delete a value from root if present
*
* @param value value to be deleted from the Treap
* @return root of the Treap where delete has been performed
*/
public TreapNode delete(int value) {
root = deleteNode(root, value);
return root;
}
private TreapNode deleteNode(TreapNode root, int value) {
if (root == null) {
return null;
}
if (value < root.value) {
root.left = deleteNode(root.left, value);
} else if (value > root.value) {
root.right = deleteNode(root.right, value);
} else {
root = merge(root.left, root.right);
}
if (root != null) {
root.updateSize();
}
return root;
}
/**
* print inorder traversal of the Treap
*/
public void inOrder() {
System.out.print("{");
printInorder(root);
System.out.print("}");
}
private void printInorder(TreapNode root) {
if (root == null) {
return;
}
printInorder(root.left);
System.out.print(root.value + ",");
printInorder(root.right);
}
/**
* print preOrder traversal of the Treap
*/
public void preOrder() {
System.out.print("{");
printPreOrder(root);
System.out.print("}");
}
private void printPreOrder(TreapNode root) {
if (root == null) {
return;
}
System.out.print(root.value + ",");
printPreOrder(root.left);
printPreOrder(root.right);
}
/**
* print postOrder traversal of the Treap
*/
public void postOrder() {
System.out.print("{");
printPostOrder(root);
System.out.print("}");
}
private void printPostOrder(TreapNode root) {
if (root == null) {
return;
}
printPostOrder(root.left);
printPostOrder(root.right);
System.out.print(root.value + ",");
}
/**
* Search a value in the Treap
*
* @param value value to be searched for
* @return node containing the value
* null if not found
*/
public TreapNode search(int value) {
return searchVal(root, value);
}
private TreapNode searchVal(TreapNode root, int value) {
if (root == null) {
return null;
}
if (root.value == value) {
return root;
} else if (root.value < value) {
return searchVal(root.right, value);
} else {
return searchVal(root.left, value);
}
}
/**
* find the lowerBound of a value in the Treap
*
* @param value value for which lowerBound is to be found
* @return node which is the lowerBound of the value passed
*/
public TreapNode lowerBound(int value) {
TreapNode lowerBoundNode = null;
TreapNode current = root;
while (current != null) {
if (current.value >= value) {
lowerBoundNode = current;
current = current.left;
} else {
current = current.right;
}
}
return lowerBoundNode;
}
/**
* find the upperBound of a value in the Treap
*
* @param value value for which upperBound is to be found
* @return node which is the upperBound of the value passed
*/
public TreapNode upperBound(int value) {
TreapNode upperBoundNode = null;
TreapNode current = root;
while (current != null) {
if (current.value > value) {
upperBoundNode = current;
current = current.left;
} else {
current = current.right;
}
}
return upperBoundNode;
}
/**
* returns size of the Treap
*/
public int size() {
if (root == null) {
return 0;
}
return root.size;
}
/**
* returns if Treap is empty
*/
public boolean isEmpty() {
return root == null;
}
/**
* returns root node of the Treap
*/
public TreapNode getRoot() {
return root;
}
/**
* returns left node of the TreapNode
*/
public TreapNode getLeft(TreapNode node) {
return node.left;
}
/**
* returns the right node of the TreapNode
*/
public TreapNode getRight(TreapNode node) {
return node.right;
}
/**
* prints the value, priority, size of the subtree of the TreapNode, left TreapNode and right TreapNode of the node
*/
public String toString(TreapNode node) {
return "{value : " + node.value + ", priority : " + node.priority + ", subTreeSize = " + node.size + ", left = " + node.left + ", right = " + node.right + "}";
}
}