Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to print a tree level with tabs

881282Dec 5 2011 — edited Dec 5 2011
Hi all,

I'd like to print the tree:
A-
  |- B
  |- C
D-
  |- E
     |- F
  |- G
like:
A
\tB
\tC
D
\tE
\t\tF
\tG
The problem is that every time I call the recursion it does not print an extra tab to the lower level.

here is my code:
private void printTree(MyTreeNode node) {
    System.out.println(node.getName());
    if(node.getChildren() != null && node.getChildren().length != 0){
      for(MyTreeNode n : node.getChildren()){
        System.out.print("\t");
        printTree(n);
      }
    }
  }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 2 2012
Added on Dec 5 2011
1 comment
318 views