Skip to content

Commit bece8c6

Browse files
committed
Added Treap class and test for the same
1 parent 3bee8b3 commit bece8c6

File tree

2 files changed

+27
-22
lines changed
  • src
    • main/java/com/thealgorithms/datastructures/trees
    • test/java/com/thealgorithms/datastructures/trees

2 files changed

+27
-22
lines changed

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

+22-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* Treap -> Tree + Heap
77
* Also called as cartesian tree
8-
*
8+
*
99
* @see
1010
* <a href = "https://cp-algorithms.com/data_structures/treap.html" />
1111
*/
@@ -15,23 +15,23 @@ public class Treap {
1515
public class TreapNode {
1616
/**
1717
* TreapNode class defines the individual nodes in the Treap
18-
*
18+
*
1919
* value -> holds the value of the node.
2020
* Binary Search Tree is built based on value.
21-
*
21+
*
2222
* priority -> holds the priority of the node.
2323
* Heaps are maintained based on priority.
2424
* It is randomly assigned
25-
*
25+
*
2626
* size -> holds the size of the subtree with current node as root
27-
*
27+
*
2828
* left -> holds the left subtree
2929
* right -> holds the right subtree
3030
*/
3131
public int value;
3232
private int priority, size;
3333
public TreapNode left, right;
34-
34+
3535
public TreapNode(int value, int priority) {
3636
this.value = value;
3737
this.priority = priority;
@@ -58,7 +58,7 @@ private void updateSize() {
5858

5959
/**
6060
* Constructors
61-
*
61+
*
6262
* Treap() -> create an empty Treap
6363
* Treap(int[] nodeValues) -> add the elements given in the array to the Treap
6464
*/
@@ -72,7 +72,7 @@ public Treap(int[] nodeValues) {
7272

7373
/**
7474
* merges two Treaps left and right into a single Treap
75-
*
75+
*
7676
* @param left left Treap
7777
* @param right right Treap
7878
* @return root of merged Treap
@@ -94,7 +94,7 @@ private TreapNode merge(TreapNode left, TreapNode right) {
9494

9595
/**
9696
* split the Treap into two Treaps where left Treap has nodes <= key and right Treap has nodes > key
97-
*
97+
*
9898
* @param node root node to be split
9999
* @param key key to compare the nodes
100100
* @return TreapNode array of size 2.
@@ -130,7 +130,7 @@ private TreapNode[] split(TreapNode node, int key) {
130130
* @return root of the Treap where the value is inserted
131131
*/
132132
public TreapNode insert(int value) {
133-
if (root == null){
133+
if (root == null) {
134134
root = new TreapNode(value, random.nextInt());
135135
return root;
136136
}
@@ -142,7 +142,7 @@ public TreapNode insert(int value) {
142142
TreapNode tempMerged = merge(splitted[0], node);
143143
tempMerged.updateSize();
144144

145-
TreapNode merged =merge(tempMerged, splitted[1]);
145+
TreapNode merged = merge(tempMerged, splitted[1]);
146146
merged.updateSize();
147147

148148
root = merged;
@@ -152,7 +152,7 @@ public TreapNode insert(int value) {
152152

153153
/**
154154
* delete a value from root if present
155-
*
155+
*
156156
* @param value value to be deleted from the Treap
157157
* @return root of the Treap where delete has been performed
158158
*/
@@ -171,7 +171,7 @@ private TreapNode deleteNode(TreapNode root, int value) {
171171
} else {
172172
root = merge(root.left, root.right);
173173
}
174-
174+
175175
if (root != null) root.updateSize();
176176
return root;
177177
}
@@ -238,14 +238,17 @@ public TreapNode search(int value) {
238238
private TreapNode searchVal(TreapNode root, int value) {
239239
if (root == null) return null;
240240

241-
if (root.value == value) return root;
242-
else if(root.value < value) return searchVal(root.right, value);
243-
else return searchVal(root.left, value);
241+
if (root.value == value)
242+
return root;
243+
else if(root.value < value)
244+
return searchVal(root.right, value);
245+
else
246+
return searchVal(root.left, value);
244247
}
245248

246249
/**
247250
* find the lowerBound of a value in the Treap
248-
*
251+
*
249252
* @param value value for which lowerBound is to be found
250253
* @return node which is the lowerBound of the value passed
251254
*/
@@ -257,7 +260,7 @@ public TreapNode lowerBound(int value) {
257260
if (current.value >= value) {
258261
lowerBoundNode = current;
259262
current = current.left;
260-
} else
263+
} else
261264
current = current.right;
262265
}
263266

@@ -266,7 +269,7 @@ public TreapNode lowerBound(int value) {
266269

267270
/**
268271
* find the upperBound of a value in the Treap
269-
*
272+
*
270273
* @param value value for which upperBound is to be found
271274
* @return node which is the upperBound of the value passed
272275
*/

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void arrayBuild() {
1313
assertEquals("{1,2,3,5,6,8,9,}", treap.inOrder());
1414
}
1515

16-
@Test
16+
@Test
1717
void build() {
1818
Treap treap = new Treap();
1919
treap.insert(4);
@@ -52,13 +52,15 @@ void lowerBound() {
5252
assertEquals(5, treap.lowerBound(4));
5353
}
5454

55-
@Test upperBound() {
55+
@Test
56+
void upperBound() {
5657
int[] arr = {5, 9, 6, 2, 3, 8, 1};
5758
Treap treap = new Treap(arr);
5859
assertEquals(6, treap.upperBound(5));
5960
}
6061

61-
@Test misc() {
62+
@Test
63+
void misc() {
6264
int[] arr = {5, 9, 6, 2, 3, 8, 1};
6365
Treap treap = new Treap(arr);
6466
assertEquals(7, treap.size());

0 commit comments

Comments
 (0)