创建族树结构

时间:2017-06-11 22:48:33

标签: c# linq tree

我需要使用下面的代码创建一个家族树结构。我只能在ShowFamily方法中添加代码。我可以归还GrandDad叔叔阿姨和爸爸。但由于某种原因,我无法回报我和姐姐。你们中的任何人可以帮助我知道它必须简单,谢谢大家

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace FamilyPrinter
{
// Classes defining the structure of a family
abstract class Person
{
    public string Name { get; set; }
}

class Child : Person
{
}

class Parent : Person
{
    public List<Person> Children { get; set; }

}

class Ancestor : Parent
{

}


class Program
{
    static void Main(string[] args)
    {

        Ancestor myAncestor = new Ancestor()
        {
            Name = "GrandDad",
            Children = new List<Person>()
            {
                new Child() { Name = "Aunt" },
                new Child() { Name = "Uncle" },
                new Parent()
                {
                    Name = "Dad",
                    Children = new List<Person>()
                    {
                        new Child { Name = "Me" },
                        new Child { Name = "Sister" }
                    }
                }
            }
        };
        ShowFamily(myAncestor);
    }


    private static void ShowFamily(Ancestor a)
    {
        Console.WriteLine("*"+a.Name);
        foreach (var value in a.Children)
        {
            Console.WriteLine("-"+value.Name);
        }           
    }
}

2 个答案:

答案 0 :(得分:2)

您应该将类​​结构更改为:

public class Person
{
    public string Name { get; set; }
    public List<Person> Children { get; set; } = new List<Person>();
}

然后任何Person都可以生孩子。

现在你可以像这样定义你的家庭:

var myAncestor = new Person()
{
    Name = "GrandDad",
    Children = new List<Person>()
    {
        new Person() { Name = "Aunt" },
        new Person() { Name = "Uncle" },
        new Person()
        {
            Name = "Dad",
            Children = new List<Person>()
            {
                new Person() { Name = "Me" },
                new Person() { Name = "Sister" },
            }
        }
    }
};

然后ShowFamily看起来像这样(用两种方法):

private static void ShowFamily(Person a)
{
    ShowFamily(a, 0);       
}

private static void ShowFamily(Person a, int level)
{
    Console.WriteLine("".PadLeft(level * 4) + (a.Children.Any() ? "*" : "-") + a.Name);
    foreach (var c in a.Children)
    {
        ShowFamily(c, level + 1);
    }             
}

现在调用ShowFamily(myAncestor);会输出:

*GrandDad
    -Aunt
    -Uncle
    *Dad
        -Me
        -Sister

你也可以尽可能深入:

var myAncestor = new Person()
{
    Name = "GrandDad",
    Children = new List<Person>()
    {
        new Person() { Name = "Aunt" },
        new Person() { Name = "Uncle" },
        new Person()
        {
            Name = "Dad",
            Children = new List<Person>()
            {
                new Person()
                {
                    Name = "Me",
                    Children = new List<Person>()
                    {
                        new Person() { Name = "John" },
                        new Person()
                        {
                            Name = "Jill",
                            Children = new List<Person>()
                            {
                                new Person() { Name = "Sally" },
                                new Person() { Name = "Simon" },
                            }
                        },
                    }
                },
                new Person() { Name = "Sister" },
            }
        }
    }
};

现在ShowFamily给出了这个:

*GrandDad
    -Aunt
    -Uncle
    *Dad
        *Me
            -John
            *Jill
                -Sally
                -Simon
        -Sister

答案 1 :(得分:1)

这是另一种使用递归(以及一些缩进来显示关系)的方法。因为所有类型都基于Parent,我们可以使用该类型作为输入,然后检查方法内部以查看该人是否为*。这将允许您从同一方法中获取多个扩展树(父母的子女是父母,他们是父母,父母是兄弟姐妹)。按照您的示例,父母姓名旁边有一个星号private static void ShowFamily(Person person, int indent = 0) { var indentLines = new string(' ', indent); if (person is Parent) { Console.WriteLine(indentLines + "*" + person.Name); var parent = person as Parent; foreach (var child in parent.Children) { ShowFamily(child, indent + 2); } } else { Console.WriteLine(indentLines + "-" + person.Name); } }

static void Main(string[] args)
{
    Ancestor myAncestor = new Ancestor
    {
        Name = "GrandDad",
        Children = new List<Person>
        {
            new Parent
            {
                Name = "Aunt",
                Children = new List<Person>
                {
                    new Child { Name = "Cousin1" },
                    new Parent
                    {
                        Name = "Cousin2",
                        Children = new List<Person>
                        {
                            new Child { Name = "FirstCousinOnceRemoved" }
                        }
                    },
                    new Child { Name = "Cousin3" }
                }
            },
            new Child { Name = "Uncle" },
            new Parent
            {
                Name = "Dad",
                Children = new List<Person>()
                {
                    new Child { Name = "Me" },
                    new Parent
                    {
                        Name = "Sister",
                        Children = new List<Person>
                        {
                            new Child { Name = "Niece" }
                        }
                    }
                }
            }
        }
    };

    ShowFamily(myAncestor);
}

更复杂的族树方法可能如下所示:

public class SwaggerMessage {

    public static final String SC_OK = "OK";
    public static final String SC_CREATED = "Created";
    public static final String SC_ACCEPTED = "Accepted";
    public static final String SC_NO_CONTENT = "No Content";

    public static final String SC_MOVED_PERMANENTLY = "Moved Permanently";

    public static final String BAD_REQUEST = "Bad Request";
    public static final String SC_UNAUTHORIZED = "Unauthorized";
    public static final String SC_FORBIDDEN = "Forbidden";
    public static final String SC_NOT_FOUND = "Not Found";

    public static final String SC_INTERNAL_SERVER_ERROR = "Server Error";
    public static final String SC_NOT_IMPLEMENTED = "Not Implemented";
    public static final String SC_SERVICE_UNAVAILABLE = "Service Unavailable";

}

<强>输出

enter image description here