• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to dispaly Nodes and children ?

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could you please tell me how to iterate a Node and its children and display them.
thanks
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"mlotfi",

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 ]
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
majid nakit
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

thanks
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic