aspose file tools*
The moose likes Java in General and the fly likes Java Tree Structure creation from parent child relation ship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Tree Structure creation from parent child relation ship" Watch "Java Tree Structure creation from parent child relation ship" New topic
Author

Java Tree Structure creation from parent child relation ship

Saurabh Pol
Greenhorn

Joined: Dec 05, 2012
Posts: 12
I have to build a tree structure with the following data
lets say that with this data
childId parentId
1 - 0 (lets say this is root)
2 - 1
3 - 1
4 - 3
5 - 3
6 - 5
7 - 6

all I need is that i need to have a way to construct a tree.

Please let me know how can i do this.

Thanks,
Saurabh Pol.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Saurabh Pol wrote:all I need is that i need to have a way to construct a tree.

Please let me know how can i do this.


Design a class whose instances:

- can hold an item of data
- can optionally be attached to one instance of the same class that is higher in a hierarchy
- can optionally be attached to a number of instances of the same class that are lower in a hierarchy

when I say "attached" I simply mean holding a reference
Saurabh Pol
Greenhorn

Joined: Dec 05, 2012
Posts: 12
Hi Dennis,
Thank you for your reply,

Can you please explain in detail, because I am new to the java.

And this tree is dynamic tree we have to generate using database values.

Thanks,
Saurabh Pol.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Dennis Deems wrote: . . .
- can optionally be attached to one instance of the same class that is higher in a hierarchy
- can optionally be attached to a number of instances of the same class that are lower in a hierarchy
. . .
Wouldn’t that be optionally one instance each way. Of course those instances can themselves be attached to instances, and those instances can be attached, etc etc.

I think you are going to have to search for the solution; it should be quite easy to find a decent explanation on Wikipedia. I presume by tree you mean a binary tree? When you find the solution, please tell us what the difference is between a red‑black tree and an ordinary binary tree.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Campbell Ritchie wrote:
Dennis Deems wrote: . . .
- can optionally be attached to one instance of the same class that is higher in a hierarchy
- can optionally be attached to a number of instances of the same class that are lower in a hierarchy
. . .
Wouldn’t that be optionally one instance each way. Of course those instances can themselves be attached to instances, and those instances can be attached, etc etc.


No, it's zero to one parent node -- zero means we are dealing with the root. But it's zero to many child nodes; for example zero to two if you are a binary tree. I was trying to be circumspect and avoid an implementation spoiler by using the term collection.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I see what you mean now. I thought you meant 0…1 on the left and 0…1 on the right. Misunderstood that bit. Sorry.
 
jQuery in Action, 2nd edition
 
subject: Java Tree Structure creation from parent child relation ship