I want to implement a tree like data structure in java for activity objects, where nodes can be shared between two parents. I want to build a hierarchy of activities, where one activity can be contained by multiple activities. How can I do it? Or should I use some other data structure and how?
            Asked
            
        
        
            Active
            
        
            Viewed 591 times
        
    1
            
            
        - 
                    1If nodes can be shared between two parents, it's not a *tree*, it's a *directed graph*. – Sergey Kalinichenko Jul 12 '12 at 19:07
- 
                    http://stackoverflow.com/questions/3522454/java-tree-data-structure ? – Nivas Jul 12 '12 at 19:08
- 
                    1How do "Tree" and "shared" fit together? You want a [Tree](http://www.squidoo.com/computer-trees) or a [Graph](http://www.google.com/imgres?um=1&hl=en&sa=N&biw=1366&bih=639&authuser=0&tbm=isch&tbnid=JgCaJ-PTd1gprM:&imgrefurl=http://krisnadhi.wordpress.com/2011/06/22/classical-programming-vs-knowledge-based-programming-example/&docid=aLgE2hLRVqgpGM&imgurl=http://krisnadhi.files.wordpress.com/2011/06/graph.png&w=824&h=536&ei=ciD_T43LI-_Z0QWDrIztCQ&zoom=1&iact=rc&dur=334&sig=112451261835707508851&page=2&tbnh=140&tbnw=200&start=21&ndsp=24&ved=1t:429,r:13,s:21,i:187&tx=106&ty=104)? – Adel Boutros Jul 12 '12 at 19:09
- 
                    @dasblinkenlight It's also a *hierarchy*. – Marko Topolnik Jul 12 '12 at 19:12
- 
                    @AdelBoutros: Parents are shared in a tree. – megazord Jul 12 '12 at 21:16
- 
                    @megazord please read correctly before you explain. He specifically said "shared between two parents" which is not what you meant :) In a tree, every node has **one and only one** parent – Adel Boutros Jul 12 '12 at 21:31
- 
                    @dasblinkenlight How do i induce the concept of hierarchy in directed graph ? – Golra Jul 12 '12 at 21:53
2 Answers
1
            
            
        Something like this should work:
public class Activity {
    private Activity parent1;
    private Activity parent2;
    private List<Activity> children;
    // other fields, getters, setters, methods, etc    
}
 
    
    
        Bohemian
        
- 412,405
- 93
- 575
- 722
- 
                    But this one says that every activity has two parents. and many children. however I want a data structure where i can handle many activities, but some of them might have two parents. And then which data structure of use? – Golra Jul 12 '12 at 21:44
0
            
            
        To decide on the design, it is of key importance to know how you need to navigate your structure. If only top-down, all you need is for an Activity to contain a list of its children. How you ensure that at most two activities own a certain activity is another thing. If you need to ensure that, then you'll probably need parent refs in an activity.
 
    
    
        Marko Topolnik
        
- 195,646
- 29
- 319
- 436
- 
                    what if activity-1 has A, B, and C as childs and activity-2 has C and D as childs, where each child is itself a parent of other activities. – Golra Jul 12 '12 at 21:47
- 
                    
