Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Building a tree structure

 
subrato jagan
Greenhorn
Posts: 1
  • 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 - 1 (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 of finding out the
individual child's level
by level i mean

1 - level 0
2 - level 1 (only 1 parent)
3 - level 1
4 - level 2 (one parent and 1 grand parent)
5 - level 2
6 - level 3
7 - level 4

any ideas would be welcome
 
R van Vliet
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps I'm oversimplifying here but once you have the tree structure wouldnt a simple recursive counting method do the trick? :

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can avoid recursion if that matters to you ... to get the level for "subject" ...

I used to see this kind of thing in GUIs where an event handler wants to work back up to the parent window or some other container.
 
Campbell Ritchie
Sheriff
Posts: 48454
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saurabh Pol,
Your post was moved to a new topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic