Hey guys,
I have created a tree which is not a binary tree. Now, what I want is to search for an element. The main thing is the following: Since I have no comparison chance in contrast to a binary tree, I have to find some other ways to implement the code. Here what I thought:
public TreeNode<City> search(City parent, TreeNode<City> t){
//As you guess, City class is irrelevant to the issue, I have no problem with City class.
if (t.getCity().equals(parent)) {
return t;
}
else if (t.hasLeftChild()){
search(parent,t.getLeftChild());
}
else if(t.hasNextSibling()){
search(parent,t.getNextSibling());
}
else//Since I know that case will never happen, the returned value is unimportant
return t;
}
Of course, that code did not work. The difficult part is that I have to return the value I am searching for as soon as I find it. Yet, If I cannot find it, I still have to return something. How am I going to do that???
To start, you need to (somehow) use the value returned by the recursive calls to
search()— probablyreturnit: