• 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

Building a tree structure

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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? :

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saurabh Pol,
Your post was moved to a new topic.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic