Recursion schmursion
Hi all,
I'm trying to write a method which will delete all child items of the argument from a treemodel. I'm getting some weird results (below) and its Sunday, please help! - theres dollars involved.
Where the argument has 3 children 2 deletions are made.
Where the argument has 4 children 2 deletions are made.
Where the argument has 5 children 5 deletions are made.
Where the argument has 6 children 6 deletions are made.
//recursive method to remove all children of seed node from both data models.
privatevoid removeAllChildren(DefaultMutableTreeNode node){
Enumerationenum = node.children();
while(enum.hasMoreElements()){
DefaultMutableTreeNode siblingNode = (DefaultMutableTreeNode)enum.nextElement();
theApp.getLinearModel().remove(siblingNode);
treeModel.removeNodeFromParent(siblingNode);
System.out.println("Called for: "+ count+" delete actions");
count ++;
if(!siblingNode.isLeaf())
removeAllChildren(siblingNode);
}
}
Mixing recursion and iteration itsn't the problem, if I use the method to add the supposedly deleted items to a vector, it is populated correctly by the items I want to delete.
I think that the problem is that when the method returns to itself after a recurive trip down a level, the origonal enumerator is all messed up by the fact a node has been removed at that level.
The reason I'm not using the api removeAllChildren is that I need to know whats being removed - I have a linear representation of the model held in a vector and need to. I think its a good idea to look at the code for it and see how the pro's do it!
Thanx for you help petit,
I'm happy to report that I've been an ass. The simple solution was too walk the tree finding all children and sub-children while the data model was still valid, deleting from the linear data model as we go. Then use the API remove all children to change the tree. et voila ;-)
Thanks again, to give credit where credits due I suppose it was the mixing of iteration and recursion as the enumerators are fail fast.
Spend wisely!