I\'ve got a tree-like structure. Each element in this structure should be able to return a Enumerable of all elements it is root to. Let\'s call this method IEnumerabl
According to my prior experiences using yield is a lot more effective than creating a List. If you're using .NET 3.5, this implementation should be fine. But don't forget the
yield break;
At the end. :-)
No, that looks fine.
Have a look at my blog entry, it may be of some use :)
You can improve performance if you unroll recurse to stack, so you will have only one iterator:
public IEnumerable<Foo> GetAll()
{
Stack<Foo> FooStack = new Stack<Foo>();
FooStack.Push(this);
while (FooStack.Count > 0)
{
Foo Result = FooStack.Pop();
yield return Result;
foreach (Foo NextFoo in Result.MyChildren)
FooStack.Push(NextFoo);
}
}
A better solution might be to create a visit method that recursively traverses the tree, and use that to collect items up.
Something like this (assuming a binary tree):
public class Node<T>
{
public void Visit(Action<T> action)
{
action(this);
left.Visit(action);
right.Visit(action);
}
public IEnumerable<Foo> GetAll ()
{
var result = new List<T>();
Visit( n => result.Add(n));
return result;
}
}
Taking this approach
It's certainly not ideal in terms of performance - you end up creating a lot of iterators for large trees, instead of a single iterator which knows how to traverse efficiently.
Some blog entries concerning this:
It's worth noting that F# has the equivalent of the proposed "yield foreach
" with "yield!
"