-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathKDTreeTest.java
63 lines (53 loc) · 1.61 KB
/
KDTreeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.thealgorithms.datastructures.trees;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class KDTreeTest {
KDTree.Point pointOf(int x, int y) {
return new KDTree.Point(new int[] {x, y});
}
@Test
void findMin() {
int[][] coordinates = {
{30, 40},
{5, 25},
{70, 70},
{10, 12},
{50, 30},
{35, 45},
};
KDTree kdTree = new KDTree(coordinates);
assertEquals(5, kdTree.findMin(0).getCoordinate(0));
assertEquals(12, kdTree.findMin(1).getCoordinate(1));
}
@Test
void delete() {
int[][] coordinates = {
{30, 40},
{5, 25},
{70, 70},
{10, 12},
{50, 30},
{35, 45},
};
KDTree kdTree = new KDTree(coordinates);
kdTree.delete(pointOf(30, 40));
assertEquals(35, kdTree.getRoot().getPoint().getCoordinate(0));
assertEquals(45, kdTree.getRoot().getPoint().getCoordinate(1));
}
@Test
void findNearest() {
int[][] coordinates = {
{2, 3},
{5, 4},
{9, 6},
{4, 7},
{8, 1},
{7, 2},
};
KDTree kdTree = new KDTree(coordinates);
assertEquals(pointOf(7, 2), kdTree.findNearest(pointOf(7, 2)));
assertEquals(pointOf(8, 1), kdTree.findNearest(pointOf(8, 1)));
assertEquals(pointOf(2, 3), kdTree.findNearest(pointOf(1, 1)));
assertEquals(pointOf(5, 4), kdTree.findNearest(pointOf(5, 5)));
}
}