题注
最近实在是太忙了,主要是又进入了论文周期,技术方案刚刚确定,估计后面的3周要一直写论文… 本来打算这一阵子不更新技术博客了,没想到优快云的朋友们有的还等着我写Week 5的Assignment呢!我这人就怕让别人等… 于是就花了整个一中午的时间把这个东西写完了。为什么这么快呢?第一个原因是以前Stanford在Coursera中也开过一门算法课,叫做《Algorithm: Design and Analysis I》,那里面也用到了KdTree,原始的代码我还留着。第二个原因是,KdTree搜索最近Point2D是一个很模块化的东西,网上实际上是有现成的代码的,甚至国外的友人们也有用Princeton的Stdlib做的。所以,Week5的Assignment我基本上结合了上面的两套代码,组合成了自己的代码,其主体框架用的是国外友人的,因为代码结构更清晰~
国外友人针对这一问题答案的链接为:https://gist.github.com/agjacome/5246693。有兴趣的朋友们可以看一下,不过其没有太多的分析。也难怪啦,Assignment题目中对于算法的描述已经非常清楚了,结合课程本身里面KdTree章节的讲解,相信大家只要认真写,是肯定可以写出来的。
题目
Write a data typeto represent a set of points in the unit square (all points have x- and y-coordinates between 0 and 1)using a 2d-tree to supportefficient range search (find all of the points containedin a query rectangle) and nearest neighbor search (find aclosest point to a query point).2d-trees have numerous applications, ranging from classifying astronomical objectsto computer animation to speeding up neural networks to mining data to image retrieval.

Geometric primitives.To get started, use the following geometric primitives for points andaxis-aligned rectangles in the plane.

Use the immutable data type Point2D.java (part of algs4.jar) for points in the plane.Here is the subset of its API that you may use:
Use the immutable data type RectHV.java(not part of algs4.jar)for axis-aligned rectanges.Here is the subset of its API that you may use:public class Point2D implements Comparable<Point2D> { public Point2D(double x, double y) // construct the point (x, y) public double x() // x-coordinate public double y() // y-coordinate public double distanceTo(Point2D that) // Euclidean distance between two points public double distanceSquaredTo(Point2D that) // square of Euclidean distance between two points public int compareTo(Point2D that) // for use in an ordered symbol table public boolean equals(Object that) // does this point equal that? public void draw() // draw to standard draw public String toString() // string representation }
Do not modify these data types.public class RectHV { public RectHV(double xmin, double ymin, // construct the rectangle [xmin, xmax] x [ymin, ymax] double xmax, double ymax) // throw a java.lang.IllegalArgumentException if (xmin > xmax) or (ymin > ymax) public double xmin() // minimum x-coordinate of rectangle public double ymin() // minimum y-coordinate of rectangle public double xmax() // maximum x-coordinate of rectangle public double ymax() // maximum y-coordinate of rectangle public boolean contains(Point2D p) // does this rectangle contain the point p (either inside or on boundary)? public boolean intersects(RectHV that) // does this rectangle intersect that rectangle (at one or more points)? public double distanceTo(Point2D p) // Euclidean distance from point p to the closest point in rectangle public double distanceSquaredTo(Point2D p) // square of Euclidean distance from point p to closest point in rectangle public boolean equals(Object that) // does this rectangle equal that? public void draw() // draw to standard draw public String toString() // string representation }
Brute-force implementation.Write a mutable data type PointSET.java that represents a set ofpoints in the unit square. Implement the following API by using ared-black BST (using either SET from algs4.jar or java.util.TreeSet).
Your implementation should support insert() and contains() in timeproportional to the logarithm of the number of points in the set in the worst case; it should support nearest() and range() in time proportional to the number of points in the set.public class PointSET { public PointSET() // construct an empty set of points public boolean isEmpty() // is the set empty? public int size() // number of points in the set public void insert(Point2D p) // add the point p to the set (if it is not already in the set) public boolean contains(Point2D p) // does the set contain the point p? public void draw() // draw all of the points to standard draw public Iterable<Point2D> range(RectHV rect) // all points in the set that are inside the rectangle public Point2D nearest(Point2D p) // a nearest neighbor in the set to p; null if set is empty }
2d-tree implementation.Write a mutable data type KdTree.java that uses a 2d-tree to implement the same API (but replace PointSET with KdTree).A 2d-tree is a generalization of a BST to two-dimensional keys.The idea is to build a BST with points in the nodes,using the x- and y-coordinates of the pointsas keys in strictly alternating sequence.
- Search and insert. The algorithms for search and insert are similar to those forBSTs, but at the root we use the x-coordinate(if the point to be inserted has a smaller x-coordinatethan the point at the root, go left; otherwise go right);then at the next level, we use the y-coordinate(if the point to be inserted has a smaller y-coordinatethan the point in the node, go left; otherwise go right);then at the next level the x-coordinate, and so forth.
![]()
insert (0.7, 0.2) ![]()