Add GraphTraversal.cs

This commit is contained in:
ig 2023-09-18 13:06:22 +02:00
parent 13611da7c8
commit 526dd2a47f
1 changed files with 17 additions and 17 deletions

View File

@ -6,7 +6,7 @@ public static class GraphTraversal
{ {
public static IEnumerable<T> TraverseDepthFirstPreOrder<T>(T root, public static IEnumerable<T> TraverseDepthFirstPreOrder<T>(T root,
Func<T, IEnumerable<T>> getChildren, Func<T, IEnumerable<T>> getChildren,
IEqualityComparer<T>? comparer = null) IEqualityComparer<T>? comparer = null)
{ {
return Traverse(root, TreeTraversal.TraverseDepthFirstPreOrder, getChildren, comparer); return Traverse(root, TreeTraversal.TraverseDepthFirstPreOrder, getChildren, comparer);
} }
@ -14,7 +14,7 @@ IEqualityComparer<T>? comparer = null)
public static IEnumerable<T> TraverseDepthFirstPostOrder<T>(T root, public static IEnumerable<T> TraverseDepthFirstPostOrder<T>(T root,
Func<T, IEnumerable<T>> getChildren, Func<T, IEnumerable<T>> getChildren,
IEqualityComparer<T>? comparer = null) IEqualityComparer<T>? comparer = null)
{ {
return Traverse(root, TreeTraversal.TraverseDepthFirstPostOrder, getChildren, comparer); return Traverse(root, TreeTraversal.TraverseDepthFirstPostOrder, getChildren, comparer);
} }
@ -28,7 +28,7 @@ IEqualityComparer<T>? comparer = null)
public static IEnumerable<T> TraverseDepthFirstPreOrder<T>(IEnumerable<T> sources, public static IEnumerable<T> TraverseDepthFirstPreOrder<T>(IEnumerable<T> sources,
Func<T, IEnumerable<T>> getChildren, Func<T, IEnumerable<T>> getChildren,
IEqualityComparer<T>? comparer = null) IEqualityComparer<T>? comparer = null)
{ {
return Traverse(sources, TreeTraversal.TraverseDepthFirstPreOrder, getChildren, comparer); return Traverse(sources, TreeTraversal.TraverseDepthFirstPreOrder, getChildren, comparer);
} }
@ -36,30 +36,30 @@ IEqualityComparer<T>? comparer = null)
public static IEnumerable<T> TraverseDepthFirstPostOrder<T>(IEnumerable<T> sources, public static IEnumerable<T> TraverseDepthFirstPostOrder<T>(IEnumerable<T> sources,
Func<T, IEnumerable<T>> getChildren, Func<T, IEnumerable<T>> getChildren,
IEqualityComparer<T>? comparer = null) IEqualityComparer<T>? comparer = null)
{ {
return Traverse(sources, TreeTraversal.TraverseDepthFirstPostOrder, getChildren, comparer); return Traverse(sources, TreeTraversal.TraverseDepthFirstPostOrder, getChildren, comparer);
} }
public static IEnumerable<T> TraverseBreadthFirst<T>(IEnumerable<T> sources, public static IEnumerable<T> TraverseBreadthFirst<T>(IEnumerable<T> sources,
Func<T, IEnumerable<T>> getChildren, Func<T, IEnumerable<T>> getChildren,
IEqualityComparer<T>? comparer = null) IEqualityComparer<T>? comparer = null)
{ {
return Traverse(sources, TreeTraversal.TraverseBreadthFirst, getChildren, comparer); 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 , Func<T, IEnumerable<T>>,IEnumerable<T>> traversor,
Func<T, IEnumerable<T>> getChildren, Func<T, IEnumerable<T>> getChildren,
IEqualityComparer<T>? comparer = null) IEqualityComparer<T>? comparer = null)
{ {
var getUniqueChildren = GetUniqueChildren(getChildren, root, comparer); var getUniqueChildren = GetUniqueChildren(getChildren, root, comparer);
return traversor(root, getUniqueChildren); return traversor(root, getUniqueChildren);
} }
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")] [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 , Func<T, IEnumerable<T>>,IEnumerable<T>> traversor,
Func<T, IEnumerable<T>> getChildren, Func<T, IEnumerable<T>> getChildren,
IEqualityComparer<T>? comparer = null) IEqualityComparer<T>? comparer = null)
@ -97,20 +97,20 @@ IEqualityComparer<T>? comparer = null)
select e; select e;
} }
private static Func<T, IEnumerable<T>> GetUniqueChildren<T>(Func<T, IEnumerable<T>> getChildren, private static Func<T, IEnumerable<T>> GetUniqueChildren<T>(Func<T, IEnumerable<T>> getChildren,
T root, T root,
IEqualityComparer<T>? comparer) IEqualityComparer<T>? comparer)
{ {
return GetUniqueChildren(getChildren, root.AsSingleEnumerable(), comparer); return GetUniqueChildren(getChildren, root.AsSingleEnumerable(), comparer);
} }
private static Func<T, IEnumerable<T>> GetUniqueChildren<T>(Func<T, IEnumerable<T>> getChildren, private static Func<T, IEnumerable<T>> GetUniqueChildren<T>(Func<T, IEnumerable<T>> getChildren,
IEnumerable<T> sources, IEnumerable<T> sources,
IEqualityComparer<T>? comparer) IEqualityComparer<T>? comparer)
{ {
var set = new HashSet<T>(sources, comparer ?? EqualityComparer<T>.Default); var set = new HashSet<T>(sources, comparer ?? EqualityComparer<T>.Default);
return n => getChildren(n).Where(set.Add); return n => getChildren(n).Where(set.Add);
} }
} }