Пример #1
0
        /// <summary>
        /// Traverses a tree
        /// </summary>
        /// <typeparam name="T">Tree data type</typeparam>
        /// <param name="root">Root node of tree</param>
        /// <param name="order">Traversal order</param>
        /// <returns>An enumeration of the nodes in the specified traverse order</returns>
        public static IEnumerable <Tree <T> > Traverse <T>(this Tree <T> root, TreeTraversalOrder order = TreeTraversalOrder.BreadthFirst)
        {
            if (order == TreeTraversalOrder.BreadthFirst)
            {
                return(TraverseBF(root));
            }

            else
            {
                return(TraverseDF(root, order));
            }
        }
Пример #2
0
        /// <summary>
        /// Applies an action to every node of the tree
        /// </summary>
        /// <typeparam name="T">Tree data type</typeparam>
        /// <param name="root">Root node of tree</param>
        /// <param name="action">Action to apply</param>
        /// <param name="order">Traversal order</param>
        public static void Apply <T>(this Tree <T> root, Action <Tree <T> > action, TreeTraversalOrder order = TreeTraversalOrder.BreadthFirst)
        {
            // Safety check
            if (action == null)
            {
                return;
            }

            // Apply action
            foreach (var node in Traverse(root, order))
            {
                action(node);
            }
        }
        public IEnumerable <T> GetEnumerable(TreeTraversalOrder order)
        {
            switch (order)
            {
            case TreeTraversalOrder.InOrder:
                return(InOrderTraversal());

            case TreeTraversalOrder.PostOrder:
                return(PostOrderTraversal());

            case TreeTraversalOrder.PreOrder:
                return(PreOrderTraversal());
            }

            return(null);
        }
Пример #4
0
        public IEnumerable <T> Traverse(TreeTraversalOrder order)
        {
            if (this.root == null)
            {
                return(Enumerable.Empty <T>());
            }

            switch (order)
            {
            case TreeTraversalOrder.PreOrder: return(TraversePreOrder(root));

            case TreeTraversalOrder.InOrder: return(TraverseInOrder(root));

            case TreeTraversalOrder.PostOrder: return(TraversePostOrder(root));

            case TreeTraversalOrder.LevelOrder: return(TraverseLevelOrder(root));

            default:
                throw new NotImplementedException();
            }
        }
Пример #5
0
        private static IEnumerable <Tree <T> > TraverseDF <T>(this Tree <T> root, TreeTraversalOrder order)
        {
            // Preorder - return root first
            if (order == TreeTraversalOrder.DepthFirstPreOrder)
            {
                yield return(root);
            }

            // Return children
            foreach (var child in root.Children)
            {
                foreach (var node in TraverseDF(child, order))
                {
                    yield return(node);
                }
            }

            // Postorder - return root last
            if (order == TreeTraversalOrder.DepthFirstPostOrder)
            {
                yield return(root);
            }
        }