Rajeevx5
Rajeevx5

Reputation: 37

Creating Family Tree Structure

I need to create a family tree structure using the code below. I can only add code in the ShowFamily method. I can return GrandDad Uncle Aunt and Dad. But for some reason i cannot return Me and Sister. Can any of you help i know it must be simple thank you all

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);
        }           
    }
}

Upvotes: 2

Views: 6559

Answers (2)

Enigmativity
Enigmativity

Reputation: 117175

You should change you class structure to this:

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

Then any Person can have children.

Now you can define your family like this:

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" },
            }
        }
    }
};

Then ShowFamily looks like this (in two methods):

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);
    }             
}

Now calling ShowFamily(myAncestor); will output this:

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

You can also go as deep as you want:

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" },
            }
        }
    }
};

Now ShowFamily gives this:

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

Upvotes: 3

Rufus L
Rufus L

Reputation: 37070

Here's another way you can do it using recursion (and also some indentation to show relationships). Because all the types are based on Person, we can use that type as the input, then check inside the method to see if the person is a Parent. This will allow you to have multiple extended trees drawn from the same method (where parents have children who are parents who have children who are parents, as well as siblings that are parents). Following your example, the parents have a asterisk * next to their name:

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);
    }
}

A more complex family tree method might look like:

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);
}

Output

enter image description here

Upvotes: 2

Related Questions