简体   繁体   English

回溯没有可变成员的树

[英]backtrace a tree without mutable members

I am trying to make a recursive tree such that parent has reference to child and child has reference to parent . 我正在尝试创建一个递归树,以使parent引用childchild引用parent The thing I am trying to do is to backtrace the tree from a child whitout mutable members. 我正在尝试做的是从一个child变数成员追溯回树。 It is hard because giving the child a reference to the parent in the constructor requireds the parent instance to be already created. 这很困难,因为在构造函数中为子代提供对parent的引用需要先创建parent实例。

I only could think of two ways and both are not so good. 我只能想到两种方式,但都不太好。 The first way is the fallowing 第一种方法是休闲

  1. create child instance with function "setParent()" which only works once with the help of a private boolean variable. 使用“ setParent()”函数创建子实例,该实例只能在私有布尔变量的帮助下工作一次。
  2. create parent and pass the child instance. 创建父对象并传递子实例。
  3. the parent will pass itself to "setParent()". 父级会将自己传递给“ setParent()”。

After that, child has reference to parent and setParent() cannot be used. 此后,child引用了parent,因此无法使用setParent()。

The second way is to create parent and child completly separate but hold them in some sort of data structure which can search for parent of some child and the otherway arround. 第二种方法是完全分开创建parentchild但将它们保留在某种数据结构中,该结构可以搜索某个子级的parent ,反之亦然。

If there is a better way please teach me. 如果有更好的方法,请教我。 I work mainly in java but the question is general. 我主要在Java中工作,但问题很普遍。

There are several ways to do this, but I'd probably give the parent a createChild method that creates a new node, passing itself as the parent, and adds this child to itself before returning it. 有几种方法可以做到这一点,但是我可能会给父对象一个createChild方法,该方法创建一个新节点,将自身作为父节点传递,并在返回之前将其添加到自身。 You imply in the question that having a preexisting parent is difficult for some reason, but if you're making a tree, you ought to know whose parents are whose. 您在这个问题中暗示,由于某种原因,很难拥有一个既存的父母,但是如果您正在做一棵树,则应该知道谁是谁的父母。 If you don't, a tree might not be the data structure you're looking for. 否则,树可能不是您要查找的数据结构。

I'd probably structure it such that your Element constructor may take another Element . 我可能会对其进行结构化,以便您的Element构造函数可以使用另一个Element If so, it sets a private variable ( parent ) to that other Element . 如果是这样,它将为另一个Element设置一个私有变量( parent )。 If it's created without a parent element passed in, it will never have a parent. 如果创建时未传入父元素,则它将永远不会有父元素。 So the class might look something like this (omitting accessors and any other stuff obviously): 因此,该类可能看起来像这样(显然省略了访问器和任何其他内容):

public class Element {

    private Element parent;
    private ArrayList<Element> children;

    // Constructor for a node with NO parent
    public Element() {
        this(null);
    }

    // Constructor for a node WITH a parent
    public Element(Element parent) {
        this.parent = parent;
        this.children = new ArrayList<Element>();
    }

    // Method to create a child element.
    public Element createChild() {
        Element ch = new Element(this);
        this.children.add(ch);
        return ch;
    }
}

If your problem is that you're trying to build a "tree" by tracing from a child up to some ancestor, then I encourage you to notice that that's actually a list, not a tree. 如果您的问题是您试图通过从孩子到某个祖先的追踪来构建一棵“树”,那么我建议您注意那实际上是一个列表,而不是一棵树。 You can just make the list from bottom to top and then build the tree from top to bottom by starting at the end of the list. 您可以只从底部开始创建列表,然后从列表末尾开始从顶部到底部构建树。

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

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