Initial commit of RecursionUtils

This commit is contained in:
Chris Cameron
2017-09-20 11:05:54 -04:00
parent f7cd51d3df
commit bc61686fa8
2 changed files with 31 additions and 0 deletions

View File

@@ -87,6 +87,7 @@
<Compile Include="Extensions\ListExtensions.cs" />
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RecursionUtils.cs" />
<Compile Include="ReprBuilder.cs" />
<Compile Include="Services\Logging\ILoggerService.cs" />
<Compile Include="Services\Logging\LogItem.cs" />

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
namespace ICD.Common.Utils
{
public static class RecursionUtils
{
/// <summary>
/// Returns all of the nodes in the tree via breadth-first search.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="root"></param>
/// <param name="getChildren"></param>
/// <returns></returns>
public static IEnumerable<T> BreadthFirstSearch<T>(T root, Func<T, IEnumerable<T>> getChildren)
{
Queue<T> process = new Queue<T>();
process.Enqueue(root);
while (process.Count > 0)
{
T current = process.Dequeue();
yield return current;
foreach (T child in getChildren(current))
process.Enqueue(child);
}
}
}
}