美好的一天,我正在实施八叉树。但是,当输入大小大于 2 时,我一直收到 StackOverflowError。我不知道为什么会收到此错误。
八叉树的实例化
octree = new Octree(3, new Point(0,0,0));
其中第一个参数是维度,第二个参数是根
八叉树人口的代码:
public void generateList(int num)
{
for(int i = 0 ; i < num; i++)
for (int j = 0 ; j < num; j++)
for (int z = 0 ; z < num; z++)
if (!(z == j && j == i)) {
octree.add_nodes(i,j,z);
}
}
八叉树的 add_nodes 代码
public boolean add_nodes(double x, double y, double z) {
boolean success = false;
System.out.println(x+","+y+","+z);
if (this.i < 8) {
if (compare(x, y, z)) {
if (root.childPoints == null) {
System.out.println("Root is null" + x + " " + y + " " + z);
}
this.root.childPoints[i] = new Point(x, y, z);
this.i++;
success = true;
}
}
else if (this.childNodes == null) {
this.create_nodes_of_nodes(x, y, z);
for (int j = 0; j < 8; j++) {
double tempx = this.root.x - root.childPoints[j].x;
double tempy = this.root.y - root.childPoints[j].y;
double tempz = this.root.z - root.childPoints[j].z;
int checker = compareValues(tempx, tempy, tempz);
this.childNodes[checker].add_nodes(root.childPoints[j].x,
root.childPoints[j].y, root.childPoints[j].z);
}
root.childPoints = null;
double tempx = this.root.x - x;
double tempy = this.root.y - y;
double tempz = this.root.z - z;
int checker = compareValues(tempx, tempy, tempz);
this.childNodes[checker].add_nodes(x, y, z);
// this.i=0;
}
else {
if (childNodes != null) {
int checker = compareValues(x, y, z);
childNodes[checker].add_nodes(x, y, z);
}
}
return success;
}
错误:
Exception in thread "main" java.lang.StackOverflowError
at sun.misc.FloatingDecimal$BinaryToASCIIBuffer.appendTo(FloatingDecimal.java:307)
at sun.misc.FloatingDecimal.appendTo(FloatingDecimal.java:89)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:709)
at java.lang.StringBuilder.append(StringBuilder.java:226)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:73)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:97)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:107)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:107)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:107)
and an additional 400 lines of at
edu.modesta.test.octree.Octree.add_nodes(Octree.java:107) error
希望有人能回答。谢谢!