Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In JAVA please complete the TODO sections: package algs 3 2 . kdtree; import algs 1 2 . Point 2 D; import algs 1 3

In JAVA please complete the TODO sections:
package algs32.kdtree;
import algs12.Point2D;
import algs13.Queue;
import stdlib.*;
/* a set of points implemented as kd-tree */
public class KdTree {
private static class Node {
private Point2D p; // the point
private RectHV rect; // the axis-aligned rectangle corresponding to this node
private Node lb; // the left/bottom subtree
private Node rt; // the right/top subtree
private boolean isXCoord; // true if the node splits by x-coordinate, false if by y-coordinate
public Node(Point2D p, RectHV rect, boolean isXCoord){
this.p = p;
this.rect = rect;
this.isXCoord = isXCoord;
}
}
private Node root;
/* construct an empty set of points */
public KdTree(){
root = null;
}
/* is the set empty? */
public boolean isEmpty(){
return root == null;
}
/* add the point p to the set (if it is not already in the set)*/
public void insert(Point2D p){
root = insert(root, p, true, new RectHV(0,0,1,1));
}
private Node insert(Node node, Point2D p, boolean isXCoord, RectHV rect){
if (node == null) return new Node(p, rect, isXCoord);
if (node.p.equals(p)) return node; // avoid duplicates
if (isXCoord){
if (p.x()< node.p.x()){
if (node.lb == null){
RectHV subRect = new RectHV(rect.xmin(), rect.ymin(), node.p.x(), rect.ymax());
node.lb = insert(node.lb, p,!isXCoord, subRect);
} else {
node.lb = insert(node.lb, p,!isXCoord, node.lb.rect);
}
} else {
if (node.rt == null){
RectHV subRect = new RectHV(node.p.x(), rect.ymin(), rect.xmax(), rect.ymax());
node.rt = insert(node.rt, p,!isXCoord, subRect);
} else {
node.rt = insert(node.rt, p,!isXCoord, node.rt.rect);
}
}
} else {
if (p.y()< node.p.y()){
if (node.lb == null){
RectHV subRect = new RectHV(rect.xmin(), rect.ymin(), rect.xmax(), node.p.y());
node.lb = insert(node.lb, p,!isXCoord, subRect);
} else {
node.lb = insert(node.lb, p,!isXCoord, node.lb.rect);
}
} else {
if (node.rt == null){
RectHV subRect = new RectHV(rect.xmin(), node.p.y(), rect.xmax(), rect.ymax());
node.rt = insert(node.rt, p,!isXCoord, subRect);
} else {
node.rt = insert(node.rt, p,!isXCoord, node.rt.rect);
}
}
}
return node;
}
/* does the set contain the point p?*/
public boolean contains(Point2D target){
return contains(root, target, true);
}
private boolean contains(Node node, Point2D p, boolean isXCoord){
if (node == null) return false;
if (node.p.equals(p)) return true;
if (isXCoord){
if (p.x()< node.p.x()) return contains(node.lb, p,!isXCoord);
else return contains(node.rt, p,!isXCoord);
} else {
if (p.y()< node.p.y()) return contains(node.lb, p,!isXCoord);
else return contains(node.rt, p,!isXCoord);
}
}
/* all points in the set that are inside the target rectangle */
public Iterable range(RectHV target){
// TODO
return new Queue<>();
}
/* a nearest neighbor to target point; null if empty */
public Point2D nearest(Point2D target){
// TODO
return new Point2D (0,0);
}
/* draw all of the points to standard draw */
/* for x-node, use red line to draw the division between left/right */
/* for y-node, use blue line to draw the division between top/bottom */
/* see the writeup for examples */
public void draw(){
draw(root, true);
}
private void draw(Node node, boolean isXCoord){
if (node == null) return;
// Draw the point
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.01);
node.p.draw();
// Draw the division line
double xmin, ymin, xmax, ymax;
if (isXCoord){
StdDraw.setPenColor(StdDraw.RED);
xmin = node.p.x();
xmax = node.p.x();
ymin = node.rect.ymin();
ymax = node.rect.ymax();
} else {
StdDraw.setPenColor(StdDraw.BLUE);
ymin = node.p.y();
ymax = node.p.y();
xmin = node.rect.xmin();
xmax = node.rect.xmax();
}
StdDraw.setPenRadius();
StdDraw.line(xmin, ymin, xmax, ymax);
// Recursively draw children
draw(node.lb,!isXCoord);
draw(node.rt,!isXCoord);
}
/* some test code */
public void toGraphviz(){ GraphvizBuilder.nodesToFile (root); }
private static void insert5Points (KdTree kdtree, double xoffset, double yoffset){
kdtree.insert (new Point2D (0.7+ xoffset, 0.2+ yoffset));
kdtree.insert (new Point2D (0.5+ xoffset, 0.4+ yoffset));
kdtree.insert (new Point2D (0.2+ xoffset, 0.3+ yoffset));
kdtree.insert (new Point2D (0.4+ xoffset, 0.7+ yoffset));

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions