Пример #1
0
        /// <summary>
        /// Performs a breadth first traversal on this tree with the specified visitor.
        /// </summary>
        /// <param name="visitor">The visitor.</param>
        public virtual void BreadthFirstTraversal(IVisitor <T> visitor)
        {
            VisitableQueue <BinaryTree <T> > q = new VisitableQueue <BinaryTree <T> >();

            q.Enqueue(this);

            while (!q.IsEmpty)
            {
                BinaryTree <T> t = q.Dequeue();
                visitor.Visit(t.Data);

                for (int i = 0; i < t.Degree; i++)
                {
                    BinaryTree <T> child = t.GetChild(i);

                    if (child != null)
                    {
                        q.Enqueue(child);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Performs a breadth-first traversal from the specified vertex.
        /// </summary>
        /// <param name="visitor">The visitor to use.</param>
        /// <param name="startVertex">The vertex to start from.</param>
        public void BreadthFirstTraversal(IVisitor <Vertex <T> > visitor, Vertex <T> startVertex)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException("visitor");
            }

            if (startVertex == null)
            {
                throw new ArgumentNullException("startVertex");
            }

            List <Vertex <T> > visitedVertices = new List <Vertex <T> >(graphVertices.Count);

            VisitableQueue <Vertex <T> > q = new VisitableQueue <Vertex <T> >();

            q.Enqueue(startVertex);
            visitedVertices.Add(startVertex);

            while (!((q.IsEmpty) || (visitor.HasCompleted)))
            {
                Vertex <T> vertex = q.Dequeue();

                visitor.Visit(vertex);

                List <Edge <T> > edges = vertex.EmanatingEdgeList;

                for (int i = 0; i < edges.Count; i++)
                {
                    Vertex <T> vertexToVisit = edges[i].GetPartnerVertex(vertex);

                    if (!visitedVertices.Contains(vertexToVisit))
                    {
                        q.Enqueue(vertexToVisit);
                        visitedVertices.Add(vertexToVisit);
                    }
                }
            }
        }