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,14 +36,14 @@ 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);
} }
@ -52,7 +52,7 @@ IEqualityComparer<T>? comparer = null)
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);
@ -98,15 +98,15 @@ IEqualityComparer<T>? comparer = null)
} }
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);