tags:

views:

250

answers:

1

We have a tree structure implemented using the DefaultMutableTreeNode specified in Java.

Is there any way of traversing it, that is inbuilt?

If not, please suggest other techniques.

+4  A: 

If you mean you want to traverse the tree you can call breadthFirstEnumeration() or depthFirstEnumeration() in order to iterate over all nodes in the tree.

Example:

DefaultMutableTreeNode root = ...

Enumeration en = root.depthFirstEnumeration();
while (en.hasMoreElements()) {

  // Unfortunately the enumeration isn't genericised so we need to downcast
  // when calling nextElement():
  DefaultMutableTreeNode node = (DefaultMutableTreeNode) en.nextElement();
}
Adamski
How do I get access to each node that returned by the search algorithm? Can you point me to a good resource, please.
fixxxer
I've just added some example code.
Adamski