Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Tree Structure creation from parent child relation ship

 
Saurabh Pol
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49410
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49410
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic