0
public List<Person> getDiseaseRouteTo(Person c){

    if(this.contains(c)){
        if(root == c){
            route.add(c);
        }
        else if(root != c){
            route.add(root);
            for(DiseaseTree dt: children){
                if(dt.contains(c)){
                    route.add(dt.root);
                    dt.getDiseaseRouteTo(c);

                }
            }
        }
        return route;
    }
    return null; 
}

The constructor is a tree constructor named DiseaseTree which contain a root node and a Children set. The children set is a DiseaseTree set. I am supposed to find the route of seeking one node. For example, I have an existed tree named ddtt and I want to find the route to find node c in this tree. ddtt.getDiseaseroute(c) will get a list to find node c. like[A, B, C] if tree is like A --B ----C

I used recursion to realize that. But it can't have complete route. I don't know what is going on and totally confused.

3
  • post your complete code Commented Oct 7, 2015 at 18:44
  • Is that code inside the deseaseTree class? What is 'this.contains(c)'? Commented Oct 7, 2015 at 19:26
  • yes I forgot to mention that. It is the statement to see whether c is in this DiseaseTree Commented Oct 7, 2015 at 20:38

1 Answer 1

1

you are adding the root twice to the route list, once on the line "route.add(root);" and the second time when you go over his children "route.add(dt.root);". you don't need to add it twice. Also next time try to debug it with a simple input and see what is going on.

Sign up to request clarification or add additional context in comments.

2 Comments

I also found that. But I should add the root of this tree first and I really don't know where should I put it.
try removing the second "route.add(dt.root);" it should do the trick

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.