Return value in recursive JAVA function

I have developed a function that calls itself recursively. But the return statement does not do what I want it to do. We have checked with printing that a return has been achieved, but it does not return to the original function. In the statement, it includes:

if(depth==0 && pb.isGoalState()){
            System.out.println("!!!!!WOOOOOW!!!!!");
            return pb;
}

      

println displays fine, but when pb returns, things get weird.

When it returns to the function:

result = DLS(pb,depth); //never returns here!!!
System.out.println("Here: "+result.toString());

      

it never prints the above print. I don't understand what happened! I have tested other methods that I developed myself.

private puzzleBoard IDS(String initial){
        puzzleBoard pb = new puzzleBoard(initial,0,new Vector<Integer>(),new Vector<puzzleBoard>(),new Vector<puzzleBoard>());
        int depth=0;
        puzzleBoard result=new puzzleBoard("999999999",0,new Vector<Integer>(),new Vector<puzzleBoard>(),new Vector<puzzleBoard>());
        while(true){//Repeat
            System.out.println("DP "+depth);
            result = DLS(pb,depth);
            System.out.println("Here: "+result.toString());
            if(result.isGoalState())
                return result;
            depth++;
        }

        }

    private puzzleBoard DLS(puzzleBoard pb, int depth){
        System.out.println("AVskilj depth "+depth+" "+(depth==0 && pb.isGoalState()));
        pb.printPuzzle();
        if(depth==0 && pb.isGoalState()){
            System.out.println("!!!!!WOOOOOW!!!!!");
            return pb;
        }
        else if(depth>0){
            for(Iterator<puzzleBoard> child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
                puzzleBoard tmp;
                tmp=child.next();
                tmp.printPuzzle();
                DLS(tmp,(depth-1));
            }

        }
        else
            return new puzzleBoard("999999999",0,new Vector<Integer>(),new Vector<puzzleBoard>(),new Vector<puzzleBoard>());
        return pb;
        }

      


So my problem is now in this piece of code

for(Iterator<puzzleBoard> child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
                DLS(child.next(),(depth-1));
            }

      

When I do not use return to DLS (child.next (), (depth-1)); it goes through each child as intended, but does not retain value due to no return. When I use return in front of it, it just loops through the first child in the iterator and ignores the rest because return statements end up for loops.

How to solve this? I can't think of anything else either.

+3


source to share


1 answer


At this iteration:

   for(Iterator<puzzleBoard> child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
                puzzleBoard tmp;
                tmp=child.next();
                tmp.printPuzzle();
                DLS(tmp,(depth-1));
            }

      

Look at the line:

DLS(tmp,(depth-1));

      



DLS returns an object puzzleBoard

, but you are not using the object returned from that string, so the returned recursive object will be ignored. I have not confirmed to fix your method, but you should start here. and BTW, if the number of baby boards is large, this function can take a long time when you call it for each child.

EDIT: This is an example of how you can handle returned boards from your DLS:

 else if(depth>0){
       for(Iterator<puzzleBoard> child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
                    puzzleBoard tmp;
                    tmp=child.next();
                    tmp.printPuzzle();
                    puzzleBoard resultPB = DLS(tmp,(depth-1));

                    // mergre resultPB with current puzzle board (e.g. pb.addChild(resultPB));
                }

       return pb;
}

      

+3


source







All Articles