• 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

recursive tree

 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a group of object, each object knows it’s ID and it’s parent ID (if it has one).

I have a method call that will return a List of objects based on a parent ID.

public List<MyRecords> getChildRecords(Long recordID)…

what I need to do is create a method that will return all the objects after I have gotten the root object. I would like to do this recursively.

And in case you are wondering, no this isn’t a class assignment or anything. It is just that looking at the problem I am thinking it would probably be a good example of where to do recursion but I just can’t wrap my head around it right now.

Thanks
 
Pat Peg
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and to give an example of what the data might look like


sorry if the formatting got lost....
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrapped your data in code tags. while it's not actually code, doing that will preserve the formatting.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The algorithm as I've described before:
1) use a Map<ID,Node> to first store the nodes unlinked to their parent nodes. They will need a parent ID though.
2) iterate over the nodes, lookup the parent node in the map using the parent ID, and link together.
 
reply
    Bookmark Topic Watch Topic
  • New Topic