Correctness: 35/35 tests passed
Memory: 16/16 tests passed
Timing: 42/42 tests passed
Aggregate score: 100.00%
1. 任务
任务说明
主要实现的就是两个功能,给一堆点,得到所给任意矩形中包含的点集,以及得到与所给任意点最近的点。
2. PointSET.java
这个类使用最暴力的方法,复杂度为N,也就是遍历所有点,找到符合条件的点。
这个类使用的是Java库函数中定义好的TreeSet,该集合是使用TreeMap实现的,TreeMap则是用红黑树实现的。因此在各个功能的实现中,直接调用TreeSet中的库函数即可。
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.RectHV;
import java.util.ArrayList;
import java.util.TreeSet;
public class PointSET {
// Java中的TreeSet是用TreeMap实现的,TreeMap是用的红黑树
private final TreeSet<Point2D> points;
// construct an empty set of points
public PointSET() {
points = new TreeSet<>();
}
// is the set empty?
public boolean isEmpty() {
return points.isEmpty();
}
// number of points in the set
public int size() {
return points.size();
}
// add the point to the set (if it is not already in the set)
public void insert(Point2D p) {
if (p == null)
throw new IllegalArgumentException();
points.add(p);
}
// does the set contain point p?
public boolean contains(Point2D p) {
if (p == null)
throw new IllegalArgumentException();
return points.contains(p);
}
// draw all points to standard draw
public void draw() {
for (Point2D p : points) {
p.draw();
}
}
// all points that are inside the rectangle (or on the boundary)
public Iterable<Point2D> range(RectHV rect) {
if (rect == null)
throw new IllegalArgumentException();
ArrayList<Point2D> a = new ArrayList<>();
for (Point2D p : points) {
if (rect.contains(p))
a.add(p);
}
return a;
}
// a nearest neighbor in the set to point p; null if the set is empty
public Point2D nearest(Point2D p) {
if (p == null)
throw new IllegalArgumentException();
if (isEmpty()) return null;
double minDis = Double.POSITIVE_INFINITY;
Point2D near = null;
for (Point2D pp : points) {
if (p.distanceSquaredTo(pp) < minDis) {
minDis = p.distanceSquaredTo(pp);
near = pp;
}
}
return near;
}
// unit testing of the methods (optional)
public static void main(String[] args) {
PointSET ps = new PointSET();
ps.insert(new Point2D(1, 2));
ps.insert(new Point2D(3, 4));
ps.insert(new Point2D(7, 8));
System.out.println("size " + ps.size());
RectHV rec = new RectHV(0, 0, 5, 5);
Sys