Example #1
0
        /// <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));
            }
        }
Example #2
0
        protected void VisitAllChildren(ITreeBase <TPayload> tree)
        {
            IEnumerable <ITreeBase <TPayload> > children = tree.Children;

            foreach (ITreeBase <TPayload> child in children)
            {
                child.Accept(this);
            }
        }
Example #3
0
        /// <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));
            }
        }
Example #4
0
        /// <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);
        }
Example #6
0
 protected override void DefaultOperation(ITreeBase <TPayload> tree)
 {
     Count++;
 }
Example #7
0
 public TRec WithChild(ITreeBase <TPayload> child)
 {
     _chilren.Add(child);
     return(GetMe());
 }
Example #8
0
 /// <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)));
Example #9
0
 protected void DoOpAndVisitAllChildren(ITreeBase <TPayload> tree, Action <ITreeBase <TPayload> > operation)
 {
     operation(tree);
     VisitAllChildren(tree);
 }
Example #10
0
 public virtual void Visit(ITreeBase <TPayload> tree)
 {
     DoOpAndVisitAllChildren(tree, DefaultOperation);
 }
Example #11
0
 protected virtual void DefaultOperation(ITreeBase <TPayload> tree)
 {
 }