2012-07-01 35 views
5

我写了这个n数组树类现在我想写一个方法来将一个孩子添加到我的树中的特定节点,方法是:首先,我应该搜索我的树来查找父亲,然后将该孩子添加到该节点中 我不知道我应该怎么申报我的方法如何将子项添加到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           
    }           
} 
+1

请你能解决你的缩进和空白;这是很难阅读现在。 –

回答

2

我回答了一个你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); 
    } 
} 

基本上,你需要找到你正在寻找的节点(在这种情况下通过名称),可以通过完成上面的。找到节点后,向其中添加一个小孩。

使用此代码:

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; 
} 
+0

thx很多人:) – Oli

+0

当然没问题:)让我知道它是怎么回事:) – GETah

+0

'for(FamilyNode child:node.children)whats node? ' – Oli

0

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

将变量和方法名称更改为与通常的以小写字符开头的Java约定一致将是一件好事。为了提高数据的一致性,你可以考虑将addChild方法简单地添加到当前节点的子节点列表中,但在方法setParents中,更新父节点的子列表,添加当前节点作为一个孩子,通过拨打father.addChild(this)mother.addChild(this)(防止它们当然无效)。

如果父母可以在之前设置时更改(推测是错误的),那么您还需要从先前设置的父母中删除当前节点。为此,您可能需要一个removeChild(FamilyNode child)方法。再次为了数据的一致性,这个方法应该也可以在子节点中设置合适的父字段为空。

相关问题