Silverlight 中的递归函数

发布于 2024-09-11 07:45:49 字数 1770 浏览 1 评论 0原文

我在我的 c# silverlight 应用程序中有以下函数来查找树中节点的总子节点,

        //get total children
    private int getTotalChildren(int id) 
    {
        int total=0;
        for (int i = 0; i < persons.Count;i++ )
        {
            if (persons[i].Manager == id)
            {
                total += 1;
                total += getTotalChildren(persons[i].Id);
            }
        }
        return total;
    }

行 Total += getTotalChildren(persons[i].id) 使浏览器窗口在我运行时自动关闭(我是猜测这是 silverlights 崩溃的方式?)在 IDE 中我没有收到任何错误。

编辑:我不明白它怎么可能是无限递归,因为没有人将自己作为管理者。 people 是使用此 xml 构建的列表

<?xml version="1.0" ?> 
<Persons>
    <Person>
        <Id>1</Id>
        <Name>temp</Name>
        <Qlid>1234</Qlid>
        <Manager>0</Manager>
    </Person>
    <Person>
        <Id>2</Id>
        <Name>someone</Name>
        <Qlid>5678</Qlid>
        <Manager>1</Manager>
    </Person>
    <Person>
        <Id>3</Id>
        <Name>wefwef</Name>
        <Qlid>3333</Qlid>
        <Manager>1</Manager>
    </Person>
    <Person>
        <Id>4</Id>
        <Name>batman</Name>
        <Qlid>6723</Qlid>
        <Manager>3</Manager>
    </Person>
    <Person>
        <Id>5</Id>
        <Name>batman</Name>
        <Qlid>6723</Qlid>
        <Manager>3</Manager>
    </Person>
</Persons>

edit2:好吧,对不起,伙计们,这真的很愚蠢。这是一个循环,我以为我已经在桌面上创建了 xml 文件的快捷方式,但实际上却意外地创建了一个副本。当我编辑副本时,程序正在读取的文件中,人员 1 将人员 3 作为其经理,人员 3 将人员 1 作为经理

I have the following function in my c# silverlight application to find the total sub nodes of a node in the tree

        //get total children
    private int getTotalChildren(int id) 
    {
        int total=0;
        for (int i = 0; i < persons.Count;i++ )
        {
            if (persons[i].Manager == id)
            {
                total += 1;
                total += getTotalChildren(persons[i].Id);
            }
        }
        return total;
    }

the line total += getTotalChildren(persons[i].id) is making the browser windows auto close when i run it (i am guessing thats silverlights way of crashing?) in the IDE I don't get any errors.

edit: I don't see how it could be infinite recursion since there is no person which has itself as manager. persons is a List built using this xml

<?xml version="1.0" ?> 
<Persons>
    <Person>
        <Id>1</Id>
        <Name>temp</Name>
        <Qlid>1234</Qlid>
        <Manager>0</Manager>
    </Person>
    <Person>
        <Id>2</Id>
        <Name>someone</Name>
        <Qlid>5678</Qlid>
        <Manager>1</Manager>
    </Person>
    <Person>
        <Id>3</Id>
        <Name>wefwef</Name>
        <Qlid>3333</Qlid>
        <Manager>1</Manager>
    </Person>
    <Person>
        <Id>4</Id>
        <Name>batman</Name>
        <Qlid>6723</Qlid>
        <Manager>3</Manager>
    </Person>
    <Person>
        <Id>5</Id>
        <Name>batman</Name>
        <Qlid>6723</Qlid>
        <Manager>3</Manager>
    </Person>
</Persons>

edit2: ok sorry guys, it was something really stupid . It was a circular loop I thought I had made a shortcut to the xml file on my desktop but instead accidentaly made a copy. person 1 had person 3 as its manager who had person 1 as manager in the file the program was reading while I was editing the copy

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

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

发布评论

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

评论(4

柠栀 2024-09-18 07:45:49

[推测] 可能是无限递归引起的堆栈溢出导致崩溃。您是否包含其经理是 id 本身的成员?这将导致递归永远不会结束并导致堆栈溢出。

private int getTotalChildren(int id) 
{
    int total=0;
    for (int i = 0; i < persons.Count;i++ )
    {
        if (persons[i].Manager == id)
        {
            total += 1;
            if(persons[i].Manager != persons[i].Id)
            {
                total += getTotalChildren(persons[i].Id);
            }
        }
    }
    return total;
}

[Speculation] You may be crashing due to a stack overflow caused by infinite recursion. Does your contain a member whose manager is the id itself? This would cause your recursion to never end and lead to a stack overflow.

private int getTotalChildren(int id) 
{
    int total=0;
    for (int i = 0; i < persons.Count;i++ )
    {
        if (persons[i].Manager == id)
        {
            total += 1;
            if(persons[i].Manager != persons[i].Id)
            {
                total += getTotalChildren(persons[i].Id);
            }
        }
    }
    return total;
}
遇见了你 2024-09-18 07:45:49

您没有在任何地方指定子节点。你总是看人,这一点不会改变。

You aren't specifying the child nodes anywhere. You always look at persons, which isn't changing.

英雄似剑 2024-09-18 07:45:49

在内部,Silverlight 有一个“递归限制”,旨在帮助避免冻结用户浏览器的代码。如果您在 Google 上搜索“Silverlight recursion”或类似内容,您会发现一些链接。

我对此了解不多,但我的猜测是您只需要以不同的方式执行此操作。

Internally, Silverlight has a "recursion limit" designed to help avoid code that freezes the user's browser. If you Google around for "Silverlight recursion" or similar you'll find a few links.

I don't know a ton about it, but my guess is that you'll simply need to do this a different way.

我喜欢麦丽素 2024-09-18 07:45:49

专业提示:考虑使用 for/while 循环,而不是在像这样的简单场景中产生与递归相关的帧内存开销。

请记住,使用回避可以完成的所有事情都可以在没有它的情况下完成。经验丰富的开发人员知道何时为了可读性而牺牲性能。

Protip: consider a for/while loop instead of incurring the frame memory overhead associated with recusion for simple scenarios like this.

Remember, everything you can accomplish with recusion can be accomplished without it. A seasoned developer knows when to sacrifice performance for readability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文