Assignment 5 | KdTree | Princeton Algorithms

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值