Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要我的树(n元树)的搜索方法_Java_Search_Tree - Fatal编程技术网

Java 需要我的树(n元树)的搜索方法

Java 需要我的树(n元树)的搜索方法,java,search,tree,Java,Search,Tree,我为一个家庭树写了这个树类 现在我需要一个搜索方法在我的树中找到一个节点 它是一个n元树,每个节点可以有0到n个子节点 搜索方法可以搜索一个节点或两个名称,包括节点名和他/她的父亲名 请帮帮我 public class FamilyNode { public String name; public String sex; public FamilyNode Father; public FamilyNode Mother; public FamilyNode Spouse=null;

我为一个家庭树写了这个树类

现在我需要一个搜索方法在我的树中找到一个节点

它是一个n元树,每个节点可以有0到n个子节点

搜索方法可以搜索一个节点或两个名称,包括节点名和他/她的父亲名

请帮帮我

public class FamilyNode {
 public String name;
 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 name1,String sex1){
this.name=name1;
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 void AddChild(FamilyNode child){
    child.SetParents(this.Father, this.Spouse);
    this.children.add(child);
    this.Spouse.children.add(child);
}

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

}
公共类FamilyNode{
公共字符串名称;
公共性;
公共家庭;共同父亲;
公共家庭;共同母亲;
公共FamilyNode配偶=null;
公共字符串status=“alive”;
公共人口;
public ArrayList children=new ArrayList();
公共FamilyNode(字符串名称1,字符串sex1){
this.name=name1;
这个。性别=性别1;
this.population=this.children.size()+1;
}
父母(家庭成员父亲、家庭成员母亲){
这个。父亲=父亲;
这个。母亲=母亲;
}
公共无效SetHW(家庭节点HW){
这个。配偶=HW;
}
public void AddChild(FamilyNode子项){
孩子。父母(这个。父亲,这个。配偶);
this.children.add(child);
此。配偶。子女。添加(子女);
}
公共整数(){
int number_of_后代=this.population;
如果(this.party!=null)子代数为++;
for(int index=0;index
看看树遍历算法。这很好地涵盖了它们(使用递归和出列) 下面是一种遍历树的方法

public class FamilyNode {
    // ...
    // ...
    public void traverseNode(){
       if(name.equals("NameWeAreLookingFor")){
          // We found a node named "NameWeAreLookingFor"
          // return here if your are not interested in visiting children
       } 
       for(FamilyNode child : node.children){
            child.traverseNode();
       }
    }
}
要遍历所有树,请执行以下操作:

FamilyNode root = ...;
root.traverseNode();

请注意,此方法使用递归。如果您的树很大,那么我建议您改用队列,因为递归可能会导致StackOverFlow异常。

您可能需要查看和上的wikipedia文章。这些是众所周知的树搜索算法。