Project 2: FEMA needs your help!
Goal: Implement a spatial data structure
Due: 9/28/2009
Suppose FEMA or
some other organization is interested in maintaining a data structure to store
a collection of cities and towns. As
with most collections, insertion of new cities and search for a given city
should be fast operations. In addition,
FEMA has some other operations they wish to be fast. In particular, if there is a hurricane
somewhere in the country, the data structure needs to be able to quickly
produce a list of all cities within, say, a ten mile radius of the
hurricane. This is called a range query
because we are asking for all objects in the collection within a certain
distance of a given location. Second,
because cities often need to be evacuated for whatever reason, FEMA needs to be
able to quickly determine the closest city to a given location. This is called a nearest neighbor query.
Your program
should store a collection of cities.
Each city object should have the following information:
city
name
x
coordinate
y
coordinate
To make testing easy, use the following class:
class city
{
public:
string name;
double x;
double y;
};
For your convenience, you may assume all coordinates
will be from 0 to 100,000.
Your program should support the basic
insert routine:
insert(
double x, double y, string name)
To insert a new city at location x, y.
Further, you must implement efficient
procedures for range queries and nearest neighbor queries:
list
range( double x, double y, double radius )
//this should return the list of all cities within radius miles of point
(x, y)
city
nearestCity( double x, double y )
//this should return the city closest to city point (x, y).
To provide this functionality efficiently,
implement a point-quad tree. Your
solution to this problem will be graded on correctness (do the queries yield
what they are supposed to) and efficiency ( how many cities had to be searched
to compute your range query or find the nearest neighbor ).
For extra credit, generalize the concept of
the AVL binary search tree and apply it to your quadtree so that you maintain
optimal balance for fast operations.
Please contact me early if you have any questions
on this assignment.
To allow for efficient grading, your
implementation MUST work with the following test code (In addition, your code
will be tested with extremely large test cases to measure the efficiency of
your implementation):
void
print_citylist
(std::list<city> * plist, std::string message)
{
//std::cout
<< "" << std::endl;
std::cout << "---------
" << message << "
BEGIN ---------" << std::endl;
for
(std::list<city>::iterator it = plist->begin(); it != plist->end();
it ++) {
std::cout << "City: '" << (*it).name << "'\t(x=" << (*it).x << ", y=" << (*it).y << ")" << std::endl;
}
std::cout << "---------
" << message << " END
---------" << std::endl;
}
int main()
{
pointQuadTree citytree;
citytree.insert ( 100, 100, "Chicago");
citytree.insert ( 25, 125, "McAllen");
citytree.insert ( 50, 150, "Los Angeles");
citytree.insert ( 75, 130, "Detroit");
citytree.insert ( 150, 50, "New
York");
citytree.insert ( 10, 40, "Austin");
city closestCity = citytree.nearestCity
(63, 84);
cout << "The closest city is
" << closestCity.name << endl;
double x = 50;
double y = 95;
double r = 100;
std::list<city> * closeCities =
citytree.in_range (x, y, r);
if (NULL ==
closeCities) {
return;
}
print_citylist (closecities, "city list");
delete
closecities;
return 0;
}