我如何从一个给定的父节点所有的孩子?(How do I get all children from

2019-07-30 03:18发布

我有父/子ID的列表,并希望得到所有子ID为给定的父ID。 有没有空父母(顶级ID不出现子ID)。

目前的父/子ID被记录在列表中的KeyValuePair,但是这可以很容易地改变,如果这将是更好的另一种数据结构:

List<KeyValuePair<int, int>> groups = new List<KeyValuePair<int, int>>();
groups.Add(new KeyValuePair<int,int>(parentID, childID));

例如,这里有样品父母/子女。 母27的孩子会是5944,2065,2066,2067,6248,6249,6250。

Parent  Child
27      1888
1888    5943
1888    5944
5943    2064
5943    2065
5943    2066
5943    2067
2064    6248
2064    6249
2064    6250

任何帮助将不胜感激!

Answer 1:

你为什么不改变的类型Dictionary<int, List<int>> ,如果母公司是键和值(整数列表)是孩子们?

然后,你会回到使用儿童名单:

    private List<int> GetAllChildren(int parent)
    {
        List<int> children = new List<int>();
        PopulateChildren(parent, children);
        return children;
    }

    private void PopulateChildren(int parent, List<int> children)
    {
        List<int> myChildren;
        if (myitems.TryGetValue(parent, out myChildren))
        {
            children.AddRange(myChildren);
            foreach (int child in myChildren)
            {
                PopulateChildren(child, children);
            }
        }
    }

您将需要进行重量对性能的影响,因为这将加快读取和减慢写入(有大部分的时间没人甚至会发现)。

您还需要检查,如果该列表是在使用字典myitems.TryGet(...)如果没有,则需要创建它,但这是O(1),所以实际上是瞬间。

private static void AddEntry(int parent, int child)
{
    List<int> children;
    if (!myitems.TryGetValue(parent, out children))
    {
        children = new List<int>();
        myitems[parent] = children;
    }
    children.Add(child);
}


Answer 2:

这很简单。 试想,你有下面的数组列表

    List<KeyValuePair<int, int>> groups = new List<KeyValuePair<int, int>>();
    groups.Add(new KeyValuePair<int, int>(27, 1888));
    groups.Add(new KeyValuePair<int, int>(1888, 5943));
    groups.Add(new KeyValuePair<int, int>(1888, 5944));
    groups.Add(new KeyValuePair<int, int>(5943, 2064));
    groups.Add(new KeyValuePair<int, int>(5943, 2065));
    groups.Add(new KeyValuePair<int, int>(5943, 2066));
    groups.Add(new KeyValuePair<int, int>(5943, 2067));
    groups.Add(new KeyValuePair<int, int>(2064, 6248));
    groups.Add(new KeyValuePair<int, int>(2064, 6249));
    groups.Add(new KeyValuePair<int, int>(2064, 6250));
    groups.Add(new KeyValuePair<int, int>(2000, 1000));
    // Pass the 1st parameter as the parent to get all children
    List<int> childs = GetAllChild(27, groups);

你需要使用一个“递归函数”来动态地让孩子们。 只需拨打下面的方法来获得父母的所有儿童

public List<int> GetAllChild(int id,List<KeyValuePair<int, int>> newLst)
{
      List<int> list = new List<int>();
      for (int i = 0; i < newLst.Count; i++)
      {
            if (Convert.ToInt32(newLst[i].Key) == id)
            {
                 if (!list.Contains(Convert.ToInt32(newLst[i].Value)))
                 {
                     list.Add(Convert.ToInt32(newLst[i].Value));
                     List<int> l = GetAllChild(newLst[i].Value, newLst);
                     list.AddRange(l);
                 }
            }
       }
       return list;
}


文章来源: How do I get all children from a given parent node?