StackOverFlowError quadtree insert
<p>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.</p>

<pre><code>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 &lt; parent.Shapes.size(); i++) {
                    this.insert(parent, parent.Shapes.get(i));

                }   
            }

            break;
        case PARENT:
            for (int i = 0; i &lt; 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;
    }
}    
</code></pre>

<p>Split method is this:</p>

<pre><code>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);

}
</code></pre>

<p>GetQuadrantForShape method returns a list of quadrants for the new shape.</p>

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

}
</code></pre>

<p>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.</p>

<p>UPDATE:</p>

<pre><code>boolean needSplit(Node node, GeometricObj shape) {
    boolean result = false;
    for (int i = 0; i &lt; 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 &lt; r.getX() + r.getWidth() &amp;&amp; x + width &gt; r.getX() &amp;&amp; y &lt; r.getY() + r.getHeight() &amp;&amp; y + height &gt; r.getY();
}
</code></pre>