I have a tree that contains multiple nodes. Each node has a parent node (or null in the case of the root), a name (name), and a HashTable (children) that maps the name of the child node to the child node object.
Given the string name of the node, I want to create a method that iterates through the tree to find the specific node and return it. If the node doesn't exist then return null.
I thought a recursive method would be best. So far I have this:
public Node findNode(Node n, String s) {
if (n.name == s) {
return n;
} else {
for (Node child: n.children.values()) {
findNode(child, s);
}
}
}
I'm not sure exactly where to put the null statement.