10
10
* <a href = "https://cp-algorithms.com/data_structures/treap.html" />
11
11
*/
12
12
13
- public class Treap {
13
+ public class Treap {
14
14
15
- public class TreapNode {
15
+ public class TreapNode {
16
16
/**
17
17
* TreapNode class defines the individual nodes in the Treap
18
18
*
@@ -42,10 +42,10 @@ public TreapNode(int value, int priority) {
42
42
/**
43
43
* updateSize -> updates the subtree size of the current node
44
44
*/
45
- private void updateSize (){
45
+ private void updateSize () {
46
46
this .size = 1 ;
47
- if (this .left != null ) this .size += this .left .size ;
48
- if (this .right != null ) this .size += this .right .size ;
47
+ if (this .left != null ) this .size += this .left .size ;
48
+ if (this .right != null ) this .size += this .right .size ;
49
49
}
50
50
}
51
51
@@ -62,12 +62,12 @@ private void updateSize(){
62
62
* Treap() -> create an empty Treap
63
63
* Treap(int[] nodeValues) -> add the elements given in the array to the Treap
64
64
*/
65
- public Treap (){
65
+ public Treap () {
66
66
this .root = null ;
67
67
}
68
68
69
- public Treap (int [] nodeValues ){
70
- for (int nodeValue : nodeValues ) this .insert (nodeValue );
69
+ public Treap (int [] nodeValues ) {
70
+ for (int nodeValue : nodeValues ) this .insert (nodeValue );
71
71
}
72
72
73
73
/**
@@ -77,16 +77,15 @@ public Treap(int[] nodeValues){
77
77
* @param right right Treap
78
78
* @return root of merged Treap
79
79
*/
80
- private TreapNode merge (TreapNode left , TreapNode right ){
81
- if (left == null ) return right ;
82
- if (right == null ) return left ;
80
+ private TreapNode merge (TreapNode left , TreapNode right ) {
81
+ if (left == null ) return right ;
82
+ if (right == null ) return left ;
83
83
84
- if (left .priority > right .priority ){
84
+ if (left .priority > right .priority ) {
85
85
left .right = merge (left .right , right );
86
86
left .updateSize ();
87
87
return left ;
88
- }
89
- else {
88
+ } else {
90
89
right .left = merge (left , right .left );
91
90
right .updateSize ();
92
91
return right ;
@@ -102,20 +101,19 @@ private TreapNode merge(TreapNode left, TreapNode right){
102
101
* TreapNode[0] contains the root of left Treap after split
103
102
* TreapNode[1] contains the root of right Treap after split
104
103
*/
105
- private TreapNode [] split (TreapNode node , int key ){
106
- if (node == null ){
104
+ private TreapNode [] split (TreapNode node , int key ) {
105
+ if (node == null ) {
107
106
return new TreapNode [] {null , null };
108
107
}
109
108
110
109
TreapNode [] result ;
111
110
112
- if (node .value <= key ){
111
+ if (node .value <= key ) {
113
112
result = split (node .right , key );
114
113
node .right = result [0 ];
115
114
node .updateSize ();
116
115
result [0 ] = node ;
117
- }
118
- else {
116
+ } else {
119
117
result = split (node .left , key );
120
118
node .left = result [1 ];
121
119
node .updateSize ();
@@ -131,8 +129,8 @@ private TreapNode[] split(TreapNode node, int key){
131
129
* @param value value to be inserted into the Treap
132
130
* @return root of the Treap where the value is inserted
133
131
*/
134
- public TreapNode insert (int value ){
135
- if (root == null ){
132
+ public TreapNode insert (int value ) {
133
+ if (root == null ){
136
134
root = new TreapNode (value , random .nextInt ());
137
135
return root ;
138
136
}
@@ -158,39 +156,37 @@ public TreapNode insert(int value){
158
156
* @param value value to be deleted from the Treap
159
157
* @return root of the Treap where delete has been performed
160
158
*/
161
- public TreapNode delete (int value ){
159
+ public TreapNode delete (int value ) {
162
160
root = deleteNode (root , value );
163
161
return root ;
164
162
}
165
163
166
- private TreapNode deleteNode (TreapNode root , int value ){
167
- if (root == null ) return null ;
164
+ private TreapNode deleteNode (TreapNode root , int value ) {
165
+ if (root == null ) return null ;
168
166
169
- if (value < root .value ){
167
+ if (value < root .value ) {
170
168
root .left = deleteNode (root .left , value );
171
- }
172
- else if (value > root .value ){
169
+ } else if (value > root .value ) {
173
170
root .right = deleteNode (root .right , value );
174
- }
175
- else {
171
+ } else {
176
172
root = merge (root .left , root .right );
177
173
}
178
174
179
- if (root != null ) root .updateSize ();
175
+ if (root != null ) root .updateSize ();
180
176
return root ;
181
177
}
182
178
183
179
/**
184
180
* print inorder traversal of the Treap
185
181
*/
186
- public void inOrder (){
182
+ public void inOrder () {
187
183
System .out .print ("{" );
188
184
printInorder (root );
189
185
System .out .print ("}" );
190
186
}
191
187
192
- private void printInorder (TreapNode root ){
193
- if (root == null ) return ;
188
+ private void printInorder (TreapNode root ) {
189
+ if (root == null ) return ;
194
190
printInorder (root .left );
195
191
System .out .print (root .value + "," );
196
192
printInorder (root .right );
@@ -199,14 +195,14 @@ private void printInorder(TreapNode root){
199
195
/**
200
196
* print preOrder traversal of the Treap
201
197
*/
202
- public void preOrder (){
198
+ public void preOrder () {
203
199
System .out .print ("{" );
204
200
printPreOrder (root );
205
201
System .out .print ("}" );
206
202
}
207
203
208
- private void printPreOrder (TreapNode root ){
209
- if (root == null ) return ;
204
+ private void printPreOrder (TreapNode root ) {
205
+ if (root == null ) return ;
210
206
System .out .print (root .value + "," );
211
207
printPreOrder (root .left );
212
208
printPreOrder (root .right );
@@ -215,14 +211,14 @@ private void printPreOrder(TreapNode root){
215
211
/**
216
212
* print postOrder traversal of the Treap
217
213
*/
218
- public void postOrder (){
214
+ public void postOrder () {
219
215
System .out .print ("{" );
220
216
printPostOrder (root );
221
217
System .out .print ("}" );
222
218
}
223
219
224
- private void printPostOrder (TreapNode root ){
225
- if (root == null ) return ;
220
+ private void printPostOrder (TreapNode root ) {
221
+ if (root == null ) return ;
226
222
printPostOrder (root .left );
227
223
printPostOrder (root .right );
228
224
System .out .print (root .value + "," );
@@ -235,14 +231,14 @@ private void printPostOrder(TreapNode root){
235
231
* @return node containing the value
236
232
* null if not found
237
233
*/
238
- public TreapNode search (int value ){
234
+ public TreapNode search (int value ) {
239
235
return searchVal (root , value );
240
236
}
241
237
242
- private TreapNode searchVal (TreapNode root , int value ){
243
- if (root == null ) return null ;
238
+ private TreapNode searchVal (TreapNode root , int value ) {
239
+ if (root == null ) return null ;
244
240
245
- if (root .value == value ) return root ;
241
+ if (root .value == value ) return root ;
246
242
else if (root .value < value ) return searchVal (root .right , value );
247
243
else return searchVal (root .left , value );
248
244
}
@@ -253,16 +249,16 @@ private TreapNode searchVal(TreapNode root, int value){
253
249
* @param value value for which lowerBound is to be found
254
250
* @return node which is the lowerBound of the value passed
255
251
*/
256
- public TreapNode lowerBound (int value ){
252
+ public TreapNode lowerBound (int value ) {
257
253
TreapNode lowerBoundNode = null ;
258
254
TreapNode current = root ;
259
255
260
- while (current != null ){
261
- if (current .value >= value ){
256
+ while (current != null ) {
257
+ if (current .value >= value ) {
262
258
lowerBoundNode = current ;
263
259
current = current .left ;
264
- }
265
- else current = current .right ;
260
+ } else
261
+ current = current .right ;
266
262
}
267
263
268
264
return lowerBoundNode ;
@@ -274,16 +270,16 @@ public TreapNode lowerBound(int value){
274
270
* @param value value for which upperBound is to be found
275
271
* @return node which is the upperBound of the value passed
276
272
*/
277
- public TreapNode upperBound (int value ){
273
+ public TreapNode upperBound (int value ) {
278
274
TreapNode upperBoundNode = null ;
279
275
TreapNode current = root ;
280
276
281
- while (current != null ){
282
- if (current .value > value ){
277
+ while (current != null ) {
278
+ if (current .value > value ) {
283
279
upperBoundNode = current ;
284
280
current = current .left ;
285
- }
286
- else current = current .right ;
281
+ } else
282
+ current = current .right ;
287
283
}
288
284
289
285
return upperBoundNode ;
@@ -292,43 +288,43 @@ public TreapNode upperBound(int value){
292
288
/**
293
289
* returns size of the Treap
294
290
*/
295
- public int size (){
296
- if (root == null ) return 0 ;
291
+ public int size () {
292
+ if (root == null ) return 0 ;
297
293
return root .size ;
298
294
}
299
295
300
296
/**
301
297
* returns if Treap is empty
302
298
*/
303
- public boolean isEmpty (){
299
+ public boolean isEmpty () {
304
300
return root == null ;
305
301
}
306
302
307
303
/**
308
304
* returns root node of the Treap
309
305
*/
310
- public TreapNode getRoot (){
306
+ public TreapNode getRoot () {
311
307
return root ;
312
308
}
313
309
314
310
/**
315
311
* returns left node of the TreapNode
316
312
*/
317
- public TreapNode getLeft (TreapNode node ){
313
+ public TreapNode getLeft (TreapNode node ) {
318
314
return node .left ;
319
315
}
320
316
321
317
/**
322
318
* returns the right node of the TreapNode
323
319
*/
324
- public TreapNode getRight (TreapNode node ){
320
+ public TreapNode getRight (TreapNode node ) {
325
321
return node .right ;
326
322
}
327
323
328
324
/**
329
325
* prints the value, priority, size of the subtree of the TreapNode, left TreapNode and right TreapNode of the node
330
326
*/
331
- public String toString (TreapNode node ){
327
+ public String toString (TreapNode node ) {
332
328
return "{value : " + node .value + ", priority : " + node .priority + ", subTreeSize = " + node .size + ", left = " + node .left + ", right = " + node .right + "}" ;
333
329
}
334
330
}
0 commit comments