I'm trying to implement an algorithm to generate BSP trees within my application. The problem I'm having is that I need to loop through all of the "children" of each "parent", and split them and add those children to a list, and continue to iterate through the children.
I'm lost on how to do this with using concurrent modification.
public void generate() {
Parent root = new Parent(0, 0, width, height);
parents.add(root);
boolean did_split = true;
while (did_split) {
did_split = false;
for (Parent p : parents) {
if (p.leftChild == null && p.rightChild == null) {
if (p.width > MAX_SIZE || p.height > MAX_SIZE || Math.random() > 0.25) {
if (p.split()) {
parents.add(p.leftChild);
parents.add(p.rightChild);
did_split = true;
}
}
}
}
}
}
parents is an ArrayList that is defined earlier within the class.