@@ -29,12 +29,14 @@ public static class TreapNode {
29
29
* right -> holds the right subtree
30
30
*/
31
31
public int value ;
32
- private int priority , size ;
33
- public TreapNode left , right ;
34
-
35
- public TreapNode (int _value , int _priority ) {
36
- value = _value ;
37
- priority = _priority ;
32
+ private int priority ;
33
+ private int size ;
34
+ public TreapNode left ;
35
+ public TreapNode right ;
36
+
37
+ public TreapNode (int valueParam , int priorityParam ) {
38
+ value = valueParam ;
39
+ priority = priorityParam ;
38
40
size = 1 ;
39
41
left = right = null ;
40
42
}
@@ -44,8 +46,12 @@ public TreapNode(int _value, int _priority) {
44
46
*/
45
47
private void updateSize () {
46
48
size = 1 ;
47
- if (left != null ) size += left .size ;
48
- if (right != null ) size += right .size ;
49
+ if (left != null ) {
50
+ size += left .size ;
51
+ }
52
+ if (right != null ) {
53
+ size += right .size ;
54
+ }
49
55
}
50
56
}
51
57
@@ -74,8 +80,12 @@ public Treap() {
74
80
* @return root of merged Treap
75
81
*/
76
82
private TreapNode merge (TreapNode left , TreapNode right ) {
77
- if (left == null ) return right ;
78
- if (right == null ) return left ;
83
+ if (left == null ) {
84
+ return right ;
85
+ }
86
+ if (right == null ) {
87
+ return left ;
88
+ }
79
89
80
90
if (left .priority > right .priority ) {
81
91
left .right = merge (left .right , right );
@@ -158,7 +168,9 @@ public TreapNode delete(int value) {
158
168
}
159
169
160
170
private TreapNode deleteNode (TreapNode root , int value ) {
161
- if (root == null ) return null ;
171
+ if (root == null ) {
172
+ return null ;
173
+ }
162
174
163
175
if (value < root .value ) {
164
176
root .left = deleteNode (root .left , value );
@@ -168,7 +180,9 @@ private TreapNode deleteNode(TreapNode root, int value) {
168
180
root = merge (root .left , root .right );
169
181
}
170
182
171
- if (root != null ) root .updateSize ();
183
+ if (root != null ) {
184
+ root .updateSize ();
185
+ }
172
186
return root ;
173
187
}
174
188
@@ -182,7 +196,9 @@ public void inOrder() {
182
196
}
183
197
184
198
private void printInorder (TreapNode root ) {
185
- if (root == null ) return ;
199
+ if (root == null ) {
200
+ return ;
201
+ }
186
202
printInorder (root .left );
187
203
System .out .print (root .value + "," );
188
204
printInorder (root .right );
@@ -198,7 +214,9 @@ public void preOrder() {
198
214
}
199
215
200
216
private void printPreOrder (TreapNode root ) {
201
- if (root == null ) return ;
217
+ if (root == null ) {
218
+ return ;
219
+ }
202
220
System .out .print (root .value + "," );
203
221
printPreOrder (root .left );
204
222
printPreOrder (root .right );
@@ -214,7 +232,9 @@ public void postOrder() {
214
232
}
215
233
216
234
private void printPostOrder (TreapNode root ) {
217
- if (root == null ) return ;
235
+ if (root == null ) {
236
+ return ;
237
+ }
218
238
printPostOrder (root .left );
219
239
printPostOrder (root .right );
220
240
System .out .print (root .value + "," );
@@ -232,14 +252,19 @@ public TreapNode search(int value) {
232
252
}
233
253
234
254
private TreapNode searchVal (TreapNode root , int value ) {
235
- if (root == null ) return null ;
255
+ if (root == null ) {
256
+ return null ;
257
+ }
236
258
237
- if (root .value == value )
259
+ if (root .value == value ) {
238
260
return root ;
239
- else if (root .value < value )
261
+ }
262
+ else if (root .value < value ) {
240
263
return searchVal (root .right , value );
241
- else
264
+ }
265
+ else {
242
266
return searchVal (root .left , value );
267
+ }
243
268
}
244
269
245
270
/**
@@ -288,7 +313,9 @@ public TreapNode upperBound(int value) {
288
313
* returns size of the Treap
289
314
*/
290
315
public int size () {
291
- if (root == null ) return 0 ;
316
+ if (root == null ) {
317
+ return 0 ;
318
+ }
292
319
return root .size ;
293
320
}
294
321
0 commit comments