如何在java中编写递归四叉树遍历方法?

如何在java中编写递归四叉树遍历方法?,java,data-structures,quadtree,Java,Data Structures,Quadtree,我理解这个概念,但不知道如何用java编写代码。据我所知,你向下移动以检查下一个节点是否是叶子,如果不是,则继续向下移动,然后再向上移动并为其他节点执行此操作。但是我该如何编码呢 这是我的四叉树构造函数 public class QuadtreeBitmap { // location private final int x; private final int y; // height and width private final int size; // if leaf private b

我理解这个概念,但不知道如何用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;
}
公共类四叉树映射{
//位置
私人最终int x;
私人终审法院;
//高度和宽度
私人最终整数大小;
//如果叶
私有布尔叶;
//颜色。黑色或颜色。白色
私人色彩;
//否则
西北私人四合院地图;
私人四合院地图东北部;
西南私人四合院地图;
东南部的私有四叉树地图;
/**
*构造高度和宽度等于指定大小的新四叉树位图,然后
*每个像素初始化为给定颜色。指定的大小必须是2的幂,
*并且必须大于零。
* 
*@param size此四叉树位图的高度和宽度
*@param color用于初始化此四叉树位图中每个像素的颜色
*/
公共四合院地图(内部大小、颜色){
这(0,0,大小,颜色);
}
/**
*构造高度和宽度等于指定大小的新四叉树位图,然后
*每个像素初始化为白色。指定的大小必须是2的幂,并且必须为
*大于零。
* 
*@param size此四叉树位图的高度和宽度
*/
公共四叉树映射(整数大小){
这(0,0,大小,颜色,白色);
}
//仅在内部支持指定位置
专用四叉树位图(整数x、整数y、整数大小、颜色){
//仅支持2维幂次方
如果(!powerOfTwo(尺寸)){
抛出新的IllegalArgumentException(“大小不是2的幂”);
}
这个.x=x;
这个。y=y;
这个。大小=大小;
this.leaf=true;
这个颜色=颜色;
this.northWest=null;
this.northEast=null;
this.soutwest=null;
this.souther=null;
}
//组合四元组以形成仅在内部支持的树,假设位置正确
私有四叉树位图(整数x、整数y、整数大小、列表四叉树){
这(x、y、尺寸、颜色、白色);
西北=四边形。获取(0);
东北=四边形。获取(1);
西南=四边形。获取(2);
东南=四边形。获取(3);
this.leaf=false;
}
//对于任何需要在四个象限中重复的基本任务
私有列表象限(){
返回数组.asList(西北、东北、西南、东南);
}
//检索指定位置所在的象限
私有象限映射象限(整数x,整数y){
对于(QuadtreeBitmap quad:quadrants()){
if(四边形包含点(x,y)){
返回四元组;
}
}
返回null;
}
公共int getSize(){
返回大小;
}

基本上,我需要为赋值实现一整套方法,比如计算特定颜色的像素,等等,但我不知道如何开始。让我们简化一下。您如何在二叉树上编写遍历

你可以写一个函数,比如

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;
}
因此,您希望使用固定的内部值northWest、northWest、southWest、southWest来实现这一点,而不是“left”和“right”。请注意,节点值中的+1表示节点本身,因此不需要显式的“叶检查”


值得一提的是,您需要了解环境中的堆栈大小限制。Foxpro过去的堆栈高度限制为32次调用,类似这样的递归调用可以深入到非常大的主题材料中。

另外,请注意,您可能希望使用某种通用函数,可以在其中插入lambda函数n以提高清晰度。