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,14 +36,14 @@ 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);
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ IEqualityComparer<T>? comparer = null)
|
|||
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)
|
||||
IEqualityComparer<T>? comparer = null)
|
||||
{
|
||||
var getUniqueChildren = GetUniqueChildren(getChildren, root, comparer);
|
||||
return traversor(root, getUniqueChildren);
|
||||
|
@ -98,15 +98,15 @@ IEqualityComparer<T>? comparer = null)
|
|||
}
|
||||
|
||||
private static Func<T, IEnumerable<T>> GetUniqueChildren<T>(Func<T, IEnumerable<T>> getChildren,
|
||||
T root,
|
||||
IEqualityComparer<T>? comparer)
|
||||
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)
|
||||
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