【问题标题】:Naming the nodes of a Tree Structure命名树结构的节点
【发布时间】:2016-02-21 08:46:57
【问题描述】:

我正在实现基于this 答案的树。根据给定的简单使用示例,新节点以它们在树中的位置命名,因此它们是变量。 例如:

TreeNode<String> node0 = root.addChild("node0");

其中 node0 是一个新的 TreeNode 和新子节点将拥有的 data。

根据this 和this 我不能使用字符串来命名变量。

我打算为我的树的每个节点创建 26 个子节点。我的问题很简单,我是否必须通过手动创建树将具有的所有 26 个可能节点来创建树,如下所示?

TreeNode<String> node0 = root.addChild("node0");
TreeNode<String> node1 = root.addChild("node1");
TreeNode<String> node2 = root.addChild("node2");
...
TreeNode<String> node25 = root.addChild("node25");
{
    TreeNode<String> node00 = node0.addChild("node00");
    ...
    {
        //the above code for all the nodes of the tree
    }
}

还是我错过了更好的解决方案?谢谢

【问题讨论】:

  • 为什么不循环添加 26 个孩子呢? for (int id = 0; id &lt; 26; ++id) { root.addChild("node" + id); }
  • @Turing85 这将适用于 root 但 node0 例如呢?我不能使用 node+"0" 变量来做 node0.addChild(...)。
  • 您总是可以在循环中嵌套另一个循环。如果您想将构造深度作为参数传递,则可以使用递归。
  • @Turing85 我认为你没有抓住重点,我可以使用循环来添加节点,但我无法命名父节点。请看一下我包含的参考资料。如果我错过了这一点,您可以向我解释如何构造 node0.addChild 字段中的 node0(父名称)。
  • 如果您希望变量名 (nodeXY) 从 String 动态构造,那么答案是:不,这是不可能的。但是为什么不使用某种集合来管理您的节点(即数组或Lists)?

标签: java data-structures tree


【解决方案1】:

首先,我会将每个节点的“名称”存储在一个成员变量中,并为它定义一个 getter。我还将添加一个方法来获取节点已经拥有的子节点数量。如果这样做,您将在实例化节点时自动命名节点。在类定义中,添加:

public class TreeNode<T> implements Iterable<TreeNode<T>> {

    T data;
    TreeNode<T> parent;
    List<TreeNode<T>> children;

    ...

    // A string containing the node name, (e.g. "210")
    String name;


    // A name getter method
    public String getName() {
        return this.name;
    }

    // A method to get the number of children that the node has
    public int getNumChildren() {
        return this.children.size();
    }

}

现在,您可以在构造函数中自动命名节点:

public TreeNode<T>(T data, TreeNode<T> parent) {
    ...
    this.parent = parent;
    int numParentChildren = parent.getNumChildren();
    this.name = parent.getName() + numParentChildren;
}

关于你关于树创建的问题,最好将它封装在一个方法中:

public LinkedList<TreeNode<T>> createTree() {
    //Root TreeNode
    TreeNode<T> root = new TreeNode<T>(data, null);

    //TreeNode on which we currently operate
    TreeNode<T> current;
    //List of treenodes holding the result
    List<TreeNode<T>> treeList = new LinkedList<TreeNode<T>>();
    //Queue holding the nodes for which we will create 26 children elements
    Queue<TreeNode<T>> queue = new LinkedList<TreeNode<T>>();
    treeList.add(root);
    queue.add(root);

    for (int i=0; i< (some number); i++) {
    current = queue.remove();
    child = new TreeNode<T>>(data, current);
    current.addChild(child);
    queue.add(child);
    treelist.add(child);
    }
    return treeList;

}

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    虽然变量名称可能不具有动态特性,但 Map 允许抽象根据您设计的命名法(即基于树的级别和节点位置)放置/获取 TreeNode )。 这允许在循环中创建节点。

    为了便于使用,您可能需要考虑这样的方法:

    public String getKeyName(int treeLevel, int nodePosition) {
        // Build the key name...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      相关资源
      最近更新 更多