< Summary

Information
Class: MoreStructures.RecImmTrees.Visitor.FullyRecursiveDepthFirstTraversal<T1, T2>
Assembly: MoreStructures
File(s): /home/runner/work/MoreStructures/MoreStructures/MoreStructures/RecImmTrees/Visitor/FullyRecursiveDepthFirstTraversal.cs
Line coverage
100%
Covered lines: 16
Uncovered lines: 0
Coverable lines: 16
Total lines: 88
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
Visit(...)100%1100%
Visit()100%12100%

File(s)

/home/runner/work/MoreStructures/MoreStructures/MoreStructures/RecImmTrees/Visitor/FullyRecursiveDepthFirstTraversal.cs

#LineLine coverage
 1namespace 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>
 17public 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) =>
 3060        Visit(new(node, default, default, 0));
 61
 62    private IEnumerable<TreeTraversalVisit<TEdge, TNode>> Visit(TreeTraversalVisit<TEdge, TNode> visit)
 82963    {
 82964        var (node, _, _, level) = visit;
 65
 82966        switch (TraversalOrder)
 67        {
 68            case TreeTraversalOrder.ParentFirst:
 29669                yield return visit;
 145870                foreach (var child in ChildrenSorter(visit))
 2139171                    foreach (var childVisit in Visit(new(child.Value, node, child.Key, level + 1)))
 1026872                        yield return childVisit;
 73
 29674                break;
 75
 76            case TreeTraversalOrder.ChildrenFirst:
 259377                foreach (var child in ChildrenSorter(visit))
 533878                    foreach (var childVisit in Visit(new(child.Value, node, child.Key, level + 1)))
 191979                        yield return childVisit;
 53180                yield return visit;
 81
 53182                break;
 83
 84            default:
 285                throw new NotSupportedException($"{nameof(TreeTraversalOrder)} {TraversalOrder} is not supported");
 86        }
 82787    }
 88}

Methods/Properties

Visit(TNode)
Visit()