Breaking a recursive and iterative method

Viewed 75

Can anyone help me breaking this method and return the actual value of "stop" once "stop" is true? Notice that "stop" starting with false.

private boolean handleRoot(ITreeNode node, String rmiDefUuidInit, int columnIndex, String chartName, boolean stop) {
for (ITreeNode childNode : node.getChilds()){
    if (!stop && !childNode.isSelected())
        stop = true;
    handleRoot(childNode, rmiDefUuidInit, columnIndex, chartName, stop);
}
return !stop;}

It seems like, my post isn't clear enough. First I put this example together so it can be simple and easy to understand. My code is much complex than that.

Here is the problem: I have a TreeViewer in which I want to select the parent node if all his children and the children's children are all selected. For performance reason I figured, I can stop as soon as I cross a none selected child-node and break the routine. This is basically my problem since the iteration continues with the value of "stop = false" as soon as my actual iteration ends.

1 Answers

As much as I understand from your description you probably wanted to do this:

private boolean handleRoot(ITreeNode node, String rmiDefUuidInit, int columnIndex, String chartName) {
    for (ITreeNode childNode: node.getChilds()) {
        if (!childNode.isSelected()) return true;
        if (handleRoot(childNode, rmiDefUuidInit, columnIndex, chartName)) return true;
    }
    return false;
}

This code will return true if the tree contains any non-selected TreeNode, and false otherwise. When it finds a non-selected TreeNode it will skip checking other nodes and step out of recursion.

Related