I have a generic binary tree. What I want to do is find a node, providing an attribute to find that node an then return it. For example, my tree has Objects type X, X has an attribute code, so I have to compare the code of the current node with the code provided.
private X find_X_by_code(Node<X> node, String code) {
if (!node.element.code().equals(code)){
if (node.left != null){
find_X_by_code(node.left,code);
}
if (node.right != null){
find_X_by_code(node.right,code);
}
}
return node.element;
}
I call this method like this:
find_X_by_code(root,code);
For some reason when the codes match, it keeps going on.