File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Building a tree structure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Building a tree structure" Watch "Building a tree structure" New topic

Building a tree structure

subrato jagan

Joined: Nov 15, 2007
Posts: 1
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

Joined: Nov 10, 2007
Posts: 144
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

Joined: Jan 29, 2003
Posts: 8791
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.

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
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
Saurabh Pol,
Your post was moved to a new topic.
I agree. Here's the link:
subject: Building a tree structure
It's not a secret anymore!