Reputation: 16050
I am experimening with QuadTree class and QuadTreeNode class. My question is the following. Once I putted elements into the QuadTree, is there any way to extract these elements according to their geographical locations, i.e. NORTHWEST, NORTHEAST, SOUTHWEST and SOUTHEAST, without defining the bounding box?
This is what I did so far. In the QuadTree class I introduced the function getChildren
:
public Vector<E> getChildren(int loc)
{
return top.getChildren(loc);
}
And in the class QuadTreeNode I introduced this:
public Vector<E> getChildren(int loc)
{
if (loc == 0)
return _children[NORTHWEST].getItems();
else if (loc == 1)
return _children[NORTHEAST].getItems();
else if (loc == 2)
return _children[SOUTHEAST].getItems();
else
return _children[SOUTHWEST].getItems();
}
Then I created a QuadTree and tried to obtain elements according to their geographical location.
_Qtree = new ITSQtree<Obj>();
for(Obj o : Objs )
_Qtree.put(o);
List<Obj> childrenNORTHWEST = _Qtree.getChildren(0);
List<Obj> childrenNORTHEAST = _Qtree.getChildren(1);
List<Obj> childrenSOUTHEAST = _Qtree.getChildren(2);
List<Obj> childrenSOUTWEST = _Qtree.getChildren(3);
The problem is the the result is always an empty set []
.
Upvotes: 0
Views: 247
Reputation: 8809
You're not adding the children to the output in your recursive step. So you're only returning the very bottom node, which is probably empty. Also how is getItems()
defined?
Here's an tested fix for the node class:
public Vector<E> getChildren(int loc) {
Vector<E> list = new Vector<E>();
getChildren(loc, list);
return list;
}
private Vector<E> getChildren(int loc, Vector<E> list) {
list.addAll(_items);
return _children[loc].getChildren(loc, list);
}
Also, you may want to make the class generic, ie public class QuadTree<T>
and public class QuadTreeNode<T>
Upvotes: 1