2

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

我在使用递归之前已经这样做了,但是因为我的结果集可能很大,所以我想避免收到 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);
                }
            }
        }

    }
4

4 回答 4

3

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

让它先离开孩子。就这样。

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

node = node.rightChild()

将其更改为

node = node.leftChild()

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

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

于 2009-02-03T18:21:24.833 回答
1

以相反的顺序将您的项目推入堆栈,即 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())
于 2009-02-03T18:24:45.623 回答
0

如果只是提供关注的顺序,请从使用堆栈更改为使用队列。出于实际目的,它们是相同的,不同之处在于队列是先进先出的。

于 2009-02-03T18:45:55.377 回答
0

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

//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);
}
于 2009-02-03T19:07:49.170 回答