四叉数之Box索引Point

上接第一篇四叉数之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);

    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值