Esempio n. 1
0
        /// <summary>
        /// A helper method to traverse all nodes and execute an action per node.
        /// </summary>
        /// <param name="root">The travseral start.</param>
        /// <param name="onNext">The action to execute per node.</param>
        /// <param name="traversal">The type of DFS traversal.</param>
        public static void Traverse(
            T root,
            Action <T> onNext,
            Traversal traversal = Traversal.PreOrder,
            TraversalSkip skip  = TraversalSkip.None)
        {
            var itr = new TreeIterator <T>(root, traversal);

            if (skip == TraversalSkip.Root)
            {
                itr.Next();
            }

            while (itr.HasNext())
            {
                var node = itr.Next();
                onNext(node);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// A helper method to traverse all nodes and execute an action per node.
        /// This method also passes the iterator doing the traversal.
        /// </summary>
        /// <param name="root">The traversal start.</param>
        /// <param name="onNext">The action to execute per node.</param>
        /// <param name="traversal">The type of DFS traversal.</param>
        public static void Traverse(T root, Action <T, TreeIterator <T> > onNext, Traversal traversal = Traversal.PreOrder)
        {
            var itr = new TreeIterator <T>(root, traversal);

            while (itr.HasNext())
            {
                var node = itr.Next();
                onNext(node, itr);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// A helper method to traverse all nodes and accumulate a value over the traversal.
        /// </summary>
        /// <typeparam name="TAccum"></typeparam>
        /// <param name="root"></param>
        /// <param name="accumulator">The function used to accumulate the value</param>
        /// <param name="initial">The starting value for the accumulation</param>
        /// <param name="traversal"></param>
        /// <returns></returns>
        public static TAccum Traverse <TAccum>(T root, Func <TAccum, T, TAccum> accumulator, TAccum initial, Traversal traversal = Traversal.PreOrder)
        {
            var itr = new TreeIterator <T>(root, traversal);

            while (itr.HasNext())
            {
                var node = itr.Next();
                initial = accumulator(initial, node);
            }

            return(initial);
        }
Esempio n. 4
0
        /// <summary>
        /// A pre-order traversal with the option to skip some nodes.
        /// </summary>
        /// <param name="root"></param>
        /// <param name="onNext"></param>
        /// <param name="skipFilter"></param>
        public static void Traverse(T root, Action <T> onNext, Func <T, bool> skipFilter)
        {
            if (skipFilter(root))
            {
                return;
            }

            var itr = new TreeIterator <T>(root);

            itr._skipFilter = skipFilter;

            while (itr.HasNext())
            {
                var node = itr.Next();
                onNext(node);
            }
        }