C# 怎样把树压平
我有一个嵌套列表,其中包含C# 怎样把树压平,c#,list,tree,C#,List,Tree,我有一个嵌套列表,其中包含 public class Person { public Person(string name) { this.Name = name; } public string Name { get; set; } public List<Person> Childs { get; set; } } 我想要的是: -Eric, Level1 -Tom, Level2 -John, Level2 -Bil
public class Person
{
public Person(string name)
{
this.Name = name;
}
public string Name { get; set; }
public List<Person> Childs { get; set; }
}
我想要的是:
-Eric, Level1
-Tom, Level2
-John, Level2
-Bill, Level3
递归方法的完美用例
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);
}
}
}
publicstaticvoiddisplayperson(列出人员,int级别=0)
{
如果(人!=null)
{
级别++;
foreach(个人项目以个人为单位)
{
Console.WriteLine(“-”+item.Name+”,Level“+Level);
DisplayPerson(item.Childs,级别);
}
}
}
是
堆栈类的完美用例
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++;
}
}
}
}
公共静态无效显示人员(列出人员)
{
如果(人!=null)
{
Stack personStack=新堆栈();
智力水平=1;
persons.ForEach(personStack.Push);
而(personStack.Count>0)
{
Person-item=personStack.Pop();
Console.WriteLine(“-”+item.Name+”,Level:“+Level”);
如果(item.Childs!=null)
{
物品。儿童。ForEach(personStack.Push);
级别++;
}
}
}
}
堆栈比C#中的递归方法快得多,占用的内存也少得多,而且您可以避免堆栈溢出,这种溢出有时是由@fubo的答案中使用的方法引起的。非常短的代码,可以在不更改原始模型的情况下展平树:
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);
}
}
static void Main(字符串[]args)
{
var flattedTree=新列表();
var Persons=新列表();
人员。添加(新人员(“埃里克”);
人员[0]。Childs=新列表();
Persons[0].Childs.Add(新的Persons(“Tom”);
Persons[0].Childs.Add(新的Persons(“John”);
人员[0]。儿童[0]。儿童=新列表();
人员[0].Childs[0].Childs.Add(新人员(“账单”);
人员。添加(新人员(“约翰”);
展平(人、展平树);
//flattedTree变量是树的平坦模型。
}
静态空心展开(列表节点、列表展开节点)
{
foreach(节点中的var节点)
{
添加(节点);
if(node.Childs?.Count>0)
展平(node.Childs、flattedNodes);
}
}
为什么嵌套for循环不适合您?另外,您可能想看看递归,但是我的想法是暂时保持它的简单性,并且在这种情况下使用递归方法进行循环。在这些情况下使用。
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++;
}
}
}
}
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);
}
}