【问题标题】:recursive function in collection集合中的递归函数
【发布时间】:2012-09-19 04:08:54
【问题描述】:

我正在使用 c# 和 List 集合并加载了值。完成后,我会尝试以递归方式阅读它们,但有些我无法做到这一点。

以下是我的主要代码。

    private static void Main(string[] args)
            {
                var node = new Node
                    {
                        Name = "N1",
                        Nodes =
                            new List<Node>
                                {
                                    new Node { Name = "N1a" },
                                    new Node { Name = "N1b", Nodes = new List<Node> { new Node { Name = "N1B1" } } },
                                    new Node
                                        {
                                            Name = "N1c",
                                            Nodes =
                                                new List<Node> { new Node { Name = "N1C1", Nodes = new List<Node> {new Node{Name = "N1C1A"} } } }
                                        }
                                }
                    };
                GetNodes( node );
                Console.ReadLine();
            }

 public class Node
        {
            public string Name { get; set; }

            public IList<Node> Nodes { get; set; }
        }

函数调用如下

public static IEnumerable<Node> GetNodes(Node node)
        {
            if (node == null)
            {
                return null;
            }

            Console.WriteLine(node.Name);

            foreach (var n in node.Nodes)
            {
                return GetNodes(n);
            }

            return null;
        }
    }  

谁能帮我修复递归函数?

【问题讨论】:

  • 好的,你的函数将在第一个节点上停止。所以,你实际上想做什么,获取节点,不会聚集节点,然后返回 all 它们在一个打..

标签: c# function recursion


【解决方案1】:

如果你只想打印所有节点的名称,

public static void GetNodes(Node node)
{
    if (node == null)
    {
        return;
    }
    Console.WriteLine(node.Name);
    foreach (var n in node.Nodes)
    {
        GetNodes(n);
    }
}

如果你想把树弄平,

public static IEnumerable<Node> GetNodes(Node node)
{
    if (node == null)
    {
        yield break;
    }
    yield return node;
    foreach (var n in node.Nodes)
    {
        foreach(var innerN in GetNodes(n))
        {
            yield return innerN;
        }
    }
}

【讨论】:

  • 我总是会添加健康警告,如果您存储使用 yield 的方法的结果,您只能每次迭代集合一次。
  • 你是什么意思?如果你调用var iterator = GetNodes(myNode);,你可以在没有问题的情况下多次迭代它。
  • 嗯...刚试过,你是对的。我敢肯定这曾经咬过我,但一定是其他问题。谢谢。
  • @Rawling 感谢您的回答,它运行良好 * 只有第一部分,或者我不知道。压平树对我不起作用。你能告诉我如何得到它吗?
  • 如果你真的想从第二个控制台输出,你需要像foreach(var n in GetNodes(node)) Console.WriteLine(n.Name);
【解决方案2】:
public static IEnumerable<Node> GetNodes(Node node)
{
    if (node == null) return null;

    var nodes = new List<Node>();
    nodes.Add(node);

    Console.WriteLine(node.Name);

    if (node.Nodes != null)
    {
        foreach (var n in node.Nodes)
        {
            nodes.AddRange(GetNodes(n));
        }
    }

    return nodes;
}}

【讨论】:

  • 嗨@Cuong,感谢您的回答,它运行良好。虽然由于代码量较少,我选择了第一个答案。
【解决方案3】:

您的方法只返回 null,或者调用自身,然后返回 null,或者调用自身.....所以在一天结束时它返回 null 或不终止。如果您想序列化这些值,您可以在将它们写入控制台时将它们写入代码中的同一点。

public static void GetNodes(Node node, List<Node> output)
{
    if (node == null)
        return;

    output.Add(node);
    Console.WriteLine(node.Name);

    foreach (var n in node.Nodes)
    {
        GetNodes(n, output);
    }
}

【讨论】:

  • 嗨@James,感谢您的回答,它运行良好。虽然由于代码量较少,我选择了第一个答案。
【解决方案4】:

在第一次迭代时,您会立即从循环内的方法返回。不执行其他迭代。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-23
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 2013-01-06
    • 2014-12-07
    • 2020-03-24
    相关资源
    最近更新 更多