| | 1 | | namespace MoreStructures.RecImmTrees.Visitor; |
| | 2 | |
|
| | 3 | | /// <inheritdoc cref="DepthFirstTraversal{TEdge, TNode}" path="//*[not(self::summary or self::remarks)]"/> |
| | 4 | | /// <summary> |
| | 5 | | /// A lazy, fully-recursive, depth-first <see cref="IVisitStrategy{TNode, TVisitContext}"/> implementation, i.e. a |
| | 6 | | /// traversing strategy which goes in depth as far as possible along each path of the tree, only backtracking when a |
| | 7 | | /// leaf is reached. |
| | 8 | | /// </summary> |
| | 9 | | /// <remarks> |
| | 10 | | /// <inheritdoc cref="DepthFirstTraversal{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 FullyRecursiveDepthFirstTraversal<TEdge, TNode> |
| | 18 | | : DepthFirstTraversal<TEdge, TNode> |
| | 19 | | where TEdge : IRecImmDictIndexedTreeEdge<TEdge, TNode> |
| | 20 | | where TNode : IRecImmDictIndexedTreeNode<TEdge, TNode> |
| | 21 | | { |
| | 22 | | /// <inheritdoc |
| | 23 | | /// cref="TreeTraversal{TEdge, TNode}.Visit(TNode)" |
| | 24 | | /// path="//*[not(self::summary or self::remarks)]"/> |
| | 25 | | /// <summary> |
| | 26 | | /// <b>Lazily and recursively</b> visits the structure of the provided<paramref name= "node" />, returning the |
| | 27 | | /// sequence of <see cref="IRecImmDictIndexedTreeNode{TEdge, TNode}"/> of the structure, in depth-first order. |
| | 28 | | /// </summary> |
| | 29 | | /// <remarks> |
| | 30 | | /// <inheritdoc cref="FullyRecursiveDepthFirstTraversal{TEdge, TNode}" path="/remarks"/> |
| | 31 | | /// <para id = "algo" > |
| | 32 | | /// ALGORITHM |
| | 33 | | /// <br/> |
| | 34 | | /// - The algorithm visits all nodes in structure in natural recursion/depth-first order, yielding to the |
| | 35 | | /// output sequence, so that the client code implementing the visitor can lazily process the nodes. |
| | 36 | | /// </para> |
| | 37 | | /// <para id="complexity"> |
| | 38 | | /// COMPLEXITY |
| | 39 | | /// <br/> |
| | 40 | | /// - Excluding visitor, constant time work is done for each of the n nodes of the tree (such as construction |
| | 41 | | /// of the input record for the visitor). |
| | 42 | | /// <br/> |
| | 43 | | /// - Iteration-cost is constant w.r.t. n. <see cref="TreeTraversal{TEdge, TNode}.ChildrenSorter"/> cost |
| | 44 | | /// depends on the actual algorithm used. |
| | 45 | | /// <br/> |
| | 46 | | /// - So Time Complexity is dominated by <see cref="TreeTraversal{TEdge, TNode}.ChildrenSorter"/> and visitor. |
| | 47 | | /// <br/> |
| | 48 | | /// In conclusion: |
| | 49 | | /// <br/> |
| | 50 | | /// - Time Complexity is O(n * Ts), where Ts is the amortized time cost of |
| | 51 | | /// <see cref="TreeTraversal{TEdge, TNode}.ChildrenSorter"/> per node. Taking into account the visit of |
| | 52 | | /// each emitted node, Time Complexity is O(n * (Ts + Tv)), where Tv is the time cost of the visitor per |
| | 53 | | /// node. |
| | 54 | | /// <br/> |
| | 55 | | /// - Space Complexity is O(n). Taking into account the visit of each emitted node, Space Complexity is |
| | 56 | | /// O(n * Sv), where Sv is the space cost of visitor per node. |
| | 57 | | /// </para> |
| | 58 | | /// </remarks> |
| | 59 | | public override IEnumerable<TreeTraversalVisit<TEdge, TNode>> Visit(TNode node) => |
| 30 | 60 | | Visit(new(node, default, default, 0)); |
| | 61 | |
|
| | 62 | | private IEnumerable<TreeTraversalVisit<TEdge, TNode>> Visit(TreeTraversalVisit<TEdge, TNode> visit) |
| 829 | 63 | | { |
| 829 | 64 | | var (node, _, _, level) = visit; |
| | 65 | |
|
| 829 | 66 | | switch (TraversalOrder) |
| | 67 | | { |
| | 68 | | case TreeTraversalOrder.ParentFirst: |
| 296 | 69 | | yield return visit; |
| 1458 | 70 | | foreach (var child in ChildrenSorter(visit)) |
| 21391 | 71 | | foreach (var childVisit in Visit(new(child.Value, node, child.Key, level + 1))) |
| 10268 | 72 | | yield return childVisit; |
| | 73 | |
|
| 296 | 74 | | break; |
| | 75 | |
|
| | 76 | | case TreeTraversalOrder.ChildrenFirst: |
| 2593 | 77 | | foreach (var child in ChildrenSorter(visit)) |
| 5338 | 78 | | foreach (var childVisit in Visit(new(child.Value, node, child.Key, level + 1))) |
| 1919 | 79 | | yield return childVisit; |
| 531 | 80 | | yield return visit; |
| | 81 | |
|
| 531 | 82 | | break; |
| | 83 | |
|
| | 84 | | default: |
| 2 | 85 | | throw new NotSupportedException($"{nameof(TreeTraversalOrder)} {TraversalOrder} is not supported"); |
| | 86 | | } |
| 827 | 87 | | } |
| | 88 | | } |