get tree Like Class children 使用递归

get tree Like Class children using recursion

我建立了一个 class 集群如下:

public  class Cluster
{
    List<Cluster> lstChildClusters=new List<Cluster>();

     public List<Cluster> LstChildClusters
     {    
        get { return lstChildClusters; }
        set { lstChildClusters = value; }
     }

     public    classA alr;
}

我的目标是构建一个函数来获取 Cluster 对象的所有孙子 type.Basically 一个父亲可以有 0 个或更多的儿子,轮到他们可以有 0 个或更多的儿子。

我试图构建一个递归函数,但它只返回一个使用下面代码的孙子。 这是我构建的函数:

public List<classA> getLevel0Clusters(Cluster cluster,List<classA> list)
    {
        if (cluster.LstChildClusters.Count == 0)
        {
            list.Add(cluster.alr);
          return (list);

        }
        else
        {
            for (int i = 0; i < lstChildClusters.Count - 1; i++)
            {
                return (lstChildClusters[i].getLevel0Clusters(lstChildClusters[i], list));
            }
            return (lstChildClusters[0].getLevel0Clusters(lstChildClusters[0], list));
        }

    }

我正在使用这些实例进行调试:

   Cluster father = new Cluster();
        father.Alr = new Alarm("father");
        Cluster son1 = new Cluster();
        son1.Alr = new Alarm("son1");
        Cluster son2 = new Cluster();
        son2.Alr = new Alarm("son2");
        Cluster grandson1 = new Cluster();
        grandson1.Alr = new Alarm("grandson1");
        Cluster grandson2 = new Cluster();
        grandson2.Alr = new Alarm("grandson2");
        father.LstChildClusters.Add(son1);
        father.LstChildClusters.Add(son2);
        son1.LstChildClusters.Add(grandson1);
        son1.LstChildClusters.Add(grandson2);
List<classA> lst=new lst<ClassA>();
lst=father.getLevel0Clusters(father,  father.LstAlarms);

有人知道如何解决这个问题吗? 提前谢谢你

问题是,一旦您找到 一个 后代,您就会 return 到调用程序。 Count 的值除了 0 与正数没有任何影响:你进入循环,调用 lstChildClusters[0].getLevel0Clusters (lstChildClusters[0] 和 return 该值无需增加 i 并继续循环。

相反,您的 for 循环必须将每个 return 值添加到列表中。 循环完成后,可以return调用程序。

您现有的代码存在许多问题,因此我做了一些重构以使您的程序更简单。

但首先,要回答您的直接问题,您现有方法的问题是您在完成所有结果的聚合之前调用 return。您的代码查看 grandfather 并发现它有 children,因此它进入 for 循环并递归调用自身以获得 son1。它看到 son1 有 children 所以进入 for 循环并递归调用自己 for grandson1 没有 children 所以它添加 grandson1 到列表然后 returns。找到第一个值后,外部调用 returns,因此接下来的两个级别仅 return。因此列表只有 grandson1.

因此,要重构您的代码:getLevel0Clusters 方法不需要传入 Cluster(因为它在 Cluster class 中定义,它可以使用 this) 和一个 List<classA> (因为它可以根据需要生成一个)。

所以你的 getLevel0Clusters 可以简单地变成这样:

public List<classA> getLevel0Clusters()
{
    return new[] { this.alr, }
        .Concat(this.LstChildClusters
            .SelectMany(child => child.getLevel0Clusters()))
        .ToList();
}

为了编译所有内容,我将您的示例代码修改为:

Cluster father = new Cluster();
father.alr = new classA("father");
Cluster son1 = new Cluster();
son1.alr = new classA("son1");
Cluster son2 = new Cluster();
son2.alr = new classA("son2");
Cluster grandson1 = new Cluster();
grandson1.alr = new classA("grandson1");
Cluster grandson2 = new Cluster();
grandson2.alr = new classA("grandson2");
father.LstChildClusters.Add(son1);
father.LstChildClusters.Add(son2);
son1.LstChildClusters.Add(grandson1);
son1.LstChildClusters.Add(grandson2);
List<classA> lst = father.getLevel0Clusters();

...你的 class 是这样的:

public class Cluster
{
    List<Cluster> lstChildClusters = new List<Cluster>();

    public List<Cluster> LstChildClusters
    {
        get { return lstChildClusters; }
        set { lstChildClusters = value; }
    }
    public classA alr;

    public List<classA> getLevel0Clusters()
    {
        return new[] { this.alr, }
            .Concat(this.LstChildClusters
                .SelectMany(child => child.getLevel0Clusters()))
            .ToList();
    }
}

public class classA
{
    public string Name;
    public classA(string name)
    {
        this.Name = name;
    }
}

当我运行你的示例代码时,我得到了这个结果: