Answered step by step
Verified Expert Solution
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 algskdtree;
import algsPointD;
import algsQueue;
import stdlib.;
a set of points implemented as kdtree
public class KdTree
private static class Node
private PointD p; the point
private RectHV rect; the axisaligned rectangle corresponding to this node
private Node lb; the leftbottom subtree
private Node rt; the righttop subtree
private boolean isXCoord; true if the node splits by xcoordinate, false if by ycoordinate
public NodePointD 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 insertPointD p
root insertroot p true, new RectHV;
private Node insertNode node, PointD p boolean isXCoord, RectHV rect
if node null return new Nodep rect, isXCoord;
if nodepequalsp return node; avoid duplicates
if isXCoord
if px node.px
if nodelb null
RectHV subRect new RectHVrectxmin rect.ymin node.px rect.ymax;
node.lb insertnodelb pisXCoord, subRect;
else
node.lb insertnodelb pisXCoord, node.lbrect;
else
if nodert null
RectHV subRect new RectHVnodepx rect.ymin rect.xmax rect.ymax;
node.rt insertnodert pisXCoord, subRect;
else
node.rt insertnodert pisXCoord, node.rtrect;
else
if py node.py
if nodelb null
RectHV subRect new RectHVrectxmin rect.ymin rect.xmax node.py;
node.lb insertnodelb pisXCoord, subRect;
else
node.lb insertnodelb pisXCoord, node.lbrect;
else
if nodert null
RectHV subRect new RectHVrectxmin node.py rect.xmax rect.ymax;
node.rt insertnodert pisXCoord, subRect;
else
node.rt insertnodert pisXCoord, node.rtrect;
return node;
does the set contain the point p
public boolean containsPointD target
return containsroot target, true;
private boolean containsNode node, PointD p boolean isXCoord
if node null return false;
if nodepequalsp return true;
if isXCoord
if px node.px return containsnodelb pisXCoord;
else return containsnodert pisXCoord;
else
if py node.py return containsnodelb pisXCoord;
else return containsnodert pisXCoord;
all points in the set that are inside the target rectangle
public Iterable rangeRectHV target
TODO
return new Queue;
a nearest neighbor to target point; null if empty
public PointD nearestPointD target
TODO
return new PointD ;
draw all of the points to standard draw
for xnode, use red line to draw the division between leftright
for ynode, use blue line to draw the division between topbottom
see the writeup for examples
public void draw
drawroot true;
private void drawNode node, boolean isXCoord
if node null return;
Draw the point
StdDraw.setPenColorStdDrawBLACK;
StdDraw.setPenRadius;
node.pdraw;
Draw the division line
double xmin, ymin, xmax, ymax;
if isXCoord
StdDraw.setPenColorStdDrawRED;
xmin node.px;
xmax node.px;
ymin node.rect.ymin;
ymax node.rect.ymax;
else
StdDraw.setPenColorStdDrawBLUE;
ymin node.py;
ymax node.py;
xmin node.rect.xmin;
xmax node.rect.xmax;
StdDraw.setPenRadius;
StdDraw.linexmin ymin, xmax, ymax;
Recursively draw children
drawnodelbisXCoord;
drawnodertisXCoord;
some test code
public void toGraphviz GraphvizBuilder.nodesToFile root;
private static void insertPoints KdTree kdtree, double xoffset, double yoffset
kdtree.insert new PointD xoffset, yoffset;
kdtree.insert new PointD xoffset, yoffset;
kdtree.insert new PointD xoffset, yoffset;
kdtree.insert new PointD xoffset, yoffset;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started