How to add a child to a specific node in an n-array tree?

I wrote this n-array tree class. Now I want to write a method to add a child to a specific node in my tree: first I have to find my tree to find the father, and then add the child to that node children I donโ€™t know how I should declare my method

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                                         
    }                                        
}
+5
source share
2 answers

I answered one of your related questions yesterday, so let's continue with the code I posted :)

public class FamilyNode {
    // ...
    // ...
    public FamilyNode findNodeByName(String nodeName){
       if(name.equals(nodeName)){
          // We found a node named nodeName, return it
          return this;
       } 
       // That not me that you are looking for, let 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);
    }
}

Basically, you need to find the node you are looking for (by name in this case), and this can be done using the findNodeByNameabove. Once the node is found, add one child to it.

Use this code as follows:

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

, :

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

, , . .

, Java, .

, addChild -, node, setParents , node , father.addChild(this) mother.addChild(this) ( , ).

, (, ), node . removeChild(FamilyNode child). , , node null.

0

All Articles