【问题标题】:How do I get all children from a given parent node?如何从给定的父节点获取所有子节点?
【发布时间】:2012-06-26 01:14:06
【问题描述】:

我有一个父/子 ID 列表,并希望获取给定父 ID 的所有子 ID。没有 null 父级(顶级 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

任何帮助将不胜感激!

【问题讨论】:

  • 您尝试了哪些方法,哪些方法无效?走过一棵树是相当标准的任务......所以现在有点不清楚问题是什么。

标签: c# .net algorithm recursion relationship


【解决方案1】:

为什么不更改Dictionary&lt;int, List&lt;int&gt;&gt; 的类型,其中父项是键,值(整数列表)是子项?

然后你会得到孩子的列表使用:

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

【讨论】:

  • 这并不能帮助我找到所有的孩子——它只会得到一个级别的孩子。
  • 好的,所以你想递归地得到所有的孩子?
  • 试试看...只需调用 GetAllChildren(27)
  • 这是一种享受。非常感谢,这很有意义:)
【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    相关资源
    最近更新 更多