通过递归检查父子关系构建树类型列表 C# [英] Build tree type list by recursively checking parent-child relationship C#

查看:30
本文介绍了通过递归检查父子关系构建树类型列表 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它有自己的列表,因此可以用树结构表示.

I have One class that has a list of itself so it can be represented in a tree structure.

我正在拉取这些类的平面列表,并希望将其展开.

I am pulling a flat list of these classes and want to unflatten it.

public class Group
{
     public int ID {get;set;}

     public int? ParentID {get;set;}

     public List<Group> Children {get;set;}

}

我希望能够执行以下操作

I want to be able to do the following

List<Group> flatList = GetFlatList() //I CAN ALREADY DO THIS
List<Group> tree = BuildTree(flatList);

如果不明显,则 ParentID 与其父组的 ID 属性相关.

The ParentID related to the ID property on its parent group if that wasnt obvious.

编辑

对于为什么我返回列表而不是单个对象存在一些困惑.

There is some confusion as to why I am returning a list and not a single object.

我正在构建一个 UI 元素,其中包含一个项目列表,每个项目都有一个子项.所以初始列表没有根节点.到目前为止,似乎所有的解决方案都不起作用.

I am building a UI element that has a list of items, each of why has a child. So the initial list DOES NOT have a root node. It seems all of the solutions so far do not work.

这意味着我本质上需要一个使用 Group 类的树型结构列表.

What this means is I essentially need a list of tree type structures using Group class.

推荐答案

我不知道你为什么想要你的 BuildTree 方法返回 List - tree 需要有根节点,所以你应该期望它返回单个 Group 元素,而不是一个列表.

I have no idea why you want your BuildTree method return List<Group> - tree needs to have root node, so you should expect it to return single Group element, not a list.

我会在 IEnumerable 上创建一个扩展方法:

I would create an extension method on IEnumerable<Group>:

public static class GroupEnumerable
{
    public static IList<Group> BuildTree(this IEnumerable<Group> source)
    {
        var groups = source.GroupBy(i => i.ParentID);

        var roots = groups.FirstOrDefault(g => g.Key.HasValue == false).ToList();

        if (roots.Count > 0)
        {
            var dict = groups.Where(g => g.Key.HasValue).ToDictionary(g => g.Key.Value, g => g.ToList());
            for (int i = 0; i < roots.Count; i++)
                AddChildren(roots[i], dict);
        }

        return roots;
    }

    private static void AddChildren(Group node, IDictionary<int, List<Group>> source)
    {
        if (source.ContainsKey(node.ID))
        {
            node.Children = source[node.ID];
            for (int i = 0; i < node.Children.Count; i++)
                AddChildren(node.Children[i], source);
        }
        else
        {
            node.Children = new List<Group>();
        }
    }
}

使用

var flatList = new List<Group>() {
    new Group() { ID = 1, ParentID = null },    // root node
    new Group() { ID = 2, ParentID = 1 },
    new Group() { ID = 3, ParentID = 1 },
    new Group() { ID = 4, ParentID = 3 },
    new Group() { ID = 5, ParentID = 4 },
    new Group() { ID = 6, ParentID = 4 }
};


var tree = flatList.BuildTree();

这篇关于通过递归检查父子关系构建树类型列表 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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