Add GraphTraversal.cs
This commit is contained in:
parent
13611da7c8
commit
526dd2a47f
|
@ -6,7 +6,7 @@ public static class GraphTraversal
|
|||
{
|
||||
public static IEnumerable<T> TraverseDepthFirstPreOrder<T>(T root,
|
||||
Func<T, IEnumerable<T>> getChildren,
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
{
|
||||
return Traverse(root, TreeTraversal.TraverseDepthFirstPreOrder, getChildren, comparer);
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ IEqualityComparer<T>? comparer = null)
|
|||
|
||||
public static IEnumerable<T> TraverseDepthFirstPostOrder<T>(T root,
|
||||
Func<T, IEnumerable<T>> getChildren,
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
{
|
||||
return Traverse(root, TreeTraversal.TraverseDepthFirstPostOrder, getChildren, comparer);
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ IEqualityComparer<T>? comparer = null)
|
|||
|
||||
public static IEnumerable<T> TraverseDepthFirstPreOrder<T>(IEnumerable<T> sources,
|
||||
Func<T, IEnumerable<T>> getChildren,
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
{
|
||||
return Traverse(sources, TreeTraversal.TraverseDepthFirstPreOrder, getChildren, comparer);
|
||||
}
|
||||
|
@ -36,30 +36,30 @@ IEqualityComparer<T>? comparer = null)
|
|||
|
||||
public static IEnumerable<T> TraverseDepthFirstPostOrder<T>(IEnumerable<T> sources,
|
||||
Func<T, IEnumerable<T>> getChildren,
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
{
|
||||
return Traverse(sources, TreeTraversal.TraverseDepthFirstPostOrder, getChildren, comparer);
|
||||
}
|
||||
|
||||
public static IEnumerable<T> TraverseBreadthFirst<T>(IEnumerable<T> sources,
|
||||
Func<T, IEnumerable<T>> getChildren,
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
{
|
||||
return Traverse(sources, TreeTraversal.TraverseBreadthFirst, getChildren, comparer);
|
||||
}
|
||||
|
||||
|
||||
private static IEnumerable<T> Traverse<T>(T root,
|
||||
private static IEnumerable<T> Traverse<T>(T root,
|
||||
Func<T , Func<T, IEnumerable<T>>,IEnumerable<T>> traversor,
|
||||
Func<T, IEnumerable<T>> getChildren,
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
Func<T, IEnumerable<T>> getChildren,
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
{
|
||||
var getUniqueChildren = GetUniqueChildren(getChildren, root, comparer);
|
||||
return traversor(root, getUniqueChildren);
|
||||
}
|
||||
|
||||
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
|
||||
private static IEnumerable<T> Traverse<T>(IEnumerable<T> sources,
|
||||
private static IEnumerable<T> Traverse<T>(IEnumerable<T> sources,
|
||||
Func<T , Func<T, IEnumerable<T>>,IEnumerable<T>> traversor,
|
||||
Func<T, IEnumerable<T>> getChildren,
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
|
@ -97,20 +97,20 @@ IEqualityComparer<T>? comparer = null)
|
|||
select e;
|
||||
}
|
||||
|
||||
private static Func<T, IEnumerable<T>> GetUniqueChildren<T>(Func<T, IEnumerable<T>> getChildren,
|
||||
T root,
|
||||
IEqualityComparer<T>? comparer)
|
||||
private static Func<T, IEnumerable<T>> GetUniqueChildren<T>(Func<T, IEnumerable<T>> getChildren,
|
||||
T root,
|
||||
IEqualityComparer<T>? comparer)
|
||||
{
|
||||
return GetUniqueChildren(getChildren, root.AsSingleEnumerable(), comparer);
|
||||
}
|
||||
|
||||
private static Func<T, IEnumerable<T>> GetUniqueChildren<T>(Func<T, IEnumerable<T>> getChildren,
|
||||
IEnumerable<T> sources,
|
||||
IEqualityComparer<T>? comparer)
|
||||
private static Func<T, IEnumerable<T>> GetUniqueChildren<T>(Func<T, IEnumerable<T>> getChildren,
|
||||
IEnumerable<T> sources,
|
||||
IEqualityComparer<T>? comparer)
|
||||
{
|
||||
var set = new HashSet<T>(sources, comparer ?? EqualityComparer<T>.Default);
|
||||
return n => getChildren(n).Where(set.Add);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue