Example #1
0
        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="obj">An object to compare with this instance.</param>
        /// <returns>
        /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance is less than obj. Zero This instance is equal to obj. Greater than zero This instance is greater than obj.
        /// </returns>
        /// <exception cref="T:System.ArgumentException">obj is not the same type as this instance. </exception>
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (obj.GetType() == this.GetType())
            {
                VisitableQueue <T> q = obj as VisitableQueue <T>;

                return(this.Count.CompareTo(q.Count));
            }
            else
            {
                return(this.GetType().FullName.CompareTo(obj.GetType().FullName));
            }
        }
Example #2
0
        /// <summary>
        /// Accepts the specified visitor.
        /// </summary>
        /// <param name="visitor">The visitor.</param>
        public void Accept(IVisitor <T> visitor)
        {
            if (visitor == null)
            {
                throw new ArgumentNullException("visitor");
            }

            VisitableQueue <T> .Enumerator enumerator = this.GetEnumerator();

            while (enumerator.MoveNext())
            {
                visitor.Visit(enumerator.Current);

                if (visitor.HasCompleted)
                {
                    break;
                }
            }
        }
Example #3
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);
                    }
                }
            }
        }
Example #4
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);
                    }
                }
            }
        }