c# - 创建新流程是否有助于我遍历一棵大树?

标签 c# tree-traversal

让我们把它想象成一个家谱,父亲有 child ,那些 child 有 child ,那些 child 有 child ,等等...... 所以我有一个递归函数,让父亲使用递归来获取 child ,现在只需将它们打印到调试输出窗口......但在某个时候(让它运行一小时并打印 26000 行后)它给了我StackOverFlowException。

那么我真的内存不足了吗?嗯?那我不应该得到一个“内存不足异常”吗? 在其他帖子上我发现人们说如果递归调用的次数太多,你可能仍然会得到一个 SOF 异常......

无论如何,我的第一个想法是将树分解成更小的子树..所以我知道我的根父亲总是有这五个 child ,所以与其调用我的方法一次并传递给它,不如我说好吧,通过 Kids of root Passes 调用它五次。我认为这对它有帮助......但其中一个仍然很大 - 当它崩溃时有 26000 行 - 并且仍然有这个问题。

应用程序域和在运行时在某个特定深度级别创建新进程怎么样?这有帮助吗?

如何创建我自己的 Stack 并使用它来代替递归方法?这有帮助吗?

这也是我的代码的高级部分,请看一下,也许这实际上有一些愚蠢的错误导致了 SOF 错误:

private void MyLoadMethod(string conceptCKI)
{

// make some script calls to DB, so that moTargetConceptList2 will have Concept-Relations for the current node. 

            // when this is zero, it means its a leaf. 
            int numberofKids = moTargetConceptList2.ConceptReltns.Count();
            if (numberofKids == 0)
                return;
            for (int i = 1; i <= numberofKids; i++)
            {
                oUCMRConceptReltn = moTargetConceptList2.ConceptReltns.get_ItemByIndex(i, false);

                //Get the concept linked to the relation concept
                if (oUCMRConceptReltn.SourceCKI == sConceptCKI)
                {
                    oConcept = moTargetConceptList2.ItemByKeyConceptCKI(oUCMRConceptReltn.TargetCKI, false);
                }
                else
                {
                    oConcept = moTargetConceptList2.ItemByKeyConceptCKI(oUCMRConceptReltn.SourceCKI, false);
                }

                //builder.AppendLine("\t" + oConcept.PrimaryCTerm.SourceString);
                Debug.WriteLine(oConcept.PrimaryCTerm.SourceString);

                MyLoadMethod(oConcept.ConceptCKI);
            }
        }

最佳答案

How about creating my own Stack and using that instead of recursive methods? does that help?

是的!

当您实例化 Stack<T> 时这将存在于中并且可以增长到任意大(直到您用完可寻址内存)。

如果您使用递归,您将使用调用堆栈。调用堆栈比堆小得多。默认为每个线程 1 MB 的调用堆栈空间。请注意,这可以更改,但不建议这样做。

关于c# - 创建新流程是否有助于我遍历一棵大树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11917806/

相关文章:

c# - 使用 Linq 搜索

C# WMPLib mp3 的持续时间

c# - 静态类和私有(private)构造函数

c# - Xamarin:适用于 Android 和 Windows UWP 的 Xamarin 表单中分组列表的垂直字母索引(跳转列表)

tree - 树遍历的时间复杂度是多少?

c# - 绘制二维热图

binary-tree - 标准 ML 二叉树遍历

javascript - 使用 React JSX 模拟 onFocus 并遍历节点

java - 打印二叉树的层序遍历,从左到右和从右到左交替

algorithm - BST的遍历