gpt4 book ai didi

c# - 从人树生成团队树的正确递归方法是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:08 25 4
gpt4 key购买 nike

我有一个具有以下属性的 Person 对象

public class Person
{
public string FirstName;
public string LastName;
public string TeamName;
public Person Manager;
public IEnumerable<Person> DirectReports;
}

所以假设我可以通过递归循环每个人的直接报告和他们的直接报告等来创建人员层次结构,就像这样:

foreach (Person direct in person.DirectReports)
{
foreach (Person subDirect in direct.DirectReports)
{
etc . . .
}
}

根据这些数据,我现在正在尝试转换以生成 Teams 的层次结构,所以我有这样的东西:

 public class Team
{
public string TeamName;
public IEnumerable<Team> SubTeams;
public IEnumerable<Person> PeopleInTeam;
}

请注意,如果一个人的直接下属可能具有也可能不具有相同的团队名称,那么人员层次结构中的人员级别数不一定与团队层次结构中的级别数相同

例如:

| Person| ParentPerson | TeamName   |  | Bill  |  ""          | Management |  | Joe   |  Bill        | Management |  | Scott |  Bill        | Marketing  |  | Jim   |  Bill        | Technology |   | Mark  |  Scott       | Marketing  || Bob   |  Joe         | Marketing  |

so after the conversion, I would have a team called "Management" with 2 people in it. It would have 2 items in the SubTeams array (one for Marketing and one for Technology). each of those teams would have no entries in the SubTeams array

What would be the best way to efficient take this hierarchy of people and convert it into a hierarchy of Teams? I have listed the code below but it seems to be failing. Right now, I am looping through each person and their direct reports and creating a dictionary of teams and adding each person at a time onto a team but it seems quite slow. here is an example ..

 Dictionary<string, Team> teams = new Dictionary<string, Team>();

foreach (Person direct in person.DirectReports)
{
if (teams.ContainsKey(direct.TeamName)
{
var team = teams[direct.TeamName];
team.People.Add(direct);
}
else
{
var team = new Team(){TeamName = direct.TeamName};
team.People.Add(direct);
teams[direct.TeamName] = team;
}
foreach (Person subDirect in direct.DirectReports)
{
etc . . .
}
}

最佳答案

这是一个可以用作框架的基本解决方案。

这并不理想:特别是,Dictionary<string, string>不是一个好的数据结构,因为无论如何我们最终都会遍历所有值。构建类似 Dictionary<string, List<string>> 的东西会更有用。从部门映射到其子部门的名称。

但它应该为您提供工作的基础。

void Main()
{
var bill = new Person { FirstName = "Bill", TeamName = "Management" };
var joe = new Person { FirstName = "Joe", Manager = bill, TeamName = "Management" };
var scott = new Person { FirstName = "Scott", Manager = bill, TeamName = "Marketing" };
var jim = new Person { FirstName = "Jim", Manager = bill, TeamName = "Technology" };
var mark = new Person { FirstName = "Mark", Manager = scott, TeamName = "Marketing" };
var bob = new Person { FirstName = "Bob", Manager = joe, TeamName = "Marketing" };
var ted = new Person { FirstName = "Ted", Manager = jim, TeamName = "IT Support" };

var people = new[] { bill, joe, scott, jim, mark, bob, ted };

var teamParents = people.Select (p => new { Team = p.TeamName, ParentTeam = p.Manager == null ? null : p.Manager.TeamName });

// don't let a team be its own parent
teamParents = teamParents.Where (p => !p.Team.Equals(p.ParentTeam));

// make sure they're all unique
teamParents = teamParents.Distinct();

// put it in a dictionary
var teamHierarchy = teamParents.ToDictionary (p => p.Team, q => q.ParentTeam);

foreach (string root in teamHierarchy.Where (h => h.Value == null).Select (h => h.Key))
{
PrintSubteams(teamHierarchy, 0, root);
}
}

private void PrintSubteams(Dictionary<string, string> hierarchy, int level, string root)
{
for (int i = 0; i < level; i++)
{
Console.Write(" ");
}

Console.WriteLine(root);

foreach (string child in hierarchy.Where (h => h.Value == root).Select(h => h.Key))
{
PrintSubteams(hierarchy, level + 1, child);
}
}

public class Person
{
public string FirstName;
public string LastName;
public string TeamName;
public Person Manager;
public IEnumerable<Person> DirectReports;
}

输出如下:

Management    Marketing    Technology        IT Support

(我添加了 IT 支持团队以使其更有趣。)

关于c# - 从人树生成团队树的正确递归方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19074270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com