C# I';我在用c语言创建族谱时遇到了麻烦#

C# I';我在用c语言创建族谱时遇到了麻烦#,c#,tree,C#,Tree,这就是我所拥有的,一个人和他们的父母和孩子一起上课。我想创建一个方法,用一个人的亲属和他们与任何给定的人的“亲密程度”创建一个字典,使用他们与主题的密切关系。我不明白的是,在一个人有活着的父母和孩子的情况下,我应该如何迭代一个人的所有内容,这就是你进来的地方,我该如何做这种新型的上下迭代。无论如何,这是我到目前为止的代码 using System; using System.Collections.Generic; namespace familytree{ public stati

这就是我所拥有的,一个人和他们的父母和孩子一起上课。我想创建一个方法,用一个人的亲属和他们与任何给定的人的“亲密程度”创建一个字典,使用他们与主题的密切关系。我不明白的是,在一个人有活着的父母和孩子的情况下,我应该如何迭代一个人的所有内容,这就是你进来的地方,我该如何做这种新型的上下迭代。无论如何,这是我到目前为止的代码

using System;
using System.Collections.Generic;

namespace familytree{

    public static Random rand = new Random();

    public class Program{

        public static void main(string[] args){


        }

        public static void relatives(Person subject){
            // help

        }

    }



    public class Person{

        public readonly bool gender;
        public string firstname;
        public string lastname;
        public Person[] parents = new Person[2];
        public List<Person> children = new List<Person>();

        public Person(Person mother, Person father){

            this.gender = rand.NextDouble() >= 0.5;
            this.parents[0] = mother;
            this.parents[1] = father;
            this.firstname = "firstname";
            this.lastname = father.lastname + " - " + mother.lastname;

        }
    }

}
使用系统;
使用System.Collections.Generic;
名称空间家族树{
public static Random rand=new Random();
公共课程{
公共静态void main(字符串[]args){
}
公共静态无效亲属(个人受试者){
//帮助
}
}
公共阶层人士{
公共只读文件;
公共字符串名;
公共字符串lastname;
公众人物[]父母=新人[2];
public List children=new List();
公众人士(个人母亲、个人父亲){
this.gender=rand.NextDouble()>=0.5;
这个。父母[0]=母亲;
这个。父母[1]=父亲;
this.firstname=“firstname”;
this.lastname=father.lastname+“-”+mother.lastname;
}
}
}

好的,所以基本上你必须理解每个“个人节点”与其他个人节点、父母、配偶、子女和兄弟姐妹(我们可以称之为父母、子女,不包括自己)有4种关系。现在我们可以使用递归方法遍历Person节点的所有亲属,如下所示:

        public static bool contains<T>(List<T> main, T sub) {

            foreach (T i in main) {
                if (i.Equals(sub)) {
                    return true;
                }
            }

            return false;
        }

        public static List<Person> relatives(Person person, List<Person> people) {

            // Spouses -> Children -> Siblings -> Parents
            // If Dead do not add to list but still iterate through

            if (person.alive) {
                people.Add(person);
            }

            // Spouces
            if (person.spouses.Count > 0) {
                foreach (Person spouse in person.spouses) {

                    if (spouse.alive) {
                        people.Add(spouse);
                    }

                    if (!contains<Person>(people, spouse)) people.AddRange(relatives(spouse, people));
                }
            }

            // Children
            if (person.children.Count > 0) {
                foreach (Person child in person.children) {

                    if (child.alive) {
                        people.Add(child);
                    }

                    if (!contains<Person>(people, child)) people.AddRange(relatives(child, people));
                }
            }

            // Parents
            if (person.parents[0] != null && person.parents[1] != null) {
                foreach (Person parent in person.parents) {

                    if (parent.alive) {
                        people.Add(parent);
                    }

                    if (!contains<Person>(people, parent)) people.AddRange(relatives(parent, people));

                    foreach (Person child in parent.children) {

                        if (child.alive) {
                            people.Add(child);
                        }

                        if (!contains<Person>(people, child)) people.AddRange(relatives(child, people));
                    }
                }
            }

            List<Person> family = new List<Person>();
            foreach (Person i in people) {
                bool copy = false;
                foreach (Person j in family) {
                    if (j == i) {
                        copy = true;
                    }
                }
                if (copy == false) {
                    family.Add(i);
                }
            }

            return family;
        }
    }
public static bool contains(列表main,T sub){
foreach(主界面中的ti){
如果(i.Equals(sub)){
返回true;
}
}
返回false;
}
公共静态列表亲属(人,列表人){
//配偶->子女->兄弟姐妹->父母
//如果死了,不要添加到列表中,但仍要遍历
如果(人活着){
人。添加(人);
}
//喷口
如果(person.partners.Count>0){
foreach(个人配偶本人。配偶){
如果(配偶活着){
添加(配偶);
}
如果(!包含(人,配偶))people.AddRange(亲属(配偶,人));
}
}
//孩子们
如果(person.children.Count>0){
foreach(人-子-人-子-子){
如果(孩子活着){
添加(儿童);
}
如果(!contains(people,child))people.AddRange(亲戚(child,people));
}
}
//父母
if(person.parents[0]!=null&&person.parents[1]!=null){
foreach(个人家长亲自。家长){
if(parent.alive){
添加(家长);
}
如果(!contains(people,parent))people.AddRange(亲戚(parent,people));
foreach(父项中的个人子项。子项){
如果(孩子活着){
添加(儿童);
}
如果(!contains(people,child))people.AddRange(亲戚(child,people));
}
}
}
列表族=新列表();
foreach(人中的人i){
bool copy=false;
foreach(家庭中的j人){
如果(j==i){
复制=真;
}
}
如果(复制==false){
家庭。添加(i);
}
}
回归家庭;
}
}

您能分享示例输入和预期输出吗?所以我只想列出受试者的所有亲属?(我想我应该更改亲戚方法中的空白以反映这一点)