递归使用C#与父子关系构建树 [英] Building a tree with parent child relationship using c# recursively

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

问题描述

我有一个想要转换为树形结构的列表。如何将其转换为树结构?

I have a list which I want to convert into tree structure. How can I convert this into tree structure?

我研究了线程来构建树类型列表,但是由于类中的键是字符串,因此无法使用该解决方案。请帮助

I have looked into Build tree type list by recursively checking parent-child relationship C# thread but since key in my class is string, I cannot use that solution. Please help

internal class Program
{
        private static void Main(string[] args)
        {
            List<node> nodeList = new List<node>();
            node n = new node("A", "A1", null, 1); nodeList.Add(n);
            n = new node("B", "A2", "A1", 2); nodeList.Add(n);
            n = new node("C", "A3", "A1", 2); nodeList.Add(n);
            n = new node("D", "A4", "A1", 2); nodeList.Add(n);

            n = new node("E", "A5", "A2", 3); nodeList.Add(n);
            n = new node("F", "A6", "A5", 4); nodeList.Add(n);
            n = new node("G", "A7", "A3", 3); nodeList.Add(n);
            n = new node("H", "A8", "A4", 3); nodeList.Add(n);
            n = new node("I", "A9", "A4", 3); nodeList.Add(n);
            n = new node("J", "A10", "A4", 3); nodeList.Add(n);
            n = new node("K", "A11", "A10", 4); nodeList.Add(n);
            n = new node("L", "A12", "A10", 4); nodeList.Add(n);
            n = new node("M", "A13", "A12", 5); nodeList.Add(n);
            n = new node("N", "A14", "A12", 5); nodeList.Add(n);
            n = new node("O", "A15", "A10", 4); nodeList.Add(n);

            n = new node("P", "A16", null, 1); nodeList.Add(n);
            n = new node("Q", "A17", "A16", 2); nodeList.Add(n);
        }
}

public class node
{
        public string name { get; set; }
        public string key { get; set; }
        public string parentKey { get; set; } 
        public int level { get; set; }

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

        public node(string Name, string Key, string PK, int Level)
        {
            name = Name;
            key = Key;
            parentKey = PK;
            level = Level;
        }
}


推荐答案

它非常简单的重构,将父ID从 int 更改为 string

It very simple re-factoring to change parent id from int to string

public static class GroupEnumerable {

    public static IList<node> BuildTree(this IEnumerable<node> source)
    {
        var groups = source.GroupBy(i => i.parentKey);

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

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

        return roots;
    }

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

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

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