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

}

}

[1400 byte] By [caracker_ayea] at [2007-9-29 7:06:46]
# 1
"To iterate is human, to recurse is divine" :-)
petitduca at 2007-7-14 21:04:09 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
private hell(Hair hair){ if(!hair.left())return; hell(hair.getAnother().pull())}
caracker_ayea at 2007-7-14 21:04:09 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
Incidently(or prehaps not) its the odd children are deleted
caracker_ayea at 2007-7-14 21:04:09 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4
What looks strange in your code is the fact that you mix recusrsion and iteration : you have a while loop. It is a possible, but...
petitduca at 2007-7-14 21:04:09 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5
<jsp:Stupid question>Why don't you simply call removeAllChildren on the argument, if that's what you want to do ?</jsp:Stupid question>
petitduca at 2007-7-14 21:04:09 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6
I mean the built-in method removeAllChildren defined in the API for this class, not yours.
petitduca at 2007-7-14 21:04:09 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 7

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!

caracker_ayea at 2007-7-14 21:04:09 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 8

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!

caracker_ayea at 2007-7-14 21:04:09 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 9
it's not like I provided much help but thanks :)
petitduca at 2007-7-14 21:04:09 > top of Java-index,Archived Forums,New To Java Technology Archive...