Beispiel #1
0
        private IEnumerable <TreeNode <T> > GetBreadthFirstEnumerable(TreeTraversalDirection TraversalDirection)
        {
            if (TraversalDirection == TreeTraversalDirection.BottomUp)
            {
                var stack = new Stack <TreeNode <T> >();
                foreach (var item in GetBreadthFirstEnumerable(TreeTraversalDirection.TopDown))
                {
                    stack.Push(item);
                }
                while (stack.Count > 0)
                {
                    yield return(stack.Pop());
                }
                yield break;
            }

            var queue = new Queue <TreeNode <T> >();

            queue.Enqueue(this);

            while (0 < queue.Count)
            {
                TreeNode <T> node = queue.Dequeue();

                foreach (TreeNode <T> child in node.Children)
                {
                    queue.Enqueue(child);
                }

                yield return(node);
            }
        }
Beispiel #2
0
        // TODO: adjust for traversal direction
        private IEnumerator <SimpleTreeNode <T> > GetDepthFirstEnumerator(TreeTraversalDirection TraversalDirection)
        {
            yield return(this);

            foreach (SimpleTreeNode <T> child in Children)
            {
                IEnumerator <SimpleTreeNode <T> > enumerator = child.GetDepthFirstEnumerator(TraversalDirection);
                while (enumerator.MoveNext())
                {
                    yield return(enumerator.Current);
                }
            }
        }
Beispiel #3
0
        // TODO: adjust for traversal direction
        private IEnumerator <SimpleTreeNode <T> > GetBreadthFirstEnumerator(TreeTraversalDirection TraversalDirection)
        {
            Queue <SimpleTreeNode <T> > queue = new Queue <SimpleTreeNode <T> >();

            queue.Enqueue(this);
            while (0 < queue.Count)
            {
                SimpleTreeNode <T> node = queue.Dequeue();
                foreach (SimpleTreeNode <T> child in node.Children)
                {
                    queue.Enqueue(child);
                }
                yield return(node);
            }
        }
Beispiel #4
0
        private IEnumerable <TreeNode <T> > GetDepthFirstEnumerable(TreeTraversalDirection TraversalDirection)
        {
            if (TraversalDirection == TreeTraversalDirection.TopDown)
            {
                yield return(this);
            }

            foreach (TreeNode <T> child in Children)
            {
                var e = child.GetDepthFirstEnumerable(TraversalDirection).GetEnumerator();
                while (e.MoveNext())
                {
                    yield return(e.Current);
                }
            }

            if (TraversalDirection == TreeTraversalDirection.BottomUp)
            {
                yield return(this);
            }
        }
Beispiel #5
0
        // TODO: adjust for traversal direction
        private IEnumerable <SimpleTreeNode <T> > GetBreadthFirstEnumerable(TreeTraversalDirection TraversalDirection)
        {
            if (TraversalDirection == TreeTraversalDirection.BottomUp)
            {
                var stack = new Stack <SimpleTreeNode <T> >();
                foreach (var item in GetBreadthFirstEnumerable(TreeTraversalDirection.TopDown))
                {
                    stack.Push(item);
                }
                while (stack.Count > 0)
                {
                    yield return(stack.Pop());
                }
                yield break;
            }

            var queue = new Queue <SimpleTreeNode <T> >();

            queue.Enqueue(this);

            while (queue.Count > 0)
            {
                GridSolver.MoveCount++;

                SimpleTreeNode <T> node = queue.Dequeue();

                foreach (SimpleTreeNode <T> child in node.Children)
                {
                    queue.Enqueue(child);
                }

                if (queue.Count % 1000 == 0)
                {
                    Log.Debug("Queue size: {QueueSize}", queue.Count);
                }

                yield return(node);
            }
        }
Beispiel #6
0
        public IEnumerable <TreeNode <T> > GetEnumerable(TreeTraversalType TraversalType, TreeTraversalDirection TraversalDirection)
        {
            switch (TraversalType)
            {
            case TreeTraversalType.DepthFirst: return(GetDepthFirstEnumerable(TraversalDirection));

            case TreeTraversalType.BreadthFirst: return(GetBreadthFirstEnumerable(TraversalDirection));

            default: return(null);
            }
        }