【问题标题】:How to add a child to a specific node in n-array tree?如何将子节点添加到 n 数组树中的特定节点?
【发布时间】:2012-07-02 07:25:55
【问题描述】:

我写了这个 n 数组树类。我想编写一个方法来将子节点添加到树中的特定节点。

首先我应该搜索我的树以找到父亲,然后将孩子添加到该节点孩子。 我不知道我应该如何声明我的方法

public class FamilyNode {
    public String name;
    public String Family;
    public String sex;
    public FamilyNode Father;
    public FamilyNode Mother;
    public FamilyNode Spouse=null;
    public String status="alive";
    public int population;
    public ArrayList<FamilyNode> children=new ArrayList<FamilyNode>() ;


    public FamilyNode(String firstname,String lastname,String sex1){
        this.name=firstname;
        this.Family=lastname;
        this.sex=sex1;
        this.population=this.children.size()+1;
    }

    public void SetParents(FamilyNode father,FamilyNode mother){
        this.Father=father;
        this.Mother=mother;
    }

    public void SetHW(FamilyNode HW){
        this.Spouse=HW;
    }

    public int Number (){
        int number_of_descendants = this.population;

        if(this.Spouse!=null) number_of_descendants++;

        for(int index = 0; index < this.children.size(); index++)
            number_of_descendants = number_of_descendants+ this.children.get(index).Number();
            return number_of_descendants;
    }

    public void AddChild(FamilyNode Father,FamilyNode child){

        //the code here                                         
    }                                        
}

【问题讨论】:

  • 请你修复你的缩进和空白;现在很难阅读。

标签: java arrays search tree


【解决方案1】:

我昨天回答了你的一个related questions,所以让我们继续我发布的代码:)

public class FamilyNode {
    // ...
    // ...
    public FamilyNode findNodeByName(String nodeName){
       if(name.equals(nodeName)){
          // We found a node named nodeName, return it
          return this;
       } 
       // That's not me that you are looking for, let's see my kids
       for(FamilyNode child : children){
            if(child.findNodeByName(nodeName) != null) 
                // We found what we are looking, just return from here
                return child;
       }
       // Finished looping over all nodes and did not find any, return null
       return null;
    }

    public void addChild(FamilyNode child){
       children.add(child);
    }
}

基本上,您需要找到您要查找的节点(在本例中按名称),这可以通过上面的 findNodeByName 完成。找到节点后,为其添加一个子节点。

像这样使用这段代码:

FamilyNode root = ...;
FamilyNode node = root.findNodeByName("Parent");
if(node != null) node.addChild(...);

注意 如果要调试并访问所有树节点,请使用此方法:

public FamilyNode findNodeByName(String nodeName){
   System.out.println("Visiting node "+ name);
   // That's not me that you are looking for, let's see my kids
   for(FamilyNode child : children){
     child.findNodeByName(nodeName)
   }
   // Finished looping over all nodes and did not find any, return null
   return null;
}

【讨论】:

  • 当然没问题 :) 让我知道情况如何 :)
  • for(FamilyNode child : node.children) whats node?
  • 抱歉,应该是children。请查看我的更新
  • 你知道我的问题吗?添加一个子节点后,我需要返回整个根(树),这样我们找到一个节点并向其添加一个子节点,但之后我们没有第一棵添加了子节点的树,我希望可以说是正确的
  • 你的树的根不应该改变,一直保持对它的引用,这样你就可以随时访问你的整个树。我希望这能解决您的问题。
【解决方案2】:

这并不完全是一棵树,因为孩子可能有两个父母,而不仅仅是一个。这是一个有向图。

最好将您的变量和方法名称更改为与通常的 Java 约定以小写字符开头。

出于数据一致性的考虑,您可能会考虑将 addChild 方法添加到当前节点的子列表中,但在您的 setParents 方法中,更新两个父节点的子列表,添加通过调用father.addChild(this)mother.addChild(this) 将当前节点作为子节点(当然要防止它们为空)。

如果父节点在之前设置时可以更改(可能是错误的),您还需要从之前设置的父节点中删除当前节点。为此,您可能需要一个 removeChild(FamilyNode child) 方法。同样为了数据的一致性,这个方法可能还应该将子节点中相应的父字段设置为 null。

【讨论】:

    猜你喜欢
    • 2012-04-15
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 2010-11-26
    • 2021-03-16
    相关资源
    最近更新 更多