C# 大树迭代
我有一个以父/子关系组装的大型结果集。 我需要遍历树并将结果显示给用户。
我在使用递归之前已经完成了此操作,但因为我的结果集可能很大,所以我想避免接收 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在当前的算法中,您首先要选择正确的孩子。
首先让它成为左孩子。 就这样。
例如,在您的代码中可能有类似以下内容:
将其更改为
这是此类问题的一般解决方案。
由于 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:
Change it to
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.
以相反的顺序将项目压入堆栈,即 2 在 1 之前。
示例:
要在代码中执行此操作,请尝试以下
for-each
语句:Push your items onto the stack in the reverse order, i.e. 2 before 1.
Example:
To do this in your code, try the following
for-each
statement:如果只是担心顺序问题,请从使用堆栈更改为使用队列。 出于实际目的,它们是相同的,区别在于队列是先进先出的。
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.
通过以相反的顺序更改子节点的迭代,它们可以根据需要显示
By changing my iteration of the child nodes in reverse order, they display as desired