Here's an extension method that does the job:
// Depth-first traversal, recursive
public static IEnumerable<T> Flatten<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> childrenSelector)
{
foreach (var item in source)
{
yield return item;
foreach (var child in childrenSelector(item).Flatten(childrenSelector))
{
yield return child;
}
}
}
You can use it like this:
foreach(var category in categories.Flatten(c => c.Children))
{
...
}
The solution above does a depth-first traversal, if you want a breadth-first traversal you can do something like this:
// Breadth-first traversal, non-recursive
public static IEnumerable<T> Flatten2<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> childrenSelector)
{
var queue = new Queue<T>(source);
while (queue.Count > 0)
{
var item = queue.Dequeue();
yield return item;
foreach (var child in childrenSelector(item))
{
queue.Enqueue(child);
}
}
}
It also has the benefit of being non-recursive...
UPDATE: Actually, I just thought of a way to make the depth-first traversal non-recursive... here it is:
// Depth-first traversal, non-recursive
public static IEnumerable<T> Flatten3<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> childrenSelector)
{
LinkedList<T> list = new LinkedList<T>(source);
while (list.Count > 0)
{
var item = list.First.Value;
yield return item;
list.RemoveFirst();
var node = list.First;
foreach (var child in childrenSelector(item))
{
if (node != null)
list.AddBefore(node, child);
else
list.AddLast(child);
}
}
}
I'm using a LinkedList<T>
because insertions are O(1) operations, whereas insertions to a List<T>
are O(n).