2

I have the following class:

class Person 
{
    public string Name { get; set; }
    public List<Person> Children { get; set; } 
}  

And I created an instance of this class, fox ex.

Person pers = new Person();

I can code foreach loops statically and store the Children in some list:

foreach (var item in pers.Children)
{
    List<Person> r = item.Children;
    foreach (var item2 in item.Children)
    {
        List<Person> k = item2.Children;
        foreach (var item3 in item2.Children)
        {
        etc..
        }
    }
}

The problem is, how to code the foreach loops, if the "pers" has a children, and one of his children (pers.Children) also has a children. It's something like genealogical tree.

I mean, how to make it more dynamically? Because I don't know how the structure of the family can be.

Edit: I just want to show all the successors of one person. For example just names of his successors

Black Frog
  • 11,595
  • 1
  • 35
  • 66
kamyk
  • 295
  • 8
  • 25

5 Answers5

2

Use recursion :

public static void ShowAll(Person pers)
{
    Console.WriteLine(pers.Name);
    foreach (var item in pers.Children)
    {
        ShowAll(item);
    }
}
Guillaume
  • 12,824
  • 3
  • 40
  • 48
1

Another way you can do this is to add a method to your class which will show the person's name (and/or other data) and then display their children's names (and/or other data) by calling the method on each child instance.

The nice thing about this approach is that it makes your code much cleaner and leaves it to the Person class to handle how it displays itself (and it's successors).

Here's an example of your class modified with such a method:

public class Person
{
    public string Name { get; set; }
    public List<Person> Children { get; set; }

    public Person()
    {
        Children = new List<Person>();
    }

    public void DisplaySuccessors(int indendationLevel = 0)
    {
        Console.WriteLine("{0}{1}", new String('.', indendationLevel * 2), Name);

        foreach (var child in Children)
        {
            child.DisplaySuccessors(indendationLevel + 1);
        }
    }
}

And here's how you could use it:

private static void Main()
{
    var mother = new Person {Name = "Mother"};

    var daughter1 = new Person {Name = "daughter1"};
    var granddaughter1 = new Person { Name = "granddaughter1" };

    var son1 = new Person { Name = "son1" };
    var grandson1 = new Person { Name = "grandson1" };
    var granddaughter2 = new Person { Name = "granddaughter2" };

    mother.Children.Add(daughter1);
    mother.Children.Add(son1);
    daughter1.Children.Add(granddaughter1);
    son1.Children.Add(grandson1);
    son1.Children.Add(granddaughter2);

    mother.DisplaySuccessors();
}

Output:

Mother
..daughter1
....granddaughter1
..son1
....grandson1
....granddaughter2

Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

Handling tree like data structures has already been intensively discussed: Tree Data Structure

Community
  • 1
  • 1
Michael Sander
  • 2,677
  • 23
  • 29
0

One way is recursion, like this method which calls itself:

static void WriteAllSuccessors(Person pers, int depth = 0)
{
  var indent = new string(' ', depth * 4);
  Console.WriteLine(indent + pers.Name);
  foreach (var child in pers.Children)
      WriteAllSuccessors(child, depth + 1);
}

Note the indentation.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
0

This is standard recursion, where you call itself and add nodes to some list:

static void Main(string[] args)
        {
            List<Person> successors = new List<Person>();

            var person = new Person()
            {
                Name = "GrandFather",
                Children = new List<Person>()
                {
                    new Person()
                    {
                        Name = "Son",
                        Children = new List<Person>()
                        {
                            new Person()
                            {
                                Name = "SonOfSon"
                            }

                        }
                    },
                    new Person()
                    {
                        Name = "Daughter",
                        Children = new List<Person>()
                        {
                            new Person()
                            {
                                Name = "DaughterOfDaughter"
                            }
                        }
                    }
                }
            };

            SearchForSuccessors(person.Children, successors);
        }

        public static void SearchForSuccessors(List<Person> persons, List<Person> successors)
        {
            foreach(var person in persons )
            {
                successors.Add(person);

                if (person.Children != null)
                    SearchForSuccessors(person.Children, successors);
            }
        }
Giorgi Nakeuri
  • 35,155
  • 8
  • 47
  • 75