0

I understand the concept but I have no idea how to write the code out in java. From my understanding you travel down to check if the next node is a leaf, if not you keep going down, then you go back up and do it for the others. But how do I code this?

Here's my constructor for the quadtree

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;
}

Essentially I need to implement a whole bunch of methods for an assignment, like count pixels of certain colour, etc but I have no idea on how to get started

1 Answers1

0

Let's simplify. How would you write a traversal on a binary tree?

Well, you'd write a function like

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;
}

So you want to implement this with your fixed internal values northEast, northWest, southEast, southWest instead of 'left' and 'right'. Notice that the +1 in the node value accounts for the node itself, making an explicit 'leaf check' unnecessary.

It should be mentioned that you need to be aware of stack size limits in your environment. Foxpro used to have a limited stack height of 32 calls, and recursive calls like this can get deep with very large subject materials.

Sean Munson
  • 343
  • 2
  • 9
  • Also - be aware that you probably want to use some kind of generic function into which you can inject a lambda function to improve clarity. – Sean Munson May 17 '18 at 15:53