/// <summary> /// Get depth of the tree. /// </summary> /// <typeparam name="T">The type of value the node contains.</typeparam> /// <param name="tree">A tree to calculte depth.</param> /// <returns>The depth of the tree.</returns> /// <exception cref="ArgumentNullException">When <paramref name="tree"/> is <see langword="null"/>.</exception> public static int GetDepth <T>(this ITreeBase <T> tree) { int GetDepthInternal(ITreeBase <T> node, int depth) { int result = depth; foreach (ITreeBase <T> child in node) { int tempDepth = GetDepthInternal(child, depth + 1); if (tempDepth > result) { result = tempDepth; } } return(result); } switch (tree ?? throw ExceptionHelper.ArgumentNull(nameof(tree))) { case IBinaryTree <T> binaryTree: return(GetDepthBinary(binaryTree)); default: return(GetDepthInternal(tree, 1)); } }
protected void VisitAllChildren(ITreeBase <TPayload> tree) { IEnumerable <ITreeBase <TPayload> > children = tree.Children; foreach (ITreeBase <TPayload> child in children) { child.Accept(this); } }
/// <summary> /// Get an <see cref="IEnumerable{T}"/> with order of depth-first-search. /// </summary> /// <typeparam name="T">The type of value the node contains.</typeparam> /// <param name="tree">A tree to enumerate.</param> /// <returns>An <see cref="IEnumerable{T}"/> with order of depth-first-search.</returns> /// <exception cref="ArgumentNullException">When <paramref name="tree"/> is <see langword="null"/>.</exception> public static IEnumerable <ITreeBase <T> > AsDFSEnumerable <T>(this ITreeBase <T> tree) { switch (tree ?? throw ExceptionHelper.ArgumentNull(nameof(tree))) { case IBinaryTree <T> binaryTree: return(AsPreOrderEnumerableIterator(binaryTree)); default: return(AsDFSEnumerableIterator(tree)); } }
/// <summary> /// Get an iterator with order of depth-first-search. /// </summary> /// <typeparam name="T">The type of value the node contains.</typeparam> /// <param name="tree">A tree to enumerate.</param> /// <returns>An <see cref="IEnumerable{T}"/> with order of depth-first-search.</returns> private static IEnumerable <ITreeBase <T> > AsDFSEnumerableIterator <T>(ITreeBase <T> tree) { Stack <ITreeBase <T> > nodes = new Stack <ITreeBase <T> >(); nodes.Push(tree); while (nodes.Count != 0) { ITreeBase <T> current = nodes.Pop(); yield return(current); foreach (var child in current.Reverse()) { nodes.Push(child); } } }
protected override void DefaultOperation(ITreeBase <TPayload> tree) { TPayload payload = tree.Payload; Result = Accumulate(payload, Result); }
protected override void DefaultOperation(ITreeBase <TPayload> tree) { Count++; }
public TRec WithChild(ITreeBase <TPayload> child) { _chilren.Add(child); return(GetMe()); }
/// <summary> /// Get an <see cref="IEnumerable{T}"/> with order of depth-first-search with current path. /// </summary> /// <typeparam name="T">The type of value the node contains.</typeparam> /// <param name="tree">A tree to enumerate.</param> /// <returns>An <see cref="IEnumerable{T}"/> with order of depth-first-search with current path.</returns> /// <exception cref="ArgumentNullException">When <paramref name="tree"/> is <see langword="null"/>.</exception> public static IEnumerable <(ITreeBase <T> Node, IReadOnlyCollection <ITreeBase <T> > Path)> AsDFSWithPath <T>(this ITreeBase <T> tree) => AsDFSWithPathIterator(tree ?? throw ExceptionHelper.ArgumentNull(nameof(tree)));
protected void DoOpAndVisitAllChildren(ITreeBase <TPayload> tree, Action <ITreeBase <TPayload> > operation) { operation(tree); VisitAllChildren(tree); }
public virtual void Visit(ITreeBase <TPayload> tree) { DoOpAndVisitAllChildren(tree, DefaultOperation); }
protected virtual void DefaultOperation(ITreeBase <TPayload> tree) { }