From 432657dd1f289a73d654781f61c3d41dc40e30d8 Mon Sep 17 00:00:00 2001 From: Shree Harish S Date: Thu, 3 Oct 2024 18:37:58 +0530 Subject: [PATCH 01/11] Added Treap class and test for the same --- .../datastructures/trees/Treap.java | 334 ++++++++++++++++++ .../datastructures/trees/TreapTest.java | 67 ++++ 2 files changed, 401 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/trees/Treap.java create mode 100644 src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java new file mode 100644 index 000000000000..bb5c25e5e4e1 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java @@ -0,0 +1,334 @@ +package com.thealgorithms.datastructures.trees; + +import java.util.Random; + +/** + * Treap -> Tree + Heap + * Also called as cartesian tree + * + * @see + * + */ + +public class Treap{ + + public 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, size; + public TreapNode left, right; + + public TreapNode(int value, int priority) { + this.value = value; + this.priority = priority; + this.size = 1; + this.left = this.right = null; + } + + /** + * updateSize -> updates the subtree size of the current node + */ + private void updateSize(){ + this.size = 1; + if(this.left != null) this.size += this.left.size; + if(this.right != null) this.size += this.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(){ + this.root = null; + } + + public Treap(int[] nodeValues){ + for(int nodeValue : nodeValues) this.insert(nodeValue); + } + + /** + * 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 + "}"; + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java b/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java new file mode 100644 index 000000000000..d8cfb35669c2 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java @@ -0,0 +1,67 @@ +package com.thealgorithms.datastructures.trees; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class TreapTest{ + + @Test + void arrayBuild() { + int[] arr = {5,9,6,2,3,8,1}; + Treap treap = new Treap(arr); + assertEquals("{1,2,3,5,6,8,9,}", treap.inOrder()); + } + + @Test + void build() { + Treap treap = new Treap(); + treap.insert(4); + treap.insert(5); + treap.insert(9); + treap.insert(2); + assertEquals("{2,4,5,9,}", treap.inOrder()); + } + + @Test + void delete(){ + int[] arr = {5,9,6,2,3,8,1}; + Treap treap = new Treap(arr); + treap.delete(5); + assertEquals("{1,2,3,6,8,9,}", treap.inOrder()); + } + + @Test + void searchAndFound(){ + int[] arr = {5,9,6,2,3,8,1}; + Treap treap = new Treap(arr); + assertEquals(5, treap.search(5).value()); + } + + @Test + void searchAndNotFound(){ + int[] arr = {5,9,6,2,3,8,1}; + Treap treap = new Treap(arr); + assertEquals(null, treap.search(4)); + } + + @Test + void lowerBound(){ + int[] arr = {5,9,6,2,3,8,1}; + Treap treap = new Treap(arr); + assertEquals(5, treap.lowerBound(4)); + } + + @Test upperBound(){ + int[] arr = {5,9,6,2,3,8,1}; + Treap treap = new Treap(arr); + assertEquals(6, treap.upperBound(5)); + } + + @Test misc(){ + int[] arr = {5,9,6,2,3,8,1}; + Treap treap = new Treap(arr); + assertEquals(7, treap.size()); + assertEquals(false, treap.isEmpty()); + } +} From bcaebd1cf20deccfcffdde5fb7c54a3aec11b9e3 Mon Sep 17 00:00:00 2001 From: Shree Harish S Date: Thu, 3 Oct 2024 18:50:46 +0530 Subject: [PATCH 02/11] Added Treap class and test for the same --- .../datastructures/trees/Treap.java | 116 +++++++++--------- .../datastructures/trees/TreapTest.java | 28 ++--- 2 files changed, 70 insertions(+), 74 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java index bb5c25e5e4e1..a99ef5c6123d 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java @@ -10,9 +10,9 @@ * */ -public class Treap{ +public class Treap { - public class TreapNode{ + public class TreapNode { /** * TreapNode class defines the individual nodes in the Treap * @@ -42,10 +42,10 @@ public TreapNode(int value, int priority) { /** * updateSize -> updates the subtree size of the current node */ - private void updateSize(){ + private void updateSize() { this.size = 1; - if(this.left != null) this.size += this.left.size; - if(this.right != null) this.size += this.right.size; + if (this.left != null) this.size += this.left.size; + if (this.right != null) this.size += this.right.size; } } @@ -62,12 +62,12 @@ private void updateSize(){ * Treap() -> create an empty Treap * Treap(int[] nodeValues) -> add the elements given in the array to the Treap */ - public Treap(){ + public Treap() { this.root = null; } - public Treap(int[] nodeValues){ - for(int nodeValue : nodeValues) this.insert(nodeValue); + public Treap(int[] nodeValues) { + for (int nodeValue : nodeValues) this.insert(nodeValue); } /** @@ -77,16 +77,15 @@ public Treap(int[] nodeValues){ * @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; + private TreapNode merge(TreapNode left, TreapNode right) { + if (left == null) return right; + if (right == null) return left; - if(left.priority > right.priority){ + if (left.priority > right.priority) { left.right = merge(left.right, right); left.updateSize(); return left; - } - else{ + } else { right.left = merge(left, right.left); right.updateSize(); return right; @@ -102,20 +101,19 @@ private TreapNode merge(TreapNode left, TreapNode right){ * 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){ + private TreapNode[] split(TreapNode node, int key) { + if (node == null) { return new TreapNode[] {null, null}; } TreapNode[] result; - if(node.value <= key){ + if (node.value <= key) { result = split(node.right, key); node.right = result[0]; node.updateSize(); result[0] = node; - } - else{ + } else { result = split(node.left, key); node.left = result[1]; node.updateSize(); @@ -131,8 +129,8 @@ private TreapNode[] split(TreapNode node, int key){ * @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){ + public TreapNode insert(int value) { + if (root == null){ root = new TreapNode(value, random.nextInt()); return root; } @@ -158,39 +156,37 @@ public TreapNode insert(int value){ * @param value value to be deleted from the Treap * @return root of the Treap where delete has been performed */ - public TreapNode delete(int value){ + public TreapNode delete(int value) { root = deleteNode(root, value); return root; } - private TreapNode deleteNode(TreapNode root, int value){ - if(root == null) return null; + private TreapNode deleteNode(TreapNode root, int value) { + if (root == null) return null; - if(value < root.value){ + if (value < root.value) { root.left = deleteNode(root.left, value); - } - else if(value > root.value){ + } else if (value > root.value) { root.right = deleteNode(root.right, value); - } - else{ + } else { root = merge(root.left, root.right); } - if(root != null) root.updateSize(); + if (root != null) root.updateSize(); return root; } /** * print inorder traversal of the Treap */ - public void inOrder(){ + public void inOrder() { System.out.print("{"); printInorder(root); System.out.print("}"); } - private void printInorder(TreapNode root){ - if(root == null) return; + private void printInorder(TreapNode root) { + if (root == null) return; printInorder(root.left); System.out.print(root.value + ","); printInorder(root.right); @@ -199,14 +195,14 @@ private void printInorder(TreapNode root){ /** * print preOrder traversal of the Treap */ - public void preOrder(){ + public void preOrder() { System.out.print("{"); printPreOrder(root); System.out.print("}"); } - private void printPreOrder(TreapNode root){ - if(root == null) return; + private void printPreOrder(TreapNode root) { + if (root == null) return; System.out.print(root.value + ","); printPreOrder(root.left); printPreOrder(root.right); @@ -215,14 +211,14 @@ private void printPreOrder(TreapNode root){ /** * print postOrder traversal of the Treap */ - public void postOrder(){ + public void postOrder() { System.out.print("{"); printPostOrder(root); System.out.print("}"); } - private void printPostOrder(TreapNode root){ - if(root == null) return; + private void printPostOrder(TreapNode root) { + if (root == null) return; printPostOrder(root.left); printPostOrder(root.right); System.out.print(root.value + ","); @@ -235,14 +231,14 @@ private void printPostOrder(TreapNode root){ * @return node containing the value * null if not found */ - public TreapNode search(int value){ + public TreapNode search(int value) { return searchVal(root, value); } - private TreapNode searchVal(TreapNode root, int value){ - if(root == null) return null; + private TreapNode searchVal(TreapNode root, int value) { + if (root == null) return null; - if(root.value == value) return root; + if (root.value == value) return root; else if(root.value < value) return searchVal(root.right, value); else return searchVal(root.left, value); } @@ -253,16 +249,16 @@ private TreapNode searchVal(TreapNode root, int value){ * @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){ + public TreapNode lowerBound(int value) { TreapNode lowerBoundNode = null; TreapNode current = root; - while(current != null){ - if(current.value >= value){ + while (current != null) { + if (current.value >= value) { lowerBoundNode = current; current = current.left; - } - else current = current.right; + } else + current = current.right; } return lowerBoundNode; @@ -274,16 +270,16 @@ public TreapNode lowerBound(int value){ * @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){ + public TreapNode upperBound(int value) { TreapNode upperBoundNode = null; TreapNode current = root; - while(current != null){ - if(current.value > value){ + while (current != null) { + if (current.value > value) { upperBoundNode = current; current = current.left; - } - else current = current.right; + } else + current = current.right; } return upperBoundNode; @@ -292,43 +288,43 @@ public TreapNode upperBound(int value){ /** * returns size of the Treap */ - public int size(){ - if(root == null) return 0; + public int size() { + if (root == null) return 0; return root.size; } /** * returns if Treap is empty */ - public boolean isEmpty(){ + public boolean isEmpty() { return root == null; } /** * returns root node of the Treap */ - public TreapNode getRoot(){ + public TreapNode getRoot() { return root; } /** * returns left node of the TreapNode */ - public TreapNode getLeft(TreapNode node){ + public TreapNode getLeft(TreapNode node) { return node.left; } /** * returns the right node of the TreapNode */ - public TreapNode getRight(TreapNode node){ + 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){ + public String toString(TreapNode node) { return "{value : " + node.value + ", priority : " + node.priority + ", subTreeSize = " + node.size + ", left = " + node.left + ", right = " + node.right + "}"; } } \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java b/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java index d8cfb35669c2..5d346f466378 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java @@ -4,11 +4,11 @@ import org.junit.jupiter.api.Test; -public class TreapTest{ +public class TreapTest { @Test void arrayBuild() { - int[] arr = {5,9,6,2,3,8,1}; + int[] arr = {5, 9, 6, 2, 3, 8, 1}; Treap treap = new Treap(arr); assertEquals("{1,2,3,5,6,8,9,}", treap.inOrder()); } @@ -24,42 +24,42 @@ void build() { } @Test - void delete(){ - int[] arr = {5,9,6,2,3,8,1}; + void delete() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; Treap treap = new Treap(arr); treap.delete(5); assertEquals("{1,2,3,6,8,9,}", treap.inOrder()); } @Test - void searchAndFound(){ - int[] arr = {5,9,6,2,3,8,1}; + void searchAndFound() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; Treap treap = new Treap(arr); assertEquals(5, treap.search(5).value()); } @Test - void searchAndNotFound(){ - int[] arr = {5,9,6,2,3,8,1}; + void searchAndNotFound() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; Treap treap = new Treap(arr); assertEquals(null, treap.search(4)); } @Test - void lowerBound(){ - int[] arr = {5,9,6,2,3,8,1}; + void lowerBound() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; Treap treap = new Treap(arr); assertEquals(5, treap.lowerBound(4)); } - @Test upperBound(){ - int[] arr = {5,9,6,2,3,8,1}; + @Test upperBound() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; Treap treap = new Treap(arr); assertEquals(6, treap.upperBound(5)); } - @Test misc(){ - int[] arr = {5,9,6,2,3,8,1}; + @Test misc() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; Treap treap = new Treap(arr); assertEquals(7, treap.size()); assertEquals(false, treap.isEmpty()); From c31aab74cf42f1bbae54ce970d7d48ef848f27f9 Mon Sep 17 00:00:00 2001 From: Shree Harish S Date: Thu, 3 Oct 2024 18:52:58 +0530 Subject: [PATCH 03/11] Added Treap class and test for the same --- src/main/java/com/thealgorithms/datastructures/trees/Treap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java index a99ef5c6123d..c18759135369 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java @@ -67,7 +67,7 @@ public Treap() { } public Treap(int[] nodeValues) { - for (int nodeValue : nodeValues) this.insert(nodeValue); + for (int nodeValue : nodeValues) insert(nodeValue); } /** From bece8c61e95c70b04519753f3a97599de95b2ba2 Mon Sep 17 00:00:00 2001 From: Shree Harish S Date: Thu, 3 Oct 2024 19:08:51 +0530 Subject: [PATCH 04/11] Added Treap class and test for the same --- .../datastructures/trees/Treap.java | 41 ++++++++++--------- .../datastructures/trees/TreapTest.java | 8 ++-- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java index c18759135369..1e76fbc2c8a0 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java @@ -5,7 +5,7 @@ /** * Treap -> Tree + Heap * Also called as cartesian tree - * + * * @see * */ @@ -15,23 +15,23 @@ public class Treap { public 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, size; public TreapNode left, right; - + public TreapNode(int value, int priority) { this.value = value; this.priority = priority; @@ -58,7 +58,7 @@ private void updateSize() { /** * Constructors - * + * * Treap() -> create an empty Treap * Treap(int[] nodeValues) -> add the elements given in the array to the Treap */ @@ -72,7 +72,7 @@ public Treap(int[] nodeValues) { /** * merges two Treaps left and right into a single Treap - * + * * @param left left Treap * @param right right Treap * @return root of merged Treap @@ -94,7 +94,7 @@ private TreapNode merge(TreapNode left, TreapNode 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. @@ -130,7 +130,7 @@ private TreapNode[] split(TreapNode node, int key) { * @return root of the Treap where the value is inserted */ public TreapNode insert(int value) { - if (root == null){ + if (root == null) { root = new TreapNode(value, random.nextInt()); return root; } @@ -142,7 +142,7 @@ public TreapNode insert(int value) { TreapNode tempMerged = merge(splitted[0], node); tempMerged.updateSize(); - TreapNode merged =merge(tempMerged, splitted[1]); + TreapNode merged = merge(tempMerged, splitted[1]); merged.updateSize(); root = merged; @@ -152,7 +152,7 @@ public TreapNode insert(int value) { /** * 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 */ @@ -171,7 +171,7 @@ private TreapNode deleteNode(TreapNode root, int value) { } else { root = merge(root.left, root.right); } - + if (root != null) root.updateSize(); return root; } @@ -238,14 +238,17 @@ public TreapNode search(int 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); + 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 */ @@ -257,7 +260,7 @@ public TreapNode lowerBound(int value) { if (current.value >= value) { lowerBoundNode = current; current = current.left; - } else + } else current = current.right; } @@ -266,7 +269,7 @@ public TreapNode lowerBound(int value) { /** * 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 */ diff --git a/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java b/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java index 5d346f466378..60002acb5582 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java @@ -13,7 +13,7 @@ void arrayBuild() { assertEquals("{1,2,3,5,6,8,9,}", treap.inOrder()); } - @Test + @Test void build() { Treap treap = new Treap(); treap.insert(4); @@ -52,13 +52,15 @@ void lowerBound() { assertEquals(5, treap.lowerBound(4)); } - @Test upperBound() { + @Test + void upperBound() { int[] arr = {5, 9, 6, 2, 3, 8, 1}; Treap treap = new Treap(arr); assertEquals(6, treap.upperBound(5)); } - @Test misc() { + @Test + void misc() { int[] arr = {5, 9, 6, 2, 3, 8, 1}; Treap treap = new Treap(arr); assertEquals(7, treap.size()); From 9226300d57a18ef700b093aef0ddcaadfc4c321c Mon Sep 17 00:00:00 2001 From: Shree Harish S Date: Thu, 3 Oct 2024 19:15:15 +0530 Subject: [PATCH 05/11] Added Treap class and test for the same --- .../datastructures/trees/Treap.java | 16 ++++++++-------- .../datastructures/trees/TreapTest.java | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java index 1e76fbc2c8a0..7c9a317f6fc7 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java @@ -16,10 +16,10 @@ public class TreapNode { /** * TreapNode class defines the individual nodes in the Treap * - * value -> holds the value of the node. + * value -> holds the value of the node. * Binary Search Tree is built based on value. * - * priority -> holds the priority of the node. + * priority -> holds the priority of the node. * Heaps are maintained based on priority. * It is randomly assigned * @@ -125,7 +125,7 @@ private TreapNode[] split(TreapNode node, int key) { /** * 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 */ @@ -174,7 +174,7 @@ private TreapNode deleteNode(TreapNode root, int value) { if (root != null) root.updateSize(); return root; - } + } /** * print inorder traversal of the Treap @@ -226,7 +226,7 @@ private void printPostOrder(TreapNode root) { /** * Search a value in the Treap - * + * * @param value value to be searched for * @return node containing the value * null if not found @@ -238,11 +238,11 @@ public TreapNode search(int value) { private TreapNode searchVal(TreapNode root, int value) { if (root == null) return null; - if (root.value == value) + if (root.value == value) return root; - else if(root.value < value) + else if(root.value < value) return searchVal(root.right, value); - else + else return searchVal(root.left, value); } diff --git a/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java b/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java index 60002acb5582..428316ebbe8c 100644 --- a/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java +++ b/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java @@ -52,14 +52,14 @@ void lowerBound() { assertEquals(5, treap.lowerBound(4)); } - @Test + @Test void upperBound() { int[] arr = {5, 9, 6, 2, 3, 8, 1}; Treap treap = new Treap(arr); assertEquals(6, treap.upperBound(5)); } - @Test + @Test void misc() { int[] arr = {5, 9, 6, 2, 3, 8, 1}; Treap treap = new Treap(arr); From 5fb73ea2afbc6ef40c008b4ce55530018e9dc88d Mon Sep 17 00:00:00 2001 From: Shree Harish S Date: Thu, 3 Oct 2024 19:17:08 +0530 Subject: [PATCH 06/11] Added Treap class and test for the same --- src/main/java/com/thealgorithms/datastructures/trees/Treap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java index 7c9a317f6fc7..ac0ac8768c88 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java @@ -240,7 +240,7 @@ private TreapNode searchVal(TreapNode root, int value) { if (root.value == value) return root; - else if(root.value < value) + else if (root.value < value) return searchVal(root.right, value); else return searchVal(root.left, value); From c566f9d6d61cab3b50a9ef8214a1f69883c332b9 Mon Sep 17 00:00:00 2001 From: Shree Harish S Date: Thu, 3 Oct 2024 19:20:13 +0530 Subject: [PATCH 07/11] Added Treap class and test for the same --- src/main/java/com/thealgorithms/datastructures/trees/Treap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java index ac0ac8768c88..f3efa9d5a73c 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java @@ -12,7 +12,7 @@ public class Treap { - public class TreapNode { + static class TreapNode { /** * TreapNode class defines the individual nodes in the Treap * From e24dca197d79d1fbe1891e0a14d61adece96eac0 Mon Sep 17 00:00:00 2001 From: Shree Harish S Date: Thu, 3 Oct 2024 19:22:23 +0530 Subject: [PATCH 08/11] Added Treap class --- .../datastructures/trees/TreapTest.java | 69 ------------------- 1 file changed, 69 deletions(-) delete mode 100644 src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java diff --git a/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java b/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java deleted file mode 100644 index 428316ebbe8c..000000000000 --- a/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.thealgorithms.datastructures.trees; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -public class TreapTest { - - @Test - void arrayBuild() { - int[] arr = {5, 9, 6, 2, 3, 8, 1}; - Treap treap = new Treap(arr); - assertEquals("{1,2,3,5,6,8,9,}", treap.inOrder()); - } - - @Test - void build() { - Treap treap = new Treap(); - treap.insert(4); - treap.insert(5); - treap.insert(9); - treap.insert(2); - assertEquals("{2,4,5,9,}", treap.inOrder()); - } - - @Test - void delete() { - int[] arr = {5, 9, 6, 2, 3, 8, 1}; - Treap treap = new Treap(arr); - treap.delete(5); - assertEquals("{1,2,3,6,8,9,}", treap.inOrder()); - } - - @Test - void searchAndFound() { - int[] arr = {5, 9, 6, 2, 3, 8, 1}; - Treap treap = new Treap(arr); - assertEquals(5, treap.search(5).value()); - } - - @Test - void searchAndNotFound() { - int[] arr = {5, 9, 6, 2, 3, 8, 1}; - Treap treap = new Treap(arr); - assertEquals(null, treap.search(4)); - } - - @Test - void lowerBound() { - int[] arr = {5, 9, 6, 2, 3, 8, 1}; - Treap treap = new Treap(arr); - assertEquals(5, treap.lowerBound(4)); - } - - @Test - void upperBound() { - int[] arr = {5, 9, 6, 2, 3, 8, 1}; - Treap treap = new Treap(arr); - assertEquals(6, treap.upperBound(5)); - } - - @Test - void misc() { - int[] arr = {5, 9, 6, 2, 3, 8, 1}; - Treap treap = new Treap(arr); - assertEquals(7, treap.size()); - assertEquals(false, treap.isEmpty()); - } -} From 777bca3b7f618b0db0fb58945143500c32da4938 Mon Sep 17 00:00:00 2001 From: Shree Harish S Date: Thu, 3 Oct 2024 19:22:59 +0530 Subject: [PATCH 09/11] Added Treap class --- src/main/java/com/thealgorithms/datastructures/trees/Treap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java index f3efa9d5a73c..547214bfb107 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java @@ -330,4 +330,4 @@ public TreapNode getRight(TreapNode node) { public String toString(TreapNode node) { return "{value : " + node.value + ", priority : " + node.priority + ", subTreeSize = " + node.size + ", left = " + node.left + ", right = " + node.right + "}"; } -} \ No newline at end of file +} From 1018b882b77f602209485627e97a26fed4d5c994 Mon Sep 17 00:00:00 2001 From: Shree Harish S Date: Thu, 3 Oct 2024 19:26:26 +0530 Subject: [PATCH 10/11] Added Treap class and test for the same --- src/main/java/com/thealgorithms/datastructures/trees/Treap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java index f3efa9d5a73c..547214bfb107 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/Treap.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/Treap.java @@ -330,4 +330,4 @@ public TreapNode getRight(TreapNode node) { public String toString(TreapNode node) { return "{value : " + node.value + ", priority : " + node.priority + ", subTreeSize = " + node.size + ", left = " + node.left + ", right = " + node.right + "}"; } -} \ No newline at end of file +} From dcd61f595e680fa75269265a855d5dc15376f942 Mon Sep 17 00:00:00 2001 From: Shree Harish S Date: Thu, 3 Oct 2024 19:27:14 +0530 Subject: [PATCH 11/11] Added Treap class and test for the same --- .../datastructures/trees/TreapTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java diff --git a/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java b/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java new file mode 100644 index 000000000000..428316ebbe8c --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java @@ -0,0 +1,69 @@ +package com.thealgorithms.datastructures.trees; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class TreapTest { + + @Test + void arrayBuild() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; + Treap treap = new Treap(arr); + assertEquals("{1,2,3,5,6,8,9,}", treap.inOrder()); + } + + @Test + void build() { + Treap treap = new Treap(); + treap.insert(4); + treap.insert(5); + treap.insert(9); + treap.insert(2); + assertEquals("{2,4,5,9,}", treap.inOrder()); + } + + @Test + void delete() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; + Treap treap = new Treap(arr); + treap.delete(5); + assertEquals("{1,2,3,6,8,9,}", treap.inOrder()); + } + + @Test + void searchAndFound() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; + Treap treap = new Treap(arr); + assertEquals(5, treap.search(5).value()); + } + + @Test + void searchAndNotFound() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; + Treap treap = new Treap(arr); + assertEquals(null, treap.search(4)); + } + + @Test + void lowerBound() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; + Treap treap = new Treap(arr); + assertEquals(5, treap.lowerBound(4)); + } + + @Test + void upperBound() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; + Treap treap = new Treap(arr); + assertEquals(6, treap.upperBound(5)); + } + + @Test + void misc() { + int[] arr = {5, 9, 6, 2, 3, 8, 1}; + Treap treap = new Treap(arr); + assertEquals(7, treap.size()); + assertEquals(false, treap.isEmpty()); + } +}