| | 1 | | namespace MoreStructures.RecImmTrees.Paths; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// <inheritdoc/> |
| | 5 | | /// <br/> |
| | 6 | | /// Recursive implementation. |
| | 7 | | /// </summary> |
| | 8 | | /// <remarks> |
| | 9 | | /// <inheritdoc cref="INodeToLeafPathsBuilder" path="/remarks"/> |
| | 10 | | /// <para id="advantages"> |
| | 11 | | /// ADVANTAGES AND DISADVANTAGES |
| | 12 | | /// <br/> |
| | 13 | | /// Implemented fully recursively, so limited by stack depth and usable with tree of a "reasonable" height. |
| | 14 | | /// </para> |
| | 15 | | /// <para id="algo"> |
| | 16 | | /// ALGORITHM |
| | 17 | | /// <br/> |
| | 18 | | /// - The implementation iterates over the children, getting its node-to-leaf subpaths by calling |
| | 19 | | /// <see cref="GetAllNodeToLeafPaths{TEdge, TNode}(TNode)"/> recursively. |
| | 20 | | /// <br/> |
| | 21 | | /// - Then, it prepends the child and its incoming edge to each subpath of each child. |
| | 22 | | /// <br/> |
| | 23 | | /// - If the node has no child, a singleton path is returned, containing only the child and its incoming edge. |
| | 24 | | /// </para> |
| | 25 | | /// <para id="complexity"> |
| | 26 | | /// COMPLEXITY |
| | 27 | | /// <br/> |
| | 28 | | /// - Each node is processed once and the number of node-to-leaf paths returned has an upper bound on n = number of |
| | 29 | | /// nodes in the tree. The length of each node-to-leaf path is also limited by n. |
| | 30 | | /// <br/> |
| | 31 | | /// - So, both Time and Space Complexity are O(n). |
| | 32 | | /// </para> |
| | 33 | | /// </remarks> |
| | 34 | | public class FullyRecursiveNodeToLeafPathsBuilder : INodeToLeafPathsBuilder |
| | 35 | | { |
| | 36 | | /// <inheritdoc path="//*[not(self::remarks)]"/> |
| | 37 | | /// <inheritdoc cref="FullyRecursiveNodeToLeafPathsBuilder" path="/remarks"/> |
| | 38 | | public IEnumerable<TreePath<TEdge, TNode>> GetAllNodeToLeafPaths<TEdge, TNode>(TNode node) |
| | 39 | | where TEdge : IRecImmDictIndexedTreeEdge<TEdge, TNode> |
| | 40 | | where TNode : IRecImmDictIndexedTreeNode<TEdge, TNode> => |
| | 41 | |
|
| 17 | 42 | | GetAllNodeToLeafPathsR<TEdge, TNode>(node).Select(pathNodes => new TreePath<TEdge, TNode>(pathNodes)); |
| | 43 | |
|
| | 44 | | private IEnumerable<IEnumerable<KeyValuePair<TEdge, TNode>>> GetAllNodeToLeafPathsR<TEdge, TNode>(TNode node) |
| | 45 | | where TEdge : IRecImmDictIndexedTreeEdge<TEdge, TNode> |
| | 46 | | where TNode : IRecImmDictIndexedTreeNode<TEdge, TNode> |
| 62 | 47 | | { |
| 253 | 48 | | foreach (var edgeAndChild in node.Children) |
| 44 | 49 | | { |
| 44 | 50 | | var childToLeafPaths = GetAllNodeToLeafPathsR<TEdge, TNode>(edgeAndChild.Value); |
| 44 | 51 | | var childPathNode = KeyValuePair.Create(edgeAndChild.Key, edgeAndChild.Value); |
| 44 | 52 | | if (childToLeafPaths.Any()) |
| 15 | 53 | | { |
| 89 | 54 | | foreach (var childToLeafPath in childToLeafPaths) |
| 25 | 55 | | yield return childToLeafPath.Prepend(childPathNode); |
| 9 | 56 | | } |
| | 57 | | else |
| 29 | 58 | | { |
| 29 | 59 | | yield return new KeyValuePair<TEdge, TNode>[] { childPathNode }; |
| 14 | 60 | | } |
| 23 | 61 | | } |
| 41 | 62 | | } |
| | 63 | | } |