2012-07-01 34 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)方法。再次爲了數據的一致性,這個方法應該也可以在子節點中設置合適的父字段爲空。

相關問題