上接第一篇四叉数之Box索引Box,以下源码如题。
这种情况貌似,再Node节点中在存一份Box更好。索引效率应该更高。
// box 索引 点的情况
public class QuadTreePoint<T> {
private Node<T> root;
public static class Node<T> {
public double x, y;
public Node<T> NW, NE, SE, SW;
public T s;
public Node(double x, double y, T s) {
this.x = x;
this.y = y;
this.s = s;
}
}
public static class Box {
private double xLow, xHigh, yLow, yHigh;
public Box(double xLow, double xHigh, double yLow, double yHigh) {
this.xLow = xLow;
this.xHigh = xHigh;
this.yLow = yLow;
this.yHigh = yHigh;
}
public boolean contain(double x, double y) {
return (x <= xHigh && x >= xLow && y <= yHigh && y >= yLow);
}
}
public Node<T> insert(Node<T> node, double x, double y, T s) {
if (node == null) {
return new Node<T>(x, y, s);
}
if (x < node.x && y < node.y) {
node.NW = insert(node.NW, x, y, s);
return node;
}
if (x < node.x && !(y < node.y)) {
node.SW = insert(node.SW, x, y, s);
return node;
}
if (!(x < node.x) && y < node.y) {
node.NE = insert(node.NE, x, y, s);
return node;
}
if (!(x < node.x) && !(y < node.y)) {
node.SE = insert(node.SE, x, y, s);
}
return null;
}
public List<Node<T>> query(Box box) {
List<Node<T>> result = Lists.newArrayList();
query(root, box, result);
return result;
}
private void query(Node<T> node, Box box, List<Node<T>> result) {
if (node == null) {
return;
}
if (box.contains(h.x, h.y))
result.add(h);
if ( (box.xlow < h.x) && (box.ylow < h.y))
query(h.SW, box, result);
if ( (box.xlow < h.x) && !(box.yhigh < h.y))
query(h.NW, box, result);
if (!(box.xhigh < h.x) && (box.ylow < h.y))
query(h.SE, box, result);
if (!(box.xhigh < h.x) && !(box.yhigh < h.y))
query(h.NE, box, result);
}
}