Question: Hello. Please I need help, its in JAVA. I just have to implement a K-D tree ADT. You may implement it as simple as you
Hello. Please I need help, its in JAVA. I just have to implement a K-D tree ADT. You may implement it as simple as you like, what I dont understand is the the instruction where I have to use the NDPoint280 class. My code is not working and I cant start again becaus eof time. Please help. The NDPoint280 class is below. Thanks This is the excat question incase you get confused.
package lib280.base;
import lib280.exception.InvalidArgument280Exception;
public class NDPoint280 implements Comparable
protected Double coords[];
protected int dim;
/**
* Create a new N-dimensional point at the origin (0,0,..., 0).
*
* @param dim Dimensionality of the point.
*/
public NDPoint280(int dim) {
if( dim
this.dim = dim;
coords = new Double[dim];
}
/**
* Create a point from a coordinate in an array of Double. The dimensionality of
* the point is equal to the length of pt.
*
* @param pt Coordinates of the new point.
*/
public NDPoint280(Double pt[]) {
this.dim = pt.length;
if( this.dim
coords = new Double[this.dim];
this.copyArray(pt);
}
/**
* Create a point from a coordinate in array of double. The dimensionality of
* the point is equal to the length of pt.
*
* @param pt Coordinates of the new point.
*/
public NDPoint280(double pt[]) {
this.dim = pt.length;
if( this.dim
coords = new Double[this.dim];
this.copyArray(pt);
}
/**
* Use the coordinates in the given array for this point.
* @param pt New coordinates for this point.
* @precond pt must be equal to this.dim()
* @throws InvalidArgument280Exception if the length of pt is not equal to the the dimensionality of this N-dimensional point.
*/
protected void copyArray(Double pt[]) {
if( this.dim() != pt.length ) throw new InvalidArgument280Exception("Array length must equal point dimensionality ("+this.dim+")");
for(int i=0; i
coords[i] = pt[i];
}
}
/**
* Use the coordinates in the given array for this point.
* @param pt New coordinates for this point.
* @precond pt must be equal to this.dim()
* @throws InvalidArgument280Exception if the length of pt is not equal to the the dimensionality of this N-dimensional point.
*/
protected void copyArray(double pt[]) {
if( this.dim() != pt.length ) throw new InvalidArgument280Exception("Array length must equal point dimensionality ("+this.dim+")");
for(int i=0; i
coords[i] = pt[i];
}
}
/**
* Get the dimensionality of the point.
* @return The dimensionality of the point.
*/
public int dim() { return this.dim; }
/**
* Set the coordinates of this point.
*
* @param pt The point to store represented as an array of double.
* @precond Length of pt must be at least 1 and be equal to this.dim()
*/
public void setPoint(Double pt[]) {
if( this.dim != pt.length) {
this.dim = pt.length;
if( this.dim
coords = new Double[this.dim];
}
this.copyArray(pt);
}
/**
* Obtain the i-th coordinate of the point.
* @param i Desired coordinate of the point.
* @precond i is less than the point's dimensionality
* @return The value of the i-th coordinate of the point.
* @throws InvalidArgument280Exception if i is not a valid dimension index for this n-diensional point (i.e. if i >= n)
*/
public double idx(int i) {
if( i >= this.dim ) throw new InvalidArgument280Exception("Requested coordinate index exceeds point dimensionality.");
return coords[i];
}
@Override
public String toString() {
String out = "(" + coords[0];
for(int i=1; i
out += ", " + coords[i];
}
out += ")";
return out;
}
/**
* Compares the i-th coordinate of two NDPoint objects.
* @param i Index of the coordinate to compare.
* @param other The point to which to compare this point.
* @precond i is less than this point's dimensionality
* @precond this.dim() must equal other.dim()
* @return -1 if the i-th coordinate of this point is smaller than that of 'other';
* 0 if the i-th coordinate of this point and 'other' are equal; or
* 1 if the i-th coordinate of this point is greater than that of 'other'.
*/
public int compareByDim(int i, NDPoint280 other) {
if( other.dim() != this.dim ) {
throw new IllegalArgumentException("NDPoint: comparing two points of different dimension");
}
if( i >= this.dim ) {
throw new IllegalArgumentException("NDPoint: comparing dimension: " + i + ", but point only has dimension " + this.dim);
}
return coords[i].compareTo(other.coords[i]);
}
/**
* Compares this point to 'o' based on the first non-equal dimension (i.e. lexographic ordering).
* @precond this.dim() must equal o.dim()
* @param o The point to which this point is to be compared.
*/
public int compareTo(NDPoint280 o) {
if( this.dim != o.dim )
throw new IllegalArgumentException("NDPoint: comparing two points of different dimension");
for(int i=0; i
if( this.coords[i].compareTo(o.coords[i]) != 0 ) {
return this.coords[i].compareTo(o.coords[i]);
}
}
return 0;
}
public static void main(String args[]) {
// test first constructor, test dim()
NDPoint280 p = new NDPoint280(5);
if( p.dim() != 5) System.out.println("Newly created point should have dimension 5, but has dimension " + p.dim());
// test second constructor, test dim()
Double pt3d[] = {1.0, 2.0, 3.0};
p = new NDPoint280(pt3d);
if( p.dim() != 3) System.out.println("Newly created point should have dimension 3, but has dimension " + p.dim());
if( p.idx(0) != 1.0 || p.idx(1) != 2.0 || p.idx(2) != 3.0 )
System.out.println("Point should be (1.0, 2.0, 3.0) but it is: " + p);
// test setPoint() and idx()
Double newpt3d[] = {3.0, 2.0, 1.0};
p.setPoint(newpt3d);
if( p.idx(0) != 3.0 || p.idx(1) != 2.0 || p.idx(2) != 1.0 )
System.out.println("Point should be (3.0, 2.0, 1.0) but it is: " + p);
// test toString()
String ptAsString = p.toString();
if( ptAsString.compareTo("(3.0, 2.0, 1.0)") != 0 )
System.out.println("Sting representation of point should be \"(3.0, 2.0, 1.0)\" but it is: \"" + ptAsString + "\"");
// test compareTo() when the first coordinate is different
NDPoint280 q = new NDPoint280(pt3d);
if( p.compareTo(p) != 0 )
System.out.println("The point is not equal to itself!");
if( p.compareTo(q) != 1 )
System.out.println("Point p should be greater than point q, but it isn't.");
if( q.compareTo(p) != -1 )
System.out.println("Point q should be less than point p, but it isn't.");
// test compareTo() when the third coordinate is different
Double anotherPoint[] = {1.0, 2.0, 4.0};
p.setPoint(anotherPoint);
if( p.compareTo(q) != 1 )
System.out.println("Point p should be greater than point q, but it isn't.");
if( q.compareTo(p) != -1 )
System.out.println("Point q should be less than point p, but it isn't.");
// test compareByDim()
if( p.compareByDim(1, q) != 0 )
System.out.println("Coordinate 0 of p and q should be equal but they are reportedly not.");
if( p.compareByDim(1, q) != 0 )
System.out.println("Coordinate 1 of p and q should be equal but they are reportedly not.");
if( p.compareByDim(2, q) != 1 )
System.out.println("Coordinate 2 of p should be larger than coordinate 2 of q, but it is reportedly not.");
System.out.println("Unit test complete.");
}
}
#exception
/* InvalidArgument280Exception.java
* ---------------------------------------------
* Copyright (c) 2004 University of Saskatchewan
* All Rights Reserved
* --------------------------------------------- */
package lib280.exception;
/** This exception is used when a method takes in an invalid argument. */
public class InvalidArgument280Exception extends Exception280
{
/** Create an exception with the specified message.
Analysis: Time = O(1) */
public InvalidArgument280Exception(String message)
{
super(message);
}
/** Create an exception with the default message.
Analysis: Time = O(1) */
public InvalidArgument280Exception()
{
super("InvalidArgument280Exception thrown!");
}
}
Implementing the k-D Tree - What You Must Do Implement a k-D tree. You must use the NDPoint280 class provided in the lib280.base package of lib280-asn6 to represent your k-dimensional points. You must design and implement both a node class (KDNode280.java) and a tree class (KDTree280.java). Other than specific instructions given in this question, the design of these classes is up to you. You may use as much or as little of lib280 as you think is appropriate. You'll be graded in the actual appropriateness of your choices here. You should aim to make the class fit into lib280 and its hierarchy of data structures, but you should not force things by extending classes inappropriately. You may use whatever private/protected methods you deem necessary A portion of the marks for this question will be awarded for the design/modularity/style of the im- plementation of your class. A portion of the marks for this question will be awarded for acceptable inline and javadoc commenting. Your ADT must support the following operations: . Construct a new (balanced) k-D tree from a set of k-dimensional points (it must work for any k > 0) . Perform a range search: given a pair of points (a1,a2,. ..ak) and (bi,b2,..,bk), ai
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
