如何给孩子添加到正阵列树中特定的节点? [英] How to add a child to a specific node in n-array tree?

查看:141
本文介绍了如何给孩子添加到正阵列树中特定的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在写这个正阵列树类我想写一个方法,一个孩子在我的树添加到特定节点的方法是:首先,我要寻找我的树找到那么父亲的孩子加入到该节点孩子
我不知道我应该怎么声明我的方法

 公共类FamilyNode {
    公共字符串名称;
    公共字符串的家庭;
    公共字符串的性行为;
    公共FamilyNode父。
    公共FamilyNode母亲;
    公共FamilyNode配偶= NULL;
    公共字符串状态=活着;
    公众诠释人口;
    公众的ArrayList< FamilyNode>孩子=新的ArrayList< FamilyNode>();
    公共FamilyNode(串名字,姓氏字符串,字符串sex1){
        this.name =名字;
        this.Family =姓氏;
        this.sex = sex1;
        this.population = this.children.size()+ 1;
    }    公共无效SetParents(FamilyNode父亲,母亲FamilyNode){
        this.Father =父亲;
        this.Mother =母亲;
    }    公共无效SetHW(FamilyNode HW){
        this.Spouse = HW;
    }    公众诠释号(){
        INT number_of_descendants = this.population;        如果(this.Spouse!= NULL)number_of_descendants ++;        对于(INT指数= 0;指数 - LT; this.children.size();指数++)
            number_of_descendants = number_of_descendants + this.children.get(指数).Number();
            返回number_of_descendants;
    }    公共无效的AddChild(FamilyNode父亲,孩子FamilyNode){        //将code在这里
    }
}


解决方案

我回答一个你的<一个href=\"http://stackoverflow.com/questions/11275817/need-a-search-method-for-my-treen-ary-tree/11275928#11275928\">related问题昨天那么让我们继续与code我张贴:)

 公共类FamilyNode {
    // ...
    // ...
    公共FamilyNode findNodeByName(字符串节点名称){
       如果(name.equals(节点名称)){
          //我们发现一个名为节点名称节点,返回它
          返回此;
       }
       //那不是我,你所寻找的,让我们来看看我的孩子
       对于(FamilyNode孩子:孩子){
            如果(child.findNodeByName(节点名称)!= NULL)
                //我们发现我们正在寻找,从这里只是返回
                返回子女;
       }
       //说完遍历所有节点,并没有发现什么,返回NULL
       返回null;
    }    公共无效的addChild(FamilyNode子){
       children.add(小孩);
    }
}

基本上,你需要找到你(在这种情况下,由名称)寻找节点,可以由 findNodeByName 上面完成。一旦节点被发现,一个孩子到它。

使用此code是这样的:

  FamilyNode根= ...;
FamilyNode节点= root.findNodeByName(母公司);
(!节点= NULL)如果node.addChild(...);

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

 公共FamilyNode findNodeByName(字符串节点名称){
   的System.out.println(访问节点+姓名);
   //那不是我,你所寻找的,让我们来看看我的孩子
   对于(FamilyNode孩子:孩子){
     child.findNodeByName(节点名称)
   }
   //说完遍历所有节点,并没有发现什么,返回NULL
   返回null;
}

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 the way is : first I should search my tree to find the father 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                                         
    }                                        
}

解决方案

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'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);
    }
}

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

Use this code like this:

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

NOTE If you want to debug and visit all your tree nodes, use this method:

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

这篇关于如何给孩子添加到正阵列树中特定的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆