Using recursion you could do something like this psuedocode
- Start at root
- If is a leaf - Print node name and return
- Print --- node name ----
- If node has children - recurse with each child (starting at 2)
- Print +++ Node name ++++
Edit My Version of a recursive method
public static void print(DefaultMutableTreeNode aNode)
{
String name = aNode.toString();
int level= aNode.getLevel();
String placement = "";
while (level > 0)
{
placement += ">";
level--;
}
if(aNode.isLeaf())
{
System.out.println(placement + name);
return;
}
System.out.println(placement + "--- " + name + " ---");
for(int i = 0 ; i < aNode.getChildCount() ; i++)
{
print((DefaultMutableTreeNode)aNode.getChildAt(i));
}
System.out.println(placement + "+++ " + name + " +++");
}
This will give you > for the levels also for example my output was:
--- A ---
>--- B ---
>>C
>>--- D ---
>>>E
>>+++ D +++
>+++ B +++
>F
>G
>H
+++ A +++