Skip to content

Instantly share code, notes, and snippets.

@DexterHaslem
Created May 9, 2012 19:45
Show Gist options
  • Save DexterHaslem/2648306 to your computer and use it in GitHub Desktop.
Save DexterHaslem/2648306 to your computer and use it in GitHub Desktop.
depth first search (kill ur stack)
public static IEnumerable<T> DepthFirstSearch<T>(IEnumerable<T> start, Func<T, IEnumerable<T>> selector, Func<T, bool> predicate)
{
var results = new List<T>();
foreach (T item in start)
{
if (predicate != null && predicate(item))
results.Add(item);
else if (predicate == null)
results.Add(item);
var subItems = selector(item);
if (subItems != null)
results.AddRange(DepthFirstSearch(selector(item), selector, predicate));
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment