树像java中的数据结构

时间:2022-01-27 16:26:29

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?

我想在java中为活动对象实现类似数据结构的树,其中节点可以在两个父节点之间共享。我想构建一个活动层次结构,其中一个活动可以包含多个活动。我该怎么做?或者我应该使用其他一些数据结构以及如何使用?

2 个解决方案

#1


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    

}

#2


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.

要决定设计,了解如何导航结构至关重要。如果只是自上而下,您只需要一个Activity来包含其子项列表。如何确保最多两个活动拥有某项活动是另一回事。如果您需要确保,那么您可能需要在活动中使用父级参考。

#1


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    

}

#2


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.

要决定设计,了解如何导航结构至关重要。如果只是自上而下,您只需要一个Activity来包含其子项列表。如何确保最多两个活动拥有某项活动是另一回事。如果您需要确保,那么您可能需要在活动中使用父级参考。