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 ]
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
Joined: Jun 26, 2001
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 ?
Joined: Jan 29, 2003
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.