| | 1 | | namespace MoreStructures.RecImmTrees.Visitor; |
| | 2 | |
|
| | 3 | | /// <inheritdoc cref="BreadthFirstTraversal{TEdge, TNode}" path="//*[not(self::summary or self::remarks)]"/> |
| | 4 | | /// <summary> |
| | 5 | | /// A lazy, fully-recursive, breadth-first <see cref="IVisitStrategy{TNode, TVisitContext}"/> implementation, i.e. a |
| | 6 | | /// traversing strategy which visits all the nodes at the current depth, along any path of the tree, before going |
| | 7 | | /// deeper or shallower, exploring nodes with higher or lower depth. |
| | 8 | | /// </summary> |
| | 9 | | /// <remarks> |
| | 10 | | /// <inheritdoc cref="BreadthFirstTraversal{TEdge, TNode}" path="/remarks"/> |
| | 11 | | /// <para id="advantages"> |
| | 12 | | /// ADVANTAGES AND DISADVANTAGES |
| | 13 | | /// <br/> |
| | 14 | | /// Implemented fully recursively, so limited by stack depth and usable with tree of a "reasonable" height. |
| | 15 | | /// </para> |
| | 16 | | /// </remarks> |
| | 17 | | public class FullyRecursiveBreadthFirstTraversal<TEdge, TNode> |
| | 18 | | : BreadthFirstTraversal<TEdge, TNode> |
| | 19 | | where TEdge : IRecImmDictIndexedTreeEdge<TEdge, TNode> |
| | 20 | | where TNode : IRecImmDictIndexedTreeNode<TEdge, TNode> |
| | 21 | | { |
| | 22 | |
|
| | 23 | | /// <inheritdoc |
| | 24 | | /// cref="TreeTraversal{TEdge, TNode}.Visit(TNode)" |
| | 25 | | /// path="//*[not(self::summary or self::remarks)]" /> |
| | 26 | | /// <summary> |
| | 27 | | /// <b>Lazily and recursively</b> visits the structure of the provided <paramref name= "node" />, returning the |
| | 28 | | /// sequence of <see cref="IRecImmDictIndexedTreeNode{TEdge, TNode}"/> of the structure, in breadth-first order. |
| | 29 | | /// </summary> |
| | 30 | | /// <remarks> |
| | 31 | | /// <inheritdoc cref="FullyRecursiveBreadthFirstTraversal{TEdge, TNode}" path="/remarks"/> |
| | 32 | | /// <para id = "algo" > |
| | 33 | | /// ALGORITHM |
| | 34 | | /// <br/> |
| | 35 | | /// - The algorithm first lazily visits all nodes in structure in natural recursion/depth-first order, |
| | 36 | | /// returning an <see cref="IEnumerable{T}"/> instance of all nodes with their level in the structure. |
| | 37 | | /// <br/> |
| | 38 | | /// - Then, it lazily sort and visit them level by level, according to |
| | 39 | | /// <see cref="TreeTraversal{TEdge, TNode}.TraversalOrder"/>, yielding to the output sequence, so that the |
| | 40 | | /// client code implementing the visitor can lazily process the nodes. |
| | 41 | | /// </para> |
| | 42 | | /// <para id="complexity"> |
| | 43 | | /// COMPLEXITY |
| | 44 | | /// <br/> |
| | 45 | | /// - Excluding visitor, constant time work is done for each of the n nodes of the tree (such as destructuring |
| | 46 | | /// of <see cref="IEnumerable{T}"/> items and construction of the input record for the recursive call). |
| | 47 | | /// <br/> |
| | 48 | | /// - Recursive traversal, as well as sorting, are lazily executed. Iteration-cost is constant w.r.t. n. |
| | 49 | | /// <see cref="TreeTraversal{TEdge, TNode}.ChildrenSorter"/> cost depends on the actual algorithm used. |
| | 50 | | /// When no sorting, Counting Sort or QuickSort is applied (respectively O(1), O(n), O(n * log(n)), the cost |
| | 51 | | /// is tipically equalized or exceeded by sorting cost (see below). |
| | 52 | | /// <br/> |
| | 53 | | /// - So Time Complexity is dominated by the two operations on the <see cref="IEnumerable{T}"/> generated by |
| | 54 | | /// the recursive traversal: sorting and visitor. |
| | 55 | | /// <br/> |
| | 56 | | /// - Sorting done on the <see cref="IEnumerable{T}"/> of all the n nodes retrieved during recursive traversal |
| | 57 | | /// via the LINQ functionalities |
| | 58 | | /// <see cref="Enumerable.OrderBy{TSource, TKey}(IEnumerable{TSource}, Func{TSource, TKey})"/> and |
| | 59 | | /// <see cref="Enumerable.OrderByDescending{TSource, TKey}(IEnumerable{TSource}, Func{TSource, TKey})"/>. |
| | 60 | | /// <br/> |
| | 61 | | /// - Visitor is client code invoked during iteration of the output sequence, containing each of the n nodes of |
| | 62 | | /// the sorted <see cref="IEnumerable{T}"/>. |
| | 63 | | /// <br/> |
| | 64 | | /// - If the size of alphabet of elements of the tree is a small constant c, sorting could be done in linear |
| | 65 | | /// time via Counting Sort. Otherwise, a comparison-based sorting takes at best a time proportional to |
| | 66 | | /// n * log(n). However, LINQ sorting by |
| | 67 | | /// <see cref="Enumerable.OrderBy{TSource, TKey}(IEnumerable{TSource}, Func{TSource, TKey})"/> and |
| | 68 | | /// <see cref="Enumerable.OrderByDescending{TSource, TKey}(IEnumerable{TSource}, Func{TSource, TKey})"/> |
| | 69 | | /// is QuickSort based, and has a O(n * log(n)) average runtime, with O(n^2) worst case. |
| | 70 | | /// <br/> |
| | 71 | | /// In conclusion: |
| | 72 | | /// <br/> |
| | 73 | | /// - Time Complexity is O(n * (log(n) + Ts)), where Ts is the amortized time cost of |
| | 74 | | /// <see cref="TreeTraversal{TEdge, TNode}.ChildrenSorter"/> per node. Taking into account the visit of |
| | 75 | | /// each emitted node, Time Complexity is O(n * (log(n) + Ts + Tv)), where Tv is the time cost of the |
| | 76 | | /// visitor per node. |
| | 77 | | /// <br/> |
| | 78 | | /// - Space Complexity is O(n). Taking into account the visit of each emitted node, Space Complexity is |
| | 79 | | /// O(n * Sv), where Sv is the space cost of visitor per node. |
| | 80 | | /// </para> |
| | 81 | | /// </remarks> |
| | 82 | | public override IEnumerable<TreeTraversalVisit<TEdge, TNode>> Visit(TNode node) |
| 15 | 83 | | { |
| 15 | 84 | | var visits = GetAllVisits(new(node, default, default, 0)); |
| 15 | 85 | | return TraversalOrder switch |
| 15 | 86 | | { |
| 103 | 87 | | TreeTraversalOrder.ParentFirst => visits.OrderBy(nodeWitLevel => nodeWitLevel.Level), |
| 45 | 88 | | TreeTraversalOrder.ChildrenFirst => visits.OrderByDescending(nodeWitLevel => nodeWitLevel.Level), |
| 2 | 89 | | _ => throw new NotSupportedException($"{nameof(TreeTraversalOrder)} {TraversalOrder} is not supported"), |
| 15 | 90 | | }; |
| 13 | 91 | | } |
| | 92 | |
|
| | 93 | | private IEnumerable<TreeTraversalVisit<TEdge, TNode>> GetAllVisits(TreeTraversalVisit<TEdge, TNode> visit) |
| 135 | 94 | | { |
| 135 | 95 | | yield return visit; |
| | 96 | |
|
| 135 | 97 | | var (node, _, _, level) = visit; |
| | 98 | |
|
| 649 | 99 | | foreach (var child in ChildrenSorter(visit)) |
| 848 | 100 | | foreach (var childVisit in GetAllVisits(new(child.Value, node, child.Key, level + 1))) |
| 241 | 101 | | yield return childVisit; |
| 135 | 102 | | } |
| | 103 | | } |