递归与yield返回树中的元素顺序

发布于 2025-01-02 16:26:40 字数 585 浏览 3 评论 0原文

我有一个递归函数,在给定起始根节点的情况下返回所有子树节点。

private IEnumerable<Node> getAllNodesRecursively(Node subnode)
{
    foreach (Node node in subnode.Nodes)
        getAllNodesRecursively(node);

    yield return subnode;
}

对于以下树结构:

A
|
+--B
|
+--C
|  |
|  +--D
|
+--E

当我尝试这样迭代时:

foreach (Node n in getAllNodesRecursively(a))
{
    Console.WriteLine(n);
}

该函数仅返回 A 值。

我希望将yield-return与递归结合使用并检索预购中的元素(本例中为A、B、C、D、E)。

(如果我将收益返回放在 foreach 之前,则 foreach 将永远不会发生)。

这可能吗?

I have a recursive function that returns all subtree nodes, given the starting root node.

private IEnumerable<Node> getAllNodesRecursively(Node subnode)
{
    foreach (Node node in subnode.Nodes)
        getAllNodesRecursively(node);

    yield return subnode;
}

For the following tree structure:

A
|
+--B
|
+--C
|  |
|  +--D
|
+--E

When I try to iterate as such:

foreach (Node n in getAllNodesRecursively(a))
{
    Console.WriteLine(n);
}

the function returns the only the A value.

I wish to use yield-return with recursion and retrieve elements in the Preorder (A, B, C, D, E in this example).

(If I put the yield return before the foreach, the foreach would never happen).

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我乃一代侩神 2025-01-09 16:26:40

您是否尝试过类似的操作:

private IEnumerable<Node> getAllNodesRecursively(Node subnode) 
{ 
    // Return the parent before its children
    yield return subnode; 

    foreach (Node node in subnode.Nodes) 
    {
        foreach(Node n in getAllNodesRecursively(node))
        {
            yield return n;
        }
    }
} 

您的实现正在递归调用 getAllNodesRecursively,但忽略其返回值。

Have you tried something like:

private IEnumerable<Node> getAllNodesRecursively(Node subnode) 
{ 
    // Return the parent before its children
    yield return subnode; 

    foreach (Node node in subnode.Nodes) 
    {
        foreach(Node n in getAllNodesRecursively(node))
        {
            yield return n;
        }
    }
} 

Your implementation is calling getAllNodesRecursively recursively, but ignoring its return value.

许仙没带伞 2025-01-09 16:26:40

是的,这是可能的,只需将 yield return 放在 foreach 之前即可。您正在考虑正常 return 语句的行为。

Yes it's possible, just put the yield return before the foreach. You are thinking of the behaviour of a normal return statement.

东北女汉子 2025-01-09 16:26:40

您需要显式迭代+yield返回每个节点ala的子节点:

        public IEnumerable<int> preOrder(Node root)
        {
            if (root == null)
                yield break;

            yield return root.val;

            if (root.left != null)
                foreach (int i in preOrder(root.left))
                    yield return i;

            if (root.right != null)
                foreach (int i in preOrder(root.right))
                    yield return i;
        }

You need to explicitly iterate + yield return the nodes of children of each node ala:

        public IEnumerable<int> preOrder(Node root)
        {
            if (root == null)
                yield break;

            yield return root.val;

            if (root.left != null)
                foreach (int i in preOrder(root.left))
                    yield return i;

            if (root.right != null)
                foreach (int i in preOrder(root.right))
                    yield return i;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文