I suppose you want to find a node with a specific string, right? Other answers explain how to do this using an enumeration approach ... (and I'm sure they all know that in the real world you will also need to be able to have more than one node to have the string you want, etc.)
But there are potentially other, sexier ways to do this. For example, if you put all the nodes in a collection some type ( ArrayList , etc. *), since they were inserted into the tree (and deleted them as they were deleted, including explicitly deleting all their descendants). and if you also implemented such things so that two nodes are considered “equal”, if they had the same result from toString (or implemented a Comparator that did this), then you could easily output the actual node (or nodes) to an ArrayList , which match and then go
tree.expandPath( new TreePath( node_found.getPath())
One of the points of the trees is that this is actually the path to the node (sometimes called "breadcrumbs"), which is the real "identity" of any given node. As for the displayed String values, this means that you can have in the same tree:
way: "Peter" - "Piper" - "pickled" - "pepper"
way: "Culinary dishes" - "spices" - "pepper"
way: "My favorites" - "food" - "seasonings" - "pepper"
And so say what you want to search, and then select or select one of these “pepper” nodes ... it’s actually not very effective to have a “bust” submenu for each of the elements along this path (the larger the tree, the worse the problem , of course).
Using my suggestion becomes quite simple: just split your breading path, starting from the root or anywhere, and then when you expand into the tree, use node.isNodeDescendant() in the “highest” node (i.e. those from the root ) that you have already found (here are 3 “pepper” nodes): if you want the first path above, you will first find the node “Peter”, and then immediately after that only the “pepper” node, which can satisfy the isNodeDescendant test, will give all the path you are looking for.
* Of course, some form of hashing will be even more efficient. But this is a consideration only if you have many thousands or more nodes in your tree.