Created
May 9, 2012 19:45
-
-
Save DexterHaslem/2648306 to your computer and use it in GitHub Desktop.
depth first search (kill ur stack)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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