C# 大树迭代

发布于 2024-07-13 07:19:19 字数 2086 浏览 4 评论 0原文

我有一个以父/子关系组装的大型结果集。 我需要遍历树并将结果显示给用户。

我在使用递归之前已经完成了此操作,但因为我的结果集可能很大,所以我想避免接收 StackOverflowException 的可能性。

我在 MSDN 上找到了以下使用堆栈的 示例。 我遇到的问题是因为堆栈是后进先出,所以我的数据无法正确显示。 我希望它看起来像下面这样:


LeveL 1
Level 1.1
Level 1.1.1 
Level 1.1.2 
Level 1.2 
Level 1.2.1 
Level 1.2.2

但它看起来像:


LeveL 1
Level 1.2 
Level 1.2.2 
Level 1.2.1 
Level 1.1 
Level 1.1.2 
Level 1.1.1 

有什么想法吗?

这是我的代码的示例。 假设 DataTable dt 具有以下列:ID、ParentID 和 Text

    private struct Item
    {
        public string Text;
        public int ID;
        public int ParentID;
    }

    private void BuildView()
    {
        Stack<Item> itemTree = new Stack<Item>(40);

        //Get All Parent Nodes
        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentID = 0";

        //Add the parent nodes to the stack
        foreach (DataRowView drv in dv)
        {
            Item item = new Item();
            item.Text = drv["Text"].ToString();
            item.ID = drv["ID"].ToString();
            item.ParentID = drv["ParentID"].ToString();
            itemTree.Push(item);
        }

        //Go through the stack one node at a time
        while (itemTree.Count > 0)
        {
            Item currentItem = itemTree.Pop();
            Debug.WriteLine(currentItem.Text);

            //Get children of current node
            dv.RowFilter = String.Format("ParentID = {0}", currentItem.ID);
            if (dv.Count > 0)
            {
                //Add child nodes to the stack
                foreach (DataRowView drvChild in dv)
                {
                    Item item = new Item();
                    item.Text = drvChild["Text"].ToString();
                    item.ID = drvChild["ID"].ToString();
                    item.ParentID = drvChild["ParentID"].ToString();
                    itemTree.Push(item);
                }
            }
        }

    }

I have a large result set assembled in a parent/child relationship. I need to walk the tree and display the results to the user.

I've done this before using recursion, but because my result set may be large, I want to avoid the possibility of receiving a StackOverflowException.

I found the following example on MSDN which uses a Stack. The problem I'm having is because a stack is last-in first-out, my data doesn't appear properly. I'd like it to look like the following:


LeveL 1
Level 1.1
Level 1.1.1 
Level 1.1.2 
Level 1.2 
Level 1.2.1 
Level 1.2.2

But it looks like:


LeveL 1
Level 1.2 
Level 1.2.2 
Level 1.2.1 
Level 1.1 
Level 1.1.2 
Level 1.1.1 

Any ideas?

Here is an example of my code. Assuming the DataTable dt has the following columns: ID, ParentID, and Text

    private struct Item
    {
        public string Text;
        public int ID;
        public int ParentID;
    }

    private void BuildView()
    {
        Stack<Item> itemTree = new Stack<Item>(40);

        //Get All Parent Nodes
        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentID = 0";

        //Add the parent nodes to the stack
        foreach (DataRowView drv in dv)
        {
            Item item = new Item();
            item.Text = drv["Text"].ToString();
            item.ID = drv["ID"].ToString();
            item.ParentID = drv["ParentID"].ToString();
            itemTree.Push(item);
        }

        //Go through the stack one node at a time
        while (itemTree.Count > 0)
        {
            Item currentItem = itemTree.Pop();
            Debug.WriteLine(currentItem.Text);

            //Get children of current node
            dv.RowFilter = String.Format("ParentID = {0}", currentItem.ID);
            if (dv.Count > 0)
            {
                //Add child nodes to the stack
                foreach (DataRowView drvChild in dv)
                {
                    Item item = new Item();
                    item.Text = drvChild["Text"].ToString();
                    item.ID = drvChild["ID"].ToString();
                    item.ParentID = drvChild["ParentID"].ToString();
                    itemTree.Push(item);
                }
            }
        }

    }

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

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

发布评论

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

评论(4

长梦不多时 2024-07-20 07:19:19

在当前的算法中,您首先要选择正确的孩子。

首先让它成为左孩子。 就这样。

例如,在您的代码中可能有类似以下内容:

node = node.rightChild()

将其更改为

node = node.leftChild()

这是此类问题的一般解决方案。

由于 MSDN 实现没有公开此类代码,因此我无法对此发表评论。

In the current algorithm you are going for the right child first.

Make it left child first. Thats all.

For example, in your code there may be something like:

node = node.rightChild()

Change it to

node = node.leftChild()

This is the general solution for this kind of issues.

Since the MSDN implementation does not expose this kind of code, I cannot comment on that.

寄居者 2024-07-20 07:19:19

以相反的顺序将项目压入堆栈,即 2 在 1 之前。

示例:

// suppose I want to push children[] onto the stack

for (int i = children.Length - 1; i >= 0; i--)
{
   stack.Push(children[i]);
}

要在代码中执行此操作,请尝试以下 for-each 语句:

foreach (DataRowView drvChild in dv.Reverse())

Push your items onto the stack in the reverse order, i.e. 2 before 1.

Example:

// suppose I want to push children[] onto the stack

for (int i = children.Length - 1; i >= 0; i--)
{
   stack.Push(children[i]);
}

To do this in your code, try the following for-each statement:

foreach (DataRowView drvChild in dv.Reverse())
ま昔日黯然 2024-07-20 07:19:19

如果只是担心顺序问题,请从使用堆栈更改为使用队列。 出于实际目的,它们是相同的,区别在于队列是先进先出的。

If it's simply the ordering that is providing concern, change from using a Stack to using a Queue. They're the same for practical purposes with the difference that a Queue is first in first out.

ˇ宁静的妩媚 2024-07-20 07:19:19

通过以相反的顺序更改子节点的迭代,它们可以根据需要显示

//Add child nodes to the stack
for (int i = dv.Count - 1; i >= 0; i--)
{
    DataRowView drvChild = dv[i];
    Item item = new Item();
    item.Text = drvChild["Text"].ToString();
    item.ID = drvChild["ID"].ToString();
    item.ParentID = drvChild["ParentID"].ToString();
    itemTree.Push(item);
}

By changing my iteration of the child nodes in reverse order, they display as desired

//Add child nodes to the stack
for (int i = dv.Count - 1; i >= 0; i--)
{
    DataRowView drvChild = dv[i];
    Item item = new Item();
    item.Text = drvChild["Text"].ToString();
    item.ID = drvChild["ID"].ToString();
    item.ParentID = drvChild["ParentID"].ToString();
    itemTree.Push(item);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文