CSE 116/504 F16 Midterm #2-Page 4 3) [46 points] Cartography (the drawing of map
ID: 3825391 • Letter: C
Question
CSE 116/504 F16 Midterm #2-Page 4 3) [46 points] Cartography (the drawing of maps) becoming increasingly digital. Digital cartography is especially advantageous by letting users select the size and color of (circles showing the area effected event). This ability, in turn, is enabled by data structures which not only order data, but can enable efficient searching-binary search trees. On this question, you will answering questions about using binary search trees to MapLoc instances, where Mapuoc is a class used to represent a single location on a map. is: public class MapLoc t private double x Location on the map's horizontal axis private doubly y; Location on the map's vertical axis public Map Loc (double nx double nY) f x n x y nY: public double distance (MapLoc source) return Math. sqrt (Math pow (x source x, 2) Math pow (y source y, 2))
Explanation / Answer
A ) Completing DistComp class:
excutable code:
public class DistComp implements Comparator<MapLoc>
{
private MapLoc origin;
public DistComp(MapLoc e1)
{
origin=e1;
}
public int compare(MapLoc m1, MapLoc m2)
{
double d1=m1.distance(origin);
double d2=m2.distance(origin);
if(d1<d2)
return -1;
else if(d1>d2)
return 1;
else
return 0;
}
}
B) Method inRad():
public class BST extends AbstractSet<MapLoc>
{
private BTNode root;
private Comparator<MapLoc> comp;
private MapLoc origin;
private class BTNode
{
protected MapLoc element;
protected BTNode parent, left, right;
public boolean inRad(double dist)
{
double d=element.distance(origin);
if(d<dist)
return true;
return false;
}