Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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
C# 递归确定子结构_C#_Recursion_Tree - Fatal编程技术网

C# 递归确定子结构

C# 递归确定子结构,c#,recursion,tree,C#,Recursion,Tree,我创建了以下类(简化): 括号之间的数字最大为;子公司数量或其分支机构中子公司的子公司数量 我尝试了很多东西,但它似乎不能正常工作。我现在有以下资料: private void FindMaxChilds(ClassItem classItem, ref Int64 horizontalMaxLevels) { //Count horizontal for each child if (horizontalMaxLevels < classI

我创建了以下类(简化):

括号之间的数字最大为;子公司数量或其分支机构中子公司的子公司数量

我尝试了很多东西,但它似乎不能正常工作。我现在有以下资料:

private void FindMaxChilds(ClassItem classItem, ref Int64 horizontalMaxLevels) {
            //Count horizontal for each child
            if (horizontalMaxLevels < classItem.Children.Count) {
                horizontalMaxLevels = classItem.Children.Count;
            }

            foreach (ClassItem cci in classItem.Children) {
                FindMaxLevels(cci, ref horizontalMaxLevels);
            }

            classItem.MaxChilds = horizontalMaxLevels;
        }
private void FindMaxChilds(ClassItem ClassItem,参考Int64 HorizontalMaxLevel){
//为每个孩子水平计数
if(水平最大级别

有人能在去那里的路上帮我吗?

我把它分为,DirectChildren和我孩子的输出

public static int MaxDirectChidrenPerItem(ClassItem item)
{
    if (item.Children == null || item.Children.Count == 0)
    {
        return 0;
    }

    int directChildrenCount = item.Children.Count();
    int descendantMaxCount = item.Children.Max(child => MaxDirectChidrenPerItem(child));
    return Math.Max(directChildrenCount, descendantMaxCount);
}

我把它分为,DirectChildren和我孩子的输出

public static int MaxDirectChidrenPerItem(ClassItem item)
{
    if (item.Children == null || item.Children.Count == 0)
    {
        return 0;
    }

    int directChildrenCount = item.Children.Count();
    int descendantMaxCount = item.Children.Max(child => MaxDirectChidrenPerItem(child));
    return Math.Max(directChildrenCount, descendantMaxCount);
}

使用@OrelEraki的答案,我认为您不需要方法,但可以将所有内容封装在一个属性中:

public class ClassItem
{
    public List<ClassItem> Children { get; set; }
    public Int64 MaxChildren
    { 
        get
        {
            if (this.Children == null || this.Children.Count == 0)
            {
                return 0;
            }

            int directChildrenCount = this.Children.Count();
            int descendantMaxCount = this.Children.Max(child => child.MaxChildren);
            return Math.Max(directChildrenCount, descendantMaxCount);
        }
    }
}
公共类分类项
{
公共列表子项{get;set;}
公共Int64 MaxChildren
{ 
得到
{
if(this.Children==null | | this.Children.Count==0)
{
返回0;
}
int directChildrenCount=this.Children.Count();
int-degenantmaxcount=this.Children.Max(child=>child.MaxChildren);
返回Math.Max(directChildrenCount,genderantmaxcount);
}
}
}

使用@OrelEraki的答案,我认为您不需要方法,但可以将所有内容封装在一个属性中:

public class ClassItem
{
    public List<ClassItem> Children { get; set; }
    public Int64 MaxChildren
    { 
        get
        {
            if (this.Children == null || this.Children.Count == 0)
            {
                return 0;
            }

            int directChildrenCount = this.Children.Count();
            int descendantMaxCount = this.Children.Max(child => child.MaxChildren);
            return Math.Max(directChildrenCount, descendantMaxCount);
        }
    }
}
公共类分类项
{
公共列表子项{get;set;}
公共Int64 MaxChildren
{ 
得到
{
if(this.Children==null | | this.Children.Count==0)
{
返回0;
}
int directChildrenCount=this.Children.Count();
int-degenantmaxcount=this.Children.Max(child=>child.MaxChildren);
返回Math.Max(directChildrenCount,genderantmaxcount);
}
}
}


是否应该是
第1(11)类
?它是1+3+2+5之和。不,它的任何分支中的子分支的最大数目是5:1.1有1个,1.2有0个,1.3有3个,1.4有5个,1它自己有4个子分支。(我并不是说所有最深层次的孩子!)那么为什么是1.4(5)呢?因为所有1.4的孩子都有0个孩子。如果我理解正确,它应该是1.4(0)。是的,但1.4它只有5个children@ArthurRey,请放心,我想自己做,但他没有给我们太多的输入(在后期创建时),所以我更喜欢返回int而不是void,让OP更新并调整它,让他处理。可以在
ClassItem
中使用它作为私有方法,然后在MaxChildren getter中使用它。它不应该是
ClassItem 1(11)
?它是1+3+2+5之和。不,它的任何分支中的子分支的最大数目是5:1.1有1个,1.2有0个,1.3有3个,1.4有5个,1它自己有4个子分支。(我并不是说所有最深层次的孩子!)那么为什么是1.4(5)呢?因为所有1.4的孩子都有0个孩子。如果我理解正确,它应该是1.4(0)。是的,但1.4它只有5个children@ArthurRey,请放心,我想自己做,但他没有给我们太多的输入(在后期创建时),所以我更喜欢返回int而不是void,让OP更新并调整它,让他处理。通过将其作为
ClassItem
中的私有方法,然后在MaxChildren getter中使用它。工作非常完美,只需包括对属性的写入。我在递归上瞎盯了好长时间:-)@SLVR2009,而不是将返回值写入属性的方法,考虑使用属性本身作为get ter。你可以在我的答案中找到这个例子。效果非常好,只需包括对物业的书写。我在递归上瞎盯了好长时间:-)@SLVR2009,而不是将返回值写入属性的方法,考虑使用属性本身作为get ter。你可以在我的答案中找到这个例子。将它包含在属性中看起来是一个非常好/聪明的解决方案!然而,我想要一个方法,所以我接受了@OrelEraki的回答,只要你们得到了你们的解决方案:)你们能改变这个项目吗。对这件事有何评论?我还想尝试这个解决方案,并看到了这个问题!好的。更新了。看起来是一个非常好的/智能的解决方案,将其包含在属性中!然而,我想要一个方法,所以我接受了@OrelEraki的回答,只要你们得到了你们的解决方案:)你们能改变这个项目吗。对这件事有何评论?我还想尝试这个解决方案,并看到了这个问题!好的。更新了它。