The Artima Developer Community
Sponsored Link

Java Answers Forum
Printing a n-Nodes Tree

1 reply on 1 page. Most recent reply: Apr 18, 2007 5:44 AM by Dave H

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Sijar ahmed

Posts: 2
Nickname: sahmed
Registered: Apr, 2007

Printing a n-Nodes Tree Posted: Apr 18, 2007 4:34 AM
Reply to this message Reply
Advertisement
I have this problem related to n-nodes Tree,
I have created a n-nodes Tree as following:


class Treenode
{
String data;
Treenode parent;
List<Treenode> children = new ArrayList<Treenode>();

/** further lots of member functions */
}


I'm stuck up in the printing part of the program.
Which is to be done in like this,

A
|---- B
|.....|----F
|.....|----E
|...........|----H
|-----C
|.....|----G
|
|------D

Where it's obvious that A is the root, B,C & D
are its children and F,G & D are leaves.

Can any body get me any logic/method to do this.


Dave H

Posts: 16
Nickname: slebtack
Registered: Apr, 2007

Re: Printing a n-Nodes Tree Posted: Apr 18, 2007 5:44 AM
Reply to this message Reply
Start by writing a recursive method that will print

A
B
F
E
H
C
G
D

Then ask yourself what extra information your method will need to give itself in order to correctly print the structural lines as well.

Flat View: This topic has 1 reply on 1 page
Topic: Printing a n-Nodes Tree Previous Topic   Next Topic Topic: Java exercise..

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use