views:

160

answers:

3

Right now my loop is

for (TreeNode n = e.Node.FirstNode; n != null; n = n.NextNode)

and my data is something like

a
    a1
    a2
b
    b1

I want to enum breadth only (a, b etc, not a1, a2 etc). How do i do this?

+3  A: 

Breadth first enumeration is typically done by using a queue of some sort as an ancillary data structure.

First push the root onto the queue. Then, while there is something in the queue:

  • Pop the first item from the front of the queue.
  • Push its children onto the end of the queue.
  • Process the item you popped.
jeffamaphone
A: 

Try

foreach (TreeNode n in e.Node.Parent.Nodes)

you might have to check for a null parent and use

TreeNodeCollection nodes;
if(e.Node.Parent != null)
{
    nodes = e.Node.Parent.Nodes;
}
else
{
    nodes = e.Node.TreeView.Nodes;
}

This should cover the breadth first algorithm (sorry I haven't tested it)

Queue<TreeNode> currentLevel = new Queue<TreeNode>( nodes );
Queue<TreeNode> nextLevel = new Queue<TreeNode>();

while( currentLevel.Count > 0 )
{
 while( currentLevel.Count > 0 )
 {
  TreeNode n = currentLevel.Dequeue();

  // Add child items to next level
  foreach( TreeNode child in n.Nodes )
  {
   nextLevel.Enqueue( child );
  }
 }
 // Switch to next level
 currentLevel = nextLevel;
 nextLevel = new Queue<TreeNode>();
}
bstoney
A: 

Inside your for loop, add an if statement to check if n.parent == e.Node

acidzombie24