Skip to content

Commit 2bebc4b

Browse files
committed
initial implementation (10/100)
1 parent 077cb7b commit 2bebc4b

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.TreeSet;
2+
import java.util.ArrayList;
3+
import edu.princeton.cs.algs4.Point2D;
4+
import edu.princeton.cs.algs4.RectHV;
5+
6+
public class KdTree {
7+
private TreeSet<Point2D> tree;
8+
9+
// construct an empty set of points
10+
public KdTree() {
11+
this.tree = new TreeSet<Point2D>();
12+
}
13+
// is the set empty?
14+
public boolean isEmpty() {
15+
return this.tree.isEmpty();
16+
}
17+
// number of points in the set
18+
public int size() {
19+
return tree.size();
20+
}
21+
// add the point to the set (if it is not already in the set)
22+
public void insert(Point2D p) {
23+
tree.add(p);
24+
}
25+
// // does the set contain point p?
26+
public boolean contains(Point2D p) {
27+
return tree.contains(p);
28+
}
29+
// draw all points to standard draw
30+
public void draw() {
31+
for (Point2D p : tree) {
32+
p.draw();
33+
}
34+
}
35+
// all points that are inside the rectangle (or on the boundary)
36+
public Iterable<Point2D> range(RectHV rect) {
37+
ArrayList<Point2D> points = new ArrayList<Point2D>();
38+
for (Point2D p : tree) {
39+
if (rect.contains(p)) {
40+
points.add(p);
41+
}
42+
}
43+
return points;
44+
}
45+
// // a nearest neighbor in the set to point p; null if the set is empty
46+
public Point2D nearest(Point2D p) {
47+
Point2D nearest = null;
48+
double minDistance = Double.POSITIVE_INFINITY;
49+
for (Point2D point : tree) {
50+
double distance = p.distanceSquaredTo(point);
51+
if (distance < minDistance) {
52+
nearest = point;
53+
minDistance = distance;
54+
}
55+
}
56+
return nearest;
57+
}
58+
59+
// unit testing of the methods (optional)
60+
public static void main(String[] args) {
61+
// TODO:
62+
}
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.TreeSet;
2+
import java.util.ArrayList;
3+
import edu.princeton.cs.algs4.Point2D;
4+
import edu.princeton.cs.algs4.RectHV;
5+
6+
public class PointSET {
7+
private TreeSet<Point2D> tree;
8+
9+
// construct an empty set of points
10+
public PointSET() {
11+
this.tree = new TreeSet<Point2D>();
12+
}
13+
// is the set empty?
14+
public boolean isEmpty() {
15+
return this.tree.isEmpty();
16+
}
17+
// number of points in the set
18+
public int size() {
19+
return tree.size();
20+
}
21+
// add the point to the set (if it is not already in the set)
22+
public void insert(Point2D p) {
23+
tree.add(p);
24+
}
25+
// // does the set contain point p?
26+
public boolean contains(Point2D p) {
27+
return tree.contains(p);
28+
}
29+
// draw all points to standard draw
30+
public void draw() {
31+
for (Point2D p : tree) {
32+
p.draw();
33+
}
34+
}
35+
// all points that are inside the rectangle (or on the boundary)
36+
public Iterable<Point2D> range(RectHV rect) {
37+
ArrayList<Point2D> points = new ArrayList<Point2D>();
38+
for (Point2D p : tree) {
39+
if (rect.contains(p)) {
40+
points.add(p);
41+
}
42+
}
43+
return points;
44+
}
45+
// // a nearest neighbor in the set to point p; null if the set is empty
46+
public Point2D nearest(Point2D p) {
47+
Point2D nearest = null;
48+
double minDistance = Double.POSITIVE_INFINITY;
49+
for (Point2D point : tree) {
50+
double distance = p.distanceTo(point);
51+
if (distance < minDistance) {
52+
nearest = point;
53+
minDistance = distance;
54+
}
55+
}
56+
return nearest;
57+
}
58+
59+
// unit testing of the methods (optional)
60+
public static void main(String[] args) {
61+
// TODO:
62+
}
63+
}

0 commit comments

Comments
 (0)