【问题标题】:How do I write a recursive Quadtree traversal method in java?如何在java中编写递归四叉树遍历方法?
【发布时间】:2018-05-17 15:40:34
【问题描述】:

我理解这个概念,但我不知道如何用 java 编写代码。根据我的理解,您向下移动以检查下一个节点是否是叶子,如果不是,您继续向下,然后您返回并为其他节点执行此操作。但是我该如何编码呢?

这是我的四叉树构造函数

public class QuadtreeBitmap {
// location
private final int x;
private final int y;
// height and width
private final int size;
// if leaf
private boolean leaf;
// either Colour.BLACK or Colour.WHITE
private Colour colour;
// otherwise
private QuadtreeBitmap northWest;
private QuadtreeBitmap northEast;
private QuadtreeBitmap southWest;
private QuadtreeBitmap southEast;

/**
 * Constructs a new quadtree bitmap with height and width equal to the specified size, and 
 * every pixel initialized to the given colour. The specified size must be a power of 2, 
 * and must be greater than zero.
 * 
 * @param size the height and width of this quadtree bitmap
 * @param colour the colour with which to initialize every pixel in this quadtree bitmap
 */
public QuadtreeBitmap(int size, Colour colour) {
    this(0, 0, size, colour);
}
/**
 * Constructs a new quadtree bitmap with height and width equal to the specified size, and 
 * every pixel initialized to white. The specified size must be a power of 2, and must be 
 * greater than zero.
 * 
 * @param size the height and width of this quadtree bitmap
 */
public QuadtreeBitmap(int size) {
    this(0, 0, size, Colour.WHITE);
}

// specifying location only supported internally
private QuadtreeBitmap(int x, int y, int size, Colour colour) {
    // only supporting power-of-2 dimensions
    if (!powerOfTwo(size)) {
        throw new IllegalArgumentException("Size not power of 2.");
    }
    this.x = x;
    this.y = y;
    this.size = size;
    this.leaf = true;
    this.colour = colour;
    this.northWest = null;
    this.northEast = null;
    this.southWest = null;
    this.southEast = null;
}
// combining quads to form tree only supported internally, assumes well-positioned
private QuadtreeBitmap(int x, int y, int size, List<QuadtreeBitmap> quads) {
    this(x, y, size, Colour.WHITE);
    northWest = quads.get(0);
    northEast = quads.get(1);
    southWest = quads.get(2);
    southEast = quads.get(3);
    this.leaf = false;
}

// for any basic task which needs to be repeated all four quadrants
private List<QuadtreeBitmap> quadrants() {
    return Arrays.asList(northWest, northEast, southWest, southEast);
}

// retrieves the quadrant within which the specified location lies
private QuadtreeBitmap quadrantOf(int x, int y) {
    for (QuadtreeBitmap quad : quadrants()) {
        if (quad.containsPoint(x, y)) {
            return quad;
        }
    }
    return null;
}


public int getSize() {
    return size;
}

基本上我需要为分配实现一大堆方法,比如计算某种颜色的像素等,但我不知道如何开始

【问题讨论】:

    标签: java data-structures quadtree


    【解决方案1】:

    让我们简化一下。您将如何在二叉树上编写遍历?

    好吧,你会写一个像

    这样的函数
    public int CountNodes(){
        int leftcount = (this.left==null) ? 0 : this.left.CountNodes();
        int rightcount = (this.right==null) ? 0 : this.right.CountNodes();
       return 1 + leftcount + rightcount;
    }
    

    因此,您希望使用固定的内部值 northEast、northWest、southEast、southWest 而不是“left”和“right”来实现这一点。请注意,节点值中的 +1 说明了节点本身,因此不需要显式的“叶子检查”。

    应该提到的是,您需要了解环境中的堆栈大小限制。 Foxpro 过去的堆栈高度有限,只有 32 次调用,像这样的递归调用可以深入处理非常大的主题材料。

    【讨论】:

    • 另外 - 请注意,您可能想要使用某种通用函数,您可以在其中注入 lambda 函数以提高清晰度。
    猜你喜欢
    • 2021-07-26
    • 2015-05-09
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    相关资源
    最近更新 更多