【问题标题】:Generating a 2D array of numbers nested in a dictionary生成嵌套在字典中的二维数字数组
【发布时间】:2019-02-25 06:43:32
【问题描述】:

我有一本字典,其中包含一些数字键。每个键都有一组值,其中包含“嵌套”在该特定键中的数字(此数据已从数据库中提取)。

一些例子:

  • 825 里面有 838,838 里面有 2941,2941 里面有 556。所以有 4 个嵌套级别。

  • 825 里面有 27 个,这只是一个嵌套级别。

  • 23 嵌套在 838 内(2 个嵌套级别),但 23 内有 66 个嵌套级别。

示例结构:

dictionary {
    825 : [838, 27],
    838 : [2941, 23],
    2941 : [556, 612],
    23 : [66]
}

我已经编写了一些代码,可以将数字嵌套在其他数字中,因为其中一项要求是了解嵌套的“深度”。

// example: call function with key: 825
public int getDepth(int number, Dictionary<int, List<int>> nestedNumbers, int depth)
{
    // 825 is in nestedNumbers
    if (nestedNumbers.Keys.Contains(number))
    {
        // foreach number in 825 [838, 27]
        foreach (var currentNumber in nestedNumbers[number])
        {
            // depth is now level 2
            depth++;
            // call the function again but with 838, which will now get nested groups in 838 [2941, 23]
            return getDepth(currentNumber, nestedNumbers, depth);
        }
    }
    return depth;
}

我需要制作一个包含所有嵌套级别的列表(或数组列表),如下所示:

lvl1  lvl2  lvl3  lvl4
[[825, 838, 2941, 556],
[825, 27],   
[825, 838, 23, 66],
[825, 838, 2941, 612]]  <-- e.g. 612 is in 2941, 2941 is in 838, 838 is in 825

但是根据我已经编写的函数,我不确定如何去做。有谁知道我该如何实现这一目标?

【问题讨论】:

  • 有错字吗?您的函数名为getDepth(),但您在方法中调用GetDepth。这是不同的方法还是错字?
  • 您的depth 计算不准确。不要在循环内部增加——这会增加列表大小的深度。致电getDepth(currentGroup, nestedNumbers, depth + 1);。此外,在可能有多个路由到叶子的情况下,取这些 getDepth 调用中的最大值。
  • 抱歉@maccettura 它是递归的,我忘了在 getDepth 上添加返回
  • 啊,我明白你的意思了,它总是会落后一个深度级别
  • 你的权利我的输出不是很清楚。我已经更新了它。我认为现在更清楚我想要实现的目标

标签: c# arrays dictionary tree nested


【解决方案1】:

这是一个tree traversal 问题。在tree 术语中,我们想要从树根到叶子的所有路径,或者没有子节点的节点。这是你的树的可视化:

       825
      /   \
     838   27
    /  \
  2941  23 
  /  \   \
556  612  66

可以使用stackqueue 数据结构(递归是基于堆栈的)来执行遍历。在这个例子中我选择使用队列。

遍历部分包括将当前节点的子节点入队并使用字典来跟踪每个子节点的父节点,然后将子节点逐个出列并重复该过程,直到到达叶节点。一旦发现这样的节点,就可以通过返回树直到到达根并将路径添加到结果列表中来构建返回根的路径。

using System;
using System.Collections.Generic;

class MainClass {
    public static void Main(string[] args) {
        Dictionary<int, List<int>> tree = new Dictionary<int, List<int>>()
        {
            {825, new List<int> {838, 27}},
            {838, new List<int> {2941, 23}},
            {2941, new List<int> {556, 612}},
            {23, new List<int> {66}},
        };

        foreach (var row in FindPathsToLeaves(825, tree))
        {
            foreach (var cell in row) 
            {
                Console.Write(cell + " ");
            }

            Console.WriteLine();
        }
    }

    static List<List<int>> FindPathsToLeaves(int root, Dictionary<int, List<int>> tree)
    {
        List<List<int>> paths = new List<List<int>>();
        Dictionary<int, int> parent = new Dictionary<int, int>();
        parent[root] = -1;
        Queue<int> q = new Queue<int>();
        q.Enqueue(root);

        while (q.Count > 0) 
        {
            int current = q.Dequeue();

            if (tree.ContainsKey(current))
            {
                foreach (int n in tree[current])
                {
                    parent[n] = current;
                    q.Enqueue(n);
                }
            } 
            else
            {
                List<int> path = new List<int>();

                while (parent.ContainsKey(current))
                {
                    path.Insert(0, current);
                    current = parent[current];
                }

                paths.Add(path);
            }
        }

        return paths;
    }
}

输出:

825 27 
825 838 2941 556 
825 838 2941 612 
825 838 23 66 

【讨论】:

    猜你喜欢
    • 2016-12-10
    • 2015-12-22
    • 2016-02-10
    • 2022-06-13
    • 1970-01-01
    • 2022-01-14
    • 2021-12-02
    • 2011-07-07
    • 1970-01-01
    相关资源
    最近更新 更多