Java 使用递归在家族树上卡住

Java 使用递归在家族树上卡住,java,recursion,Java,Recursion,有一个家谱,我想知道两个人的关系。 比如说, Getone祖先(混沌,天王星)返回“天王星出生于混沌中的盖亚” 现在我有了:(我只复制了部分代码,希望其他人不会影响) ''' 静态类个人{ 公共字符串名称; 公共个人[]儿童; 公共个人(字符串名称,个人[]子项){ this.name=名称; 这个。孩子=孩子; } 布尔noChildren(){ if(this.children==null){ 返回true; }否则{ 返回false; } } 字符串GetOneSenator(单个父项、

有一个家谱,我想知道两个人的关系。 比如说,

Getone祖先(混沌,天王星)返回“天王星出生于混沌中的盖亚”

现在我有了:(我只复制了部分代码,希望其他人不会影响) '''

静态类个人{
公共字符串名称;
公共个人[]儿童;
公共个人(字符串名称,个人[]子项){
this.name=名称;
这个。孩子=孩子;
}
布尔noChildren(){
if(this.children==null){
返回true;
}否则{
返回false;
}
}
字符串GetOneSenator(单个父项、字符串子项){
//如果祖先没有孩子
if(parent.noChildren()){
返回null;
}
for(int i=0;i
''' 我被困在getOneEssentialy()上,我知道我应该使用递归,但我无法解决它。
所以任何人都可以帮助我吗?

当使用递归时,您需要对结果做一些处理,并相应地返回一个值,否则什么也不会发生。在下面的代码中,我们使用return语句在祖先和后代之间建立了一个人链。我已经在下面的代码中注释了需要采取的其他步骤。关键部分是
stringresult=getonesensort(…)
returnresult+“bornof”+sensort.name如图所示:

//Input args renamed to keep it simple and clear
String  getOneAncestor(Individual ancestor, String descendant){
    //You can remove this check, the null return after the for loop takes care of this
    //if (ancestor.noChildren()){
    //    return null;
    //}

    for (int i = 0; i < ancestor.children.length; i++) {
        //Check to find the descendant
        if (ancestor.children[i].name.equalsIgnoreCase(descendant)){
            //when the descendant is found then return their name
            //The below recursive method inside the else statement will take care of the " born of " part
            return ancestor.children[i].name;
        }
        //Otherwise check the children's children for a match
        else{
            //Get the return of the recursive method so that you can do something with it
            String result = getOneAncestor(ancestor.children[i], descendant);
            
            //If a match is found in one of the children (not null returned)
            //Then we can append the " bourn of " text and return the result
            if (result != null){
                //Add the in-between relation and return the string of all relations so far in the tree
                return result + " born of " + ancestor.name;
            }
        }
    }
    //Return null if no match found within the generation
    //Or if the ancestor has no children
    return null;
}
//重命名输入参数以保持其简单明了
字符串GetOneSenator(单个祖先、字符串后代){
//您可以删除此检查,在for循环处理此问题之后,返回null
//if(祖先.noChildren()){
//返回null;
//}
for(int i=0;i<祖先.children.length;i++){
//检查以查找后代
if(祖先.children[i].name.equalsIgnoreCase(后代)){
//找到子代后,返回其名称
//下面else语句中的递归方法将处理“born of”部分
返回祖先.children[i].name;
}
//否则,请检查孩子的孩子是否匹配
否则{
//获取递归方法的返回值,以便对其进行处理
String result=getonesensort(祖先.children[i],后代);
//如果在其中一个子项中找到匹配项(返回非null)
//然后我们可以附加“bourn of”文本并返回结果
如果(结果!=null){
//添加in-between关系并返回树中到目前为止所有关系的字符串
返回结果+“出生于”+祖先名称;
}
}
}
//如果在生成中未找到匹配项,则返回null
//或者如果祖先没有孩子
返回null;
}

但实际上,在if语句中,返回值应该包含“born of”。我不知道为什么,但只是运行测试,它显示,当找到子代时,不需要添加部分的边界,因为我们还不知道祖先是谁,相反,当子代返回到上一个递归调用时,else语句添加部分的出生。运行代码时,它是否正常工作?
//Input args renamed to keep it simple and clear
String  getOneAncestor(Individual ancestor, String descendant){
    //You can remove this check, the null return after the for loop takes care of this
    //if (ancestor.noChildren()){
    //    return null;
    //}

    for (int i = 0; i < ancestor.children.length; i++) {
        //Check to find the descendant
        if (ancestor.children[i].name.equalsIgnoreCase(descendant)){
            //when the descendant is found then return their name
            //The below recursive method inside the else statement will take care of the " born of " part
            return ancestor.children[i].name;
        }
        //Otherwise check the children's children for a match
        else{
            //Get the return of the recursive method so that you can do something with it
            String result = getOneAncestor(ancestor.children[i], descendant);
            
            //If a match is found in one of the children (not null returned)
            //Then we can append the " bourn of " text and return the result
            if (result != null){
                //Add the in-between relation and return the string of all relations so far in the tree
                return result + " born of " + ancestor.name;
            }
        }
    }
    //Return null if no match found within the generation
    //Or if the ancestor has no children
    return null;
}