4
\$\begingroup\$

I'm trying to build this simple tree:

Multiverse
| 
|--- Universe
 |
 |--- Supercluster
 |
 |--- Galaxy
 |
 |--- Andromeda
 |
 |--- Milky way

I came up with this simple method which recursively walk a tree:

/**
 * recursively walk a tree structure
 */ 
public static void recursive(TreeViewItem treeNode){
 System.out.println(treeNode.getText());
 Iterator<TreeViewItem> itr = treeNode.getItems().iterator();
 while (itr.hasNext()) {
 TreeViewItem item = (TreeViewItem) itr.next();
 recursive(item);
 }
}

Is there a better way or more elegant way to do this?

asked Sep 17, 2014 at 3:56
\$\endgroup\$
0

2 Answers 2

10
\$\begingroup\$

You're visiting and printing the tree elements in a depth-first manner, so depthFirstPrint would be a much better name than "recursive" (which is always a terrible name for any recursive function).

You didn't need this cast:

TreeViewItem item = (TreeViewItem) itr.next();

because you correctly declared the iterator as Iterator<TreeViewItem> itr

As of Java 1.5, you can use a for-each loop, which is simple and elegant:

for (TreeViewItem item : treeNode.getItems()) {
 depthFirstPrint(item);
}
answered Sep 17, 2014 at 5:28
\$\endgroup\$
6
\$\begingroup\$

There are three kinds of recursive binary tree traversals: pre-order, in-order, and post-order. You've implemented pre-order traversal, so I suggest naming your function preOrderTraversal().

Your function is actually doing two things: traversing the tree and printing each node. It would be nice to separate them, so that you have a generic tree traversal algorithm, and a separate place to specify what to do with each node as it is encountered. With Java ≤ 7, this can be done using a visitor pattern. In Java 8, the visitor pattern can be much simpler, since the language allows functions to be passed around easily:

import java.util.function.Consumer;
...
public static void preOrderTraversal(TreeViewItem treeNode, Consumer<TreeViewItem> action) {
 action.accept(treeNode);
 for (TreeViewItem child : treeNode.getItems()) {
 preOrderTraversal(child, action);
 }
}
// Call that code with
preOrderTraversal(rootNode, (TreeViewItem node) -> System.out.println(node.getText()));

Your TreeViewItem class could have a public Iterator<TreeViewItem> iterator() method, so that TreeViewItem implements Iterable<TreeViewItem> to iterate of the children directly, without having to call .getItems(). Whether you want to make this change depends on whether there might be anything else in a TreeViewItem that might be worth iterating over, besides the child nodes.

answered Sep 17, 2014 at 6:04
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.