简体   繁体   English

Java中的树状数据结构

[英]tree like data structure in java

I want to implement a tree like data structure in java for activity objects, where nodes can be shared between two parents. 我想在Java中为活动对象实现一个像树一样的数据结构,其中节点可以在两个父对象之间共享。 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? 还是应该使用其他数据结构?如何使用?

Something like this should work: 这样的事情应该起作用:

public class Activity {

    private Activity parent1;
    private Activity parent2;
    private List<Activity> children;

    // other fields, getters, setters, methods, etc    

}

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. 如果仅是自上而下的话,则只需要一个Activity包含其子列表即可。 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. 如果需要确保这一点,那么您可能需要在活动中使用父引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM