Skip to content

Commit bcaebd1

Browse files
committed
Added Treap class and test for the same
1 parent 432657d commit bcaebd1

File tree

2 files changed

+70
-74
lines changed
  • src
    • main/java/com/thealgorithms/datastructures/trees
    • test/java/com/thealgorithms/datastructures/trees

2 files changed

+70
-74
lines changed

src/main/java/com/thealgorithms/datastructures/trees/Treap.java

+56-60
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
* <a href = "https://cp-algorithms.com/data_structures/treap.html" />
1111
*/
1212

13-
public class Treap{
13+
public class Treap {
1414

15-
public class TreapNode{
15+
public class TreapNode {
1616
/**
1717
* TreapNode class defines the individual nodes in the Treap
1818
*
@@ -42,10 +42,10 @@ public TreapNode(int value, int priority) {
4242
/**
4343
* updateSize -> updates the subtree size of the current node
4444
*/
45-
private void updateSize(){
45+
private void updateSize() {
4646
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;
4949
}
5050
}
5151

@@ -62,12 +62,12 @@ private void updateSize(){
6262
* Treap() -> create an empty Treap
6363
* Treap(int[] nodeValues) -> add the elements given in the array to the Treap
6464
*/
65-
public Treap(){
65+
public Treap() {
6666
this.root = null;
6767
}
6868

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);
7171
}
7272

7373
/**
@@ -77,16 +77,15 @@ public Treap(int[] nodeValues){
7777
* @param right right Treap
7878
* @return root of merged Treap
7979
*/
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;
8383

84-
if(left.priority > right.priority){
84+
if (left.priority > right.priority) {
8585
left.right = merge(left.right, right);
8686
left.updateSize();
8787
return left;
88-
}
89-
else{
88+
} else {
9089
right.left = merge(left, right.left);
9190
right.updateSize();
9291
return right;
@@ -102,20 +101,19 @@ private TreapNode merge(TreapNode left, TreapNode right){
102101
* TreapNode[0] contains the root of left Treap after split
103102
* TreapNode[1] contains the root of right Treap after split
104103
*/
105-
private TreapNode[] split(TreapNode node, int key){
106-
if(node == null){
104+
private TreapNode[] split(TreapNode node, int key) {
105+
if (node == null) {
107106
return new TreapNode[] {null, null};
108107
}
109108

110109
TreapNode[] result;
111110

112-
if(node.value <= key){
111+
if (node.value <= key) {
113112
result = split(node.right, key);
114113
node.right = result[0];
115114
node.updateSize();
116115
result[0] = node;
117-
}
118-
else{
116+
} else {
119117
result = split(node.left, key);
120118
node.left = result[1];
121119
node.updateSize();
@@ -131,8 +129,8 @@ private TreapNode[] split(TreapNode node, int key){
131129
* @param value value to be inserted into the Treap
132130
* @return root of the Treap where the value is inserted
133131
*/
134-
public TreapNode insert(int value){
135-
if(root == null){
132+
public TreapNode insert(int value) {
133+
if (root == null){
136134
root = new TreapNode(value, random.nextInt());
137135
return root;
138136
}
@@ -158,39 +156,37 @@ public TreapNode insert(int value){
158156
* @param value value to be deleted from the Treap
159157
* @return root of the Treap where delete has been performed
160158
*/
161-
public TreapNode delete(int value){
159+
public TreapNode delete(int value) {
162160
root = deleteNode(root, value);
163161
return root;
164162
}
165163

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;
168166

169-
if(value < root.value){
167+
if (value < root.value) {
170168
root.left = deleteNode(root.left, value);
171-
}
172-
else if(value > root.value){
169+
} else if (value > root.value) {
173170
root.right = deleteNode(root.right, value);
174-
}
175-
else{
171+
} else {
176172
root = merge(root.left, root.right);
177173
}
178174

179-
if(root != null) root.updateSize();
175+
if (root != null) root.updateSize();
180176
return root;
181177
}
182178

183179
/**
184180
* print inorder traversal of the Treap
185181
*/
186-
public void inOrder(){
182+
public void inOrder() {
187183
System.out.print("{");
188184
printInorder(root);
189185
System.out.print("}");
190186
}
191187

192-
private void printInorder(TreapNode root){
193-
if(root == null) return;
188+
private void printInorder(TreapNode root) {
189+
if (root == null) return;
194190
printInorder(root.left);
195191
System.out.print(root.value + ",");
196192
printInorder(root.right);
@@ -199,14 +195,14 @@ private void printInorder(TreapNode root){
199195
/**
200196
* print preOrder traversal of the Treap
201197
*/
202-
public void preOrder(){
198+
public void preOrder() {
203199
System.out.print("{");
204200
printPreOrder(root);
205201
System.out.print("}");
206202
}
207203

208-
private void printPreOrder(TreapNode root){
209-
if(root == null) return;
204+
private void printPreOrder(TreapNode root) {
205+
if (root == null) return;
210206
System.out.print(root.value + ",");
211207
printPreOrder(root.left);
212208
printPreOrder(root.right);
@@ -215,14 +211,14 @@ private void printPreOrder(TreapNode root){
215211
/**
216212
* print postOrder traversal of the Treap
217213
*/
218-
public void postOrder(){
214+
public void postOrder() {
219215
System.out.print("{");
220216
printPostOrder(root);
221217
System.out.print("}");
222218
}
223219

224-
private void printPostOrder(TreapNode root){
225-
if(root == null) return;
220+
private void printPostOrder(TreapNode root) {
221+
if (root == null) return;
226222
printPostOrder(root.left);
227223
printPostOrder(root.right);
228224
System.out.print(root.value + ",");
@@ -235,14 +231,14 @@ private void printPostOrder(TreapNode root){
235231
* @return node containing the value
236232
* null if not found
237233
*/
238-
public TreapNode search(int value){
234+
public TreapNode search(int value) {
239235
return searchVal(root, value);
240236
}
241237

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;
244240

245-
if(root.value == value) return root;
241+
if (root.value == value) return root;
246242
else if(root.value < value) return searchVal(root.right, value);
247243
else return searchVal(root.left, value);
248244
}
@@ -253,16 +249,16 @@ private TreapNode searchVal(TreapNode root, int value){
253249
* @param value value for which lowerBound is to be found
254250
* @return node which is the lowerBound of the value passed
255251
*/
256-
public TreapNode lowerBound(int value){
252+
public TreapNode lowerBound(int value) {
257253
TreapNode lowerBoundNode = null;
258254
TreapNode current = root;
259255

260-
while(current != null){
261-
if(current.value >= value){
256+
while (current != null) {
257+
if (current.value >= value) {
262258
lowerBoundNode = current;
263259
current = current.left;
264-
}
265-
else current = current.right;
260+
} else
261+
current = current.right;
266262
}
267263

268264
return lowerBoundNode;
@@ -274,16 +270,16 @@ public TreapNode lowerBound(int value){
274270
* @param value value for which upperBound is to be found
275271
* @return node which is the upperBound of the value passed
276272
*/
277-
public TreapNode upperBound(int value){
273+
public TreapNode upperBound(int value) {
278274
TreapNode upperBoundNode = null;
279275
TreapNode current = root;
280276

281-
while(current != null){
282-
if(current.value > value){
277+
while (current != null) {
278+
if (current.value > value) {
283279
upperBoundNode = current;
284280
current = current.left;
285-
}
286-
else current = current.right;
281+
} else
282+
current = current.right;
287283
}
288284

289285
return upperBoundNode;
@@ -292,43 +288,43 @@ public TreapNode upperBound(int value){
292288
/**
293289
* returns size of the Treap
294290
*/
295-
public int size(){
296-
if(root == null) return 0;
291+
public int size() {
292+
if (root == null) return 0;
297293
return root.size;
298294
}
299295

300296
/**
301297
* returns if Treap is empty
302298
*/
303-
public boolean isEmpty(){
299+
public boolean isEmpty() {
304300
return root == null;
305301
}
306302

307303
/**
308304
* returns root node of the Treap
309305
*/
310-
public TreapNode getRoot(){
306+
public TreapNode getRoot() {
311307
return root;
312308
}
313309

314310
/**
315311
* returns left node of the TreapNode
316312
*/
317-
public TreapNode getLeft(TreapNode node){
313+
public TreapNode getLeft(TreapNode node) {
318314
return node.left;
319315
}
320316

321317
/**
322318
* returns the right node of the TreapNode
323319
*/
324-
public TreapNode getRight(TreapNode node){
320+
public TreapNode getRight(TreapNode node) {
325321
return node.right;
326322
}
327323

328324
/**
329325
* prints the value, priority, size of the subtree of the TreapNode, left TreapNode and right TreapNode of the node
330326
*/
331-
public String toString(TreapNode node){
327+
public String toString(TreapNode node) {
332328
return "{value : " + node.value + ", priority : " + node.priority + ", subTreeSize = " + node.size + ", left = " + node.left + ", right = " + node.right + "}";
333329
}
334330
}

src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
import org.junit.jupiter.api.Test;
66

7-
public class TreapTest{
7+
public class TreapTest {
88

99
@Test
1010
void arrayBuild() {
11-
int[] arr = {5,9,6,2,3,8,1};
11+
int[] arr = {5, 9, 6, 2, 3, 8, 1};
1212
Treap treap = new Treap(arr);
1313
assertEquals("{1,2,3,5,6,8,9,}", treap.inOrder());
1414
}
@@ -24,42 +24,42 @@ void build() {
2424
}
2525

2626
@Test
27-
void delete(){
28-
int[] arr = {5,9,6,2,3,8,1};
27+
void delete() {
28+
int[] arr = {5, 9, 6, 2, 3, 8, 1};
2929
Treap treap = new Treap(arr);
3030
treap.delete(5);
3131
assertEquals("{1,2,3,6,8,9,}", treap.inOrder());
3232
}
3333

3434
@Test
35-
void searchAndFound(){
36-
int[] arr = {5,9,6,2,3,8,1};
35+
void searchAndFound() {
36+
int[] arr = {5, 9, 6, 2, 3, 8, 1};
3737
Treap treap = new Treap(arr);
3838
assertEquals(5, treap.search(5).value());
3939
}
4040

4141
@Test
42-
void searchAndNotFound(){
43-
int[] arr = {5,9,6,2,3,8,1};
42+
void searchAndNotFound() {
43+
int[] arr = {5, 9, 6, 2, 3, 8, 1};
4444
Treap treap = new Treap(arr);
4545
assertEquals(null, treap.search(4));
4646
}
4747

4848
@Test
49-
void lowerBound(){
50-
int[] arr = {5,9,6,2,3,8,1};
49+
void lowerBound() {
50+
int[] arr = {5, 9, 6, 2, 3, 8, 1};
5151
Treap treap = new Treap(arr);
5252
assertEquals(5, treap.lowerBound(4));
5353
}
5454

55-
@Test upperBound(){
56-
int[] arr = {5,9,6,2,3,8,1};
55+
@Test upperBound() {
56+
int[] arr = {5, 9, 6, 2, 3, 8, 1};
5757
Treap treap = new Treap(arr);
5858
assertEquals(6, treap.upperBound(5));
5959
}
6060

61-
@Test misc(){
62-
int[] arr = {5,9,6,2,3,8,1};
61+
@Test misc() {
62+
int[] arr = {5, 9, 6, 2, 3, 8, 1};
6363
Treap treap = new Treap(arr);
6464
assertEquals(7, treap.size());
6565
assertEquals(false, treap.isEmpty());

0 commit comments

Comments
 (0)