Skip to content

Commit 20d0d31

Browse files
committed
add null checker in test and more distinct in rabin function
1 parent 92ff833 commit 20d0d31

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

src/main/java/com/thealgorithms/randomized/ClosestPair.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ public static Object[] rabinRandomizedClosestPair(List<Point> points) {
7878

7979
// Confirm neither closestA nor closestB are null
8080
if (closestA == null || closestB == null) {
81-
closestA = points.get(0);
82-
closestB = points.get(1);
83-
delta = euclideanDistance(closestA, closestB);
81+
return new Object[] {points.get(0), points.get(1), euclideanDistance(points.get(0), points.get(1))};
8482
}
8583

8684
// Create a grid, We will use "Probabilistic Filtering" by only checking

src/test/java/com/thealgorithms/randomized/ClosestPairTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ void testStandardCaseClosestPair() {
2323
void testTwoDistinctPoints() {
2424
List<Point> points = Arrays.asList(new Point(1, 2), new Point(2, 3));
2525
Object[] closestPair = ClosestPair.rabinRandomizedClosestPair(points);
26+
27+
assertNotNull(closestPair[0]);
28+
assertNotNull(closestPair[1]);
29+
2630
assertTrue((closestPair[0].equals(points.get(0)) && closestPair[1].equals(points.get(1))) || (closestPair[1].equals(points.get(0)) && closestPair[0].equals(points.get(1))));
2731
assertEquals(closestPair[2], ClosestPair.euclideanDistance(points.get(0), points.get(1)));
2832
}

0 commit comments

Comments
 (0)