I'm trying to search for a node in a binary tree and return in case it's there, otherwise, return null. By the way, the node class has a method name() that return a string with it's name...What I have so far is:
private Node search(String name, Node node){
if(node != null){
if(node.name().equals(name)){
return node;
}
else{
search(name, node.left);
search(name, node.right);
}
}
return null;
}
Is this correct??
you don't do anything with the result of the recursive calls
you should return something if it is found in node.left or node.right so the else block should be something like that:
Actually, try to avoid recursivity. In case you have big tree structure you will get stack overflow error. Instead of this you can use a list: