0

I want to implement insert method for a quadtree. In this quadtree i will insert certain shapes like rectangle, triangle, circle and diamond. All shaoes come with an rectangle border(all shaoes are considered rectangles in this method). My method works for few inputs, but for most of all i get the error mentioned.

void insert(Node parent, GeometricObj shape) {
    switch(parent.getType()) {
        case EMPTY:
            this.setShapeForNode(parent, shape);

            break;
        case LEAF:
            if(needSplit(parent, shape)) {
                this.setShapeForNode(parent, shape);
            }

            else {
                this.split(parent);
                this.insert(parent, shape);
                for (int i = 0; i < parent.Shapes.size(); i++) {
                    this.insert(parent, parent.Shapes.get(i));

                }   
            }

            break;
        case PARENT:
            for (int i = 0; i < getQuadrantForShape(parent, shape.getX(), shape.getY(),
                    shape.getWidth(), shape.getHeight()).size(); i++) {
                if(getQuadrantForShape(parent, shape.getX(), shape.getY(),
                    shape.getWidth(), shape.getHeight()).get(i).equals(parent.Quads.get(0))) {
                    this.insert(parent.Quads.get(0), shape);
                }

                if(getQuadrantForShape(parent, shape.getX(), shape.getY(),
                    shape.getWidth(), shape.getHeight()).get(i).equals(parent.Quads.get(1))) {
                    this.insert(parent.Quads.get(1), shape);
                }

                if(getQuadrantForShape(parent, shape.getX(), shape.getY(),
                    shape.getWidth(), shape.getHeight()).get(i).equals(parent.Quads.get(2))) {
                    this.insert(parent.Quads.get(2), shape);
                }

                if(getQuadrantForShape(parent, shape.getX(), shape.getY(),
                    shape.getWidth(), shape.getHeight()).get(i).equals(parent.Quads.get(3))) {
                    this.insert(parent.Quads.get(3), shape);
                }

            }
            break;
    }
}    

Split method is this:

void split(Node node) {

    node.setType(NodeType.PARENT);

    double x = node.getX();
    double y = node.getY();
    double halfwidth = node.getWidth()/ 2;
    double halfheight = node.getHeight()/ 2;

    node.setQuad1(new Node(x + halfwidth, y + halfheight, halfwidth, halfheight, node));
    node.setQuad2(new Node(x, y + halfheight, halfwidth, halfheight, node));
    node.setQuad3(new Node(x, y, halfwidth, halfheight, node));
    node.setQuad4(new Node(x + halfwidth, y, halfwidth, halfheight, node));

    node.Quads.add(node.quad1);
    node.Quads.add(node.quad2);
    node.Quads.add(node.quad3);
    node.Quads.add(node.quad4);

}

GetQuadrantForShape method returns a list of quadrants for the new shape.

void setShapeForNode(Node node, GeometricObj shape) {
   if (node.getType() != NodeType.PARENT) {
        node.setType(NodeType.LEAF);
        node.Shapes.add(shape);
    }

}

If someone has a clue of what the problem could be, or what am I doing wrong, or any hint of what can I improve, please tell me. If you need any other class or method I'm gonna attach it here.

UPDATE:

boolean needSplit(Node node, GeometricObj shape) {
    boolean result = false;
    for (int i = 0; i < node.Shapes.size(); i++) {
        GeometricObj o = node.Shapes.get(i);
        result = this.overlaps(shape.getX(), shape.getY(), shape.getWidth(), shape.getHeight(), o);
        if(result == false) {
            break;
        }
    }
    return result;
}


boolean overlaps (double x, double y, double width, double height, GeometricObj r) {
        return x < r.getX() + r.getWidth() && x + width > r.getX() && y < r.getY() + r.getHeight() && y + height > r.getY();
}
RobertD
  • 25
  • 6
  • The problem is probably an endless recursion (i´d guess it´s due to the `insert` recursion). But it´s not visible with the given code where it actually occurs, as `insert` doesn´t get called anywhere. – SomeJavaGuy Nov 22 '16 at 15:14
  • 'needSplit' test only if a new shape overlapes an old one. 'insert' is called for certain shapes in main that are written from a file. – RobertD Nov 22 '16 at 15:19
  • Java has a very small recursion stack so If we do more recursion call the chance of getting stackoverflow error increases. This problem I've faced many times in competitive coding so there are two ways to get a rid of that. – Sanket Makani Nov 22 '16 at 15:24
  • If i remove the insert call after the split call I get rid of that error but i also lose a lot of values. – RobertD Nov 22 '16 at 15:32
  • Java has a very small recursion stack so If we make more recursion calls the chance of getting stackoverflow error increases. This problem I've faced many times in competitive coding so there are two ways to get a rid of that. **1)** Make you own stack which will work as a recursion stack. Check this [link](http://haacked.com/archive/2007/03/04/Replacing_Recursion_With_a_Stack.aspx/) . **2)** Second approach is just increase your recursion stack size. For this, refer this [link](http://codeforces.com/blog/entry/47003). In this link first comment is the answer how to increase stack size. – Sanket Makani Nov 22 '16 at 15:33
  • 1
    I will try. Thank you! – RobertD Nov 22 '16 at 15:39
  • If you find problem implementing the same..just remind me by mentioning in comment I will try to help you with the code @RobertD – Sanket Makani Nov 22 '16 at 15:45
  • Ok. Though i think Kevin is right. Probably, i have an endless recursion. – RobertD Nov 22 '16 at 15:48
  • I have a list of shapes in node definition so i can store multiple shapes. Probably needSplit is The problem. I will analize it again and i will attach the code here too. Thanks for help. – RobertD Nov 22 '16 at 16:09
  • So do you now split on overlapping or non-overlapping objects? The current version will split on overlapping shapes which as I've written in previous comments (and already deleted because of your updates) will result in endless recursion. – BeyelerStudios Nov 22 '16 at 19:51
  • So what should i do? I really don't understand. In my mind makes sense. I'm really confused. – RobertD Nov 22 '16 at 19:54
  • It's time you start visualising your algorithm, draw the rectangles and shapes (or their bounding rectangles) of the leaf elements. Construct a tree with a maximum depth of `k` and repeat for `k+1` (go on until you see the pattern)... – BeyelerStudios Nov 22 '16 at 19:57

0 Answers0