【问题标题】:How to flatten tree怎样把树弄平
【发布时间】:2019-03-06 09:20:06
【问题描述】:

我有一个包含

的嵌套列表
public class Person
{
    public Person(string name)
    {
        this.Name = name;
    }

    public string Name { get; set; }

    public List<Person> Childs { get; set; }
}

列表可以这样使用:

var Persons = new List<Person>();
Persons.Add(new Person("Eric"));
Persons[0].Childs = new List<Person>();
Persons[0].Childs.Add(new Person("Tom"));
Persons[0].Childs.Add(new Person("John"));
Persons[0].Childs[0].Childs = new List<Person>();
Persons[0].Childs[0].Childs.Add(new Person("Bill"));
Persons.Add(new Person("John");

我怎样才能展平这棵树(将所有节点和子节点以及子子节点放在一个列表中),例如我想用一个级别参数显示同一级别的所有孩子和父母。这意味着:

之前:

-Eric
    -Tom
    -John
        -Bill

我想要什么:

-Eric, Level1
-Tom, Level2
-John, Level2
-Bill, Level3

【问题讨论】:

  • 为什么嵌套的 for 循环不适合你?你也可能想看看递归,但我对你的想法是暂时保持简单,然后简单地处理一些 for 循环

标签: c# list tree


【解决方案1】:

递归方法的完美用例

public static void DisplayPerson(List<Person> persons, int level = 0)
{
    if (persons != null)
    {
        level++;
        foreach (Person item in persons)
        {
            Console.WriteLine("-" + item.Name + ", Level" + level); 
            DisplayPerson(item.Childs, level);
        }
    }
}

https://dotnetfiddle.net/2J9F5K

【讨论】:

【解决方案2】:

Stack 类的完美用例。

public static void DisplayPerson(List<Person> persons)
{
    if (persons != null)
    {
        Stack<Person> personStack = new Stack<Person>();
        int level = 1;
        persons.ForEach(personStack.Push);
        while (personStack.Count > 0)
        {
            Person item = personStack.Pop();
            Console.WriteLine("-" + item.Name + ", Level:" + level); 
            if (item.Childs != null)
            {
                item.Childs.ForEach(personStack.Push);
                level++;
            }
        }
    }
}

https://dotnetfiddle.net/eD2GmY


Stack 比 C# 中的递归方法快得多,并且消耗的内存也少得多,而且您可以避免有时由 @fubo 的答案中使用的 recursive C# methods 引起的 StackOverflow。

【讨论】:

    【解决方案3】:

    在不改变原始模型的情况下展平树的非常短的代码:

      static void Main(string[] args)
    {
        var flattedTree=new List<Person>();
        var Persons = new List<Person>();
        Persons.Add(new Person("Eric"));
        Persons[0].Childs = new List<Person>();
        Persons[0].Childs.Add(new Person("Tom"));
        Persons[0].Childs.Add(new Person("John"));
        Persons[0].Childs[0].Childs = new List<Person>();
        Persons[0].Childs[0].Childs.Add(new Person("Bill"));
        Persons.Add(new Person("John"));
        Flatten(Persons, flattedTree);
        //flattedTree variable is the flatted model of tree.
    }
     static void Flatten(List<Person> nodes, List<Person> flattedNodes)
            {
                foreach (var node in nodes)
                {
                    flattedNodes.Add(node);
                    if (node.Childs?.Count > 0)
                        Flatten(node.Childs, flattedNodes);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2014-01-28
      • 2018-03-26
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多