This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to dispaly Nodes and children ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to dispaly Nodes and children ?" Watch "How to dispaly Nodes and children ?" New topic

How to dispaly Nodes and children ?

majid nakit
Ranch Hand

Joined: Jun 26, 2001
Posts: 164
could you please tell me how to iterate a Node and its children and display them.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 4442


Welcome to JavaRanch! Please modify your profile so that your publicly displayed name conforms to the JavaRanch Naming Policy. Thanks!

Please be a bit more specific in your questions; we're just a bunch of poor old cowboys around here . What kind of Node are you referring to? It might help even more if you post a bit of code.
[ July 16, 2004: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
Well it depends somewhat on how you want to iterate them, inorder, postorder or preorder?

Iterating a single node isn't too hard but are you really thinking of a single node or an entire tree?
[ July 17, 2004: Message edited by: Elouise Kivineva ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The usual technique is recursion. I don't know if that's a new term to you or not, so excuse me if I go too basic on ya.

Say each node has a value and some children. You could make a menu tree this way or an organization chart. To display a node it's pretty simple to print value. Then we want to do exactly the same thing for each child node, so we call the very same method. Recursion is when a method calls itself.

Our first cut of display node is dead simple:

Now we make it call the same method on every child. Call this with the top node in the tree: displayNode(topNode);

Draw an organization chart - one boss, two directors, each has a couple managers, each has two or three worker bees - and follow what happens. Display the boss, the boss's first director, the director's first manager, the manager's first worker, second worker, third worker. Then return to the director and display her second manager. Eventually we visit everybody.

A nice detail might be to indent lines like an outline. For this I like to keep track of the depth in the tree:

Hope that helps!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
majid nakit
Ranch Hand

Joined: Jun 26, 2001
Posts: 164
Thank you James, I am a biginner, I am learning from the way you are developping, it's very nice and clear,
1)now I want to create a class that has nodes, I need a method for building the nodes and thier children, how to do that please ?
2) what is this :
String indent = copies(" ", depth);
where copies method come from ?

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, glad that was helpful.

There are some tree structures in the JDK. I thought you might be using the DefaultTreeNode for Swing tree widgets. If you're just doing that org chart kinda thing this would do for a node:

I left a lot out (setters, getters, validation, etc) but I hope that sholws the basic idea.

D'oh! I almost commented copies(" ", n). It's borrowed from the REXX language definition where it returns "n" copies of any input string. I use it to draw lines in logs: log(copies("-", 80)); or to indent.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: How to dispaly Nodes and children ?
Similar Threads
panels not displayed fullly in Jframe
WA #1.....word association
Driving from VT to MD with two kid
WA #2 ..... word association
Inheritance with two Children?