不使用递归/堆栈的树遍历(C#)?

发布于 2024-07-26 09:18:20 字数 775 浏览 1 评论 0原文

我正在使用 WPF,正在开发一个复杂的用户控件,它由具有丰富功能的树等组成。 为此,我使用了视图模型设计模式,因为某些操作无法直接在 WPF 中实现。 所以我采用 IHierarchyItem (这是一个节点并将其传递给此构造函数以创建树结构)

private IHierarchyItemViewModel(IHierarchyItem hierarchyItem, IHierarchyItemViewModel parent)
        {
            this.hierarchyItem = hierarchyItem;
            this.parent = parent;    

            List<IHierarchyItemViewModel> l = new List<IHierarchyItemViewModel>();
            foreach (IHierarchyItem item in hierarchyItem.Children)
            {
                l.Add(new IHierarchyItemViewModel(item, this));
            }
            children = new ReadOnlyCollection<IHierarchyItemViewModel>(l);
        }

问题是此构造函数大约需要 3 秒! 我的双核上有 200 个项目。 我做错了什么或者递归构造函数调用那么慢吗? 非常感谢!

I'm working with WPF and I'm developing a complex usercontrol, which is composed of a tree with rich functionality etc.
For this purpose I used a View-Model design pattern, because some operations couldn't be achieved directly in WPF. So I take the IHierarchyItem (which is a node and pass it to this constructor to create a tree structure)

private IHierarchyItemViewModel(IHierarchyItem hierarchyItem, IHierarchyItemViewModel parent)
        {
            this.hierarchyItem = hierarchyItem;
            this.parent = parent;    

            List<IHierarchyItemViewModel> l = new List<IHierarchyItemViewModel>();
            foreach (IHierarchyItem item in hierarchyItem.Children)
            {
                l.Add(new IHierarchyItemViewModel(item, this));
            }
            children = new ReadOnlyCollection<IHierarchyItemViewModel>(l);
        }

The problem is that this constructor takes about 3 seconds !! for 200 items on my dual-core.
Am I doing anythig wrong or recursive constructor call is that slow?
Thank you very much!

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

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

发布评论

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

评论(2

不爱素颜 2024-08-02 09:18:20

好吧,我自己找到了一个非递归版本,尽管它使用了堆栈。
它遍历整棵树:

Stack<MyItem> stack = new Stack<MyItem>();

stack.Push(root);

while (stack.Count > 0)
{
    MyItem taken = stack.Pop();

    foreach (MyItem child in taken.Children)                
       stack.Push(MyItem);                    

}

OK I found a non recursive version by myself, although it uses the Stack.
It traverses the whole tree:

Stack<MyItem> stack = new Stack<MyItem>();

stack.Push(root);

while (stack.Count > 0)
{
    MyItem taken = stack.Pop();

    foreach (MyItem child in taken.Children)                
       stack.Push(MyItem);                    

}
夏天碎花小短裙 2024-08-02 09:18:20

树的递归实现应该没有什么问题,特别是对于如此少量的项目。 递归实现有时空间效率较低,时间效率也稍差,但代码的清晰度通常可以弥补这一点。

对构造函数执行一些简单的分析对您很有用。 使用以下建议之一:http://en.csharp-online.net/Measure_execution_time你可以自己指出每件作品需要多长时间。

有可能某件作品需要很长时间。 无论如何,这可能会帮助你缩小你真正花时间的地方。

There should be nothing wrong with a recursive implementation of a tree, especially for such a small number of items. A recursive implementation is sometimes less space efficient, and slightly less time efficient, but the code clarity often makes up for it.

It would be useful for you to perform some simple profiling on your constructor. Using one of the suggestions from: http://en.csharp-online.net/Measure_execution_time you could indicate for yourself how long each piece is taking.

There is a possibility that one piece in particular is taking a long time. In any case, that might help you narrow down where you are really spending the time.

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