Пример #1
0
        /// <summary>
        /// Performs a depth first traversal on this tree with the specified visitor.
        /// </summary>
        /// <param name="orderedVisitor">The ordered visitor.</param>
        /// <exception cref="ArgumentNullException"><paramref name="orderedVisitor"/> is a null reference (<c>Nothing</c> in Visual Basic).</exception>
        public virtual void DepthFirstTraversal(OrderedVisitor <T> orderedVisitor)
        {
            Guard.ArgumentNotNull(orderedVisitor, "orderedVisitor");

            if (orderedVisitor.HasCompleted)
            {
                return;
            }

            // Preorder visit
            orderedVisitor.VisitPreOrder(Data);

            if (leftSubtree != null)
            {
                leftSubtree.DepthFirstTraversal(orderedVisitor);
            }

            // In-order visit
            orderedVisitor.VisitInOrder(data);

            if (rightSubtree != null)
            {
                rightSubtree.DepthFirstTraversal(orderedVisitor);
            }

            // PostOrder visit
            orderedVisitor.VisitPostOrder(Data);
        }
Пример #2
0
        /// <summary>
        /// Performs a depth-first traversal.
        /// </summary>
        /// <param name="visitor">The visitor.</param>
        /// <param name="startVertex">The start vertex.</param>
        /// <param name="visitedVertices">The visited vertices.</param>
        private void DepthFirstTraversal(OrderedVisitor <Vertex <T> > visitor, Vertex <T> startVertex, ref List <Vertex <T> > visitedVertices)
        {
            if (visitor.HasCompleted)
            {
                return;
            }

            // Add the vertex to the "visited" list
            visitedVertices.Add(startVertex);

            // Visit the vertex in pre-order
            visitor.VisitPreOrder(startVertex);

            // Get the list of emanating edges from the vertex
            List <Edge <T> > edges = startVertex.EmanatingEdgeList;

            for (int i = 0; i < edges.Count; i++)
            {
                // Get the partner vertex of the start vertex
                Vertex <T> vertexToVisit = edges[i].GetPartnerVertex(startVertex);

                // If the vertex hasn't been visited before, do a depth-first
                // traversal starting at that vertex
                if (!visitedVertices.Contains(vertexToVisit))
                {
                    DepthFirstTraversal(visitor, vertexToVisit, ref visitedVertices);
                }
            }

            // Visit the vertex in post order
            visitor.VisitPostOrder(startVertex);
        }
Пример #3
0
        /// <summary>
        /// Performs a depth first traversal on this tree with the specified visitor.
        /// </summary>
        /// <param name="orderedVisitor">The ordered visitor.</param>
        public virtual void DepthFirstTraversal(OrderedVisitor <T> orderedVisitor)
        {
            if (orderedVisitor.HasCompleted)
            {
                return;
            }
            else
            {
                // Preorder visit
                orderedVisitor.VisitPreOrder(Data);

                if (leftSubtree != null)
                {
                    leftSubtree.DepthFirstTraversal(orderedVisitor);
                }

                // Inorder visit
                orderedVisitor.VisitInOrder(data);

                if (rightSubtree != null)
                {
                    rightSubtree.DepthFirstTraversal(orderedVisitor);
                }

                // PostOrder visit
                orderedVisitor.VisitPostOrder(Data);
            }
        }
Пример #4
0
        /// <summary>
        /// Performs a depth-first traversal.
        /// </summary>
        /// <param name="visitor">The visitor.</param>
        /// <param name="startVertex">The start vertices.</param>
        /// <param name="visitedVertices">The visited vertices.</param>
        private static void DepthFirstTraversal(OrderedVisitor <Vertex <T> > visitor, Vertex <T> startVertex, ref List <Vertex <T> > visitedVertices)
        {
            if (visitor.HasCompleted)
            {
                return;
            }

            // Add the vertices to the "visited" list
            visitedVertices.Add(startVertex);

            // Visit the vertices in pre-order
            visitor.VisitPreOrder(startVertex);

            // Get the list of emanating edges from the vertices
            var edges = startVertex.EmanatingEdges;

            foreach (var e in edges)
            {
// Get the partner vertices of the start vertices
                var vertexToVisit = e.GetPartnerVertex(startVertex);

                // If the vertices hasn't been visited before, do a depth-first
                // traversal starting at that vertices
                if (!visitedVertices.Contains(vertexToVisit))
                {
                    DepthFirstTraversal(visitor, vertexToVisit, ref visitedVertices);
                }
            }

            // Visit the vertices in post order
            visitor.VisitPostOrder(startVertex);
        }
Пример #5
0
 /// <summary>
 /// Performs a depth first traversal on the Red Black Tree.
 /// </summary>
 /// <param name="visitor">The visitor.</param>
 public void DepthFirstTraversal(OrderedVisitor <KeyValuePair <TKey, TValue> > visitor)
 {
     if (visitor == null)
     {
         throw new ArgumentNullException("visitor");
     }
     VisitNode(root, visitor);
 }
Пример #6
0
        public void Simple()
        {
            var visitor = new TrackingVisitor<int>();
            var orderedVisitor = new OrderedVisitor<int>(visitor);

            Assert.IsFalse(orderedVisitor.HasCompleted);
            Assert.AreSame(orderedVisitor.VisitorToUse, visitor);
        }
Пример #7
0
        public void Simple()
        {
            var visitor        = new TrackingVisitor <int>();
            var orderedVisitor = new OrderedVisitor <int>(visitor);

            Assert.IsFalse(orderedVisitor.HasCompleted);
            Assert.AreSame(orderedVisitor.VisitorToUse, visitor);
        }
Пример #8
0
        public void DepthFirstTraversal(OrderedVisitor <Vertex <T> > visitor, Vertex <T> startVertex)
        {
            Guard.ArgumentNotNull(visitor, "visitor");
            Guard.ArgumentNotNull(startVertex, "startVertex");

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

            DepthFirstTraversal(visitor, startVertex, ref visitedVertices);
        }
Пример #9
0
 /// <summary>
 /// Visits the node in an inorder fashion.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="visitor">The visitor.</param>
 private void VisitNode(RedBlackTreeNode <TKey, TValue> node, OrderedVisitor <KeyValuePair <TKey, TValue> > visitor)
 {
     if (node != null)
     {
         var pair = new KeyValuePair <TKey, TValue>(node.Key, node.Value);
         visitor.VisitPreOrder(pair);
         VisitNode(node.Left, visitor);
         visitor.VisitInOrder(pair);
         VisitNode(node.Right, visitor);
         visitor.VisitPostOrder(pair);
     }
 }
Пример #10
0
        /// <summary>
        ///   Performs a depth-first traversal, starting at the specified vertex.
        /// </summary>
        /// <param name="visitor"> The visitor to use. Note that in-order is not applicable in a graph. </param>
        /// <param name="startVertex"> The vertex to start from. </param>
        public void DepthFirstTraversal(OrderedVisitor <Vertex <T> > visitor, Vertex <T> startVertex)
        {
            if (null == visitor)
            {
                throw new ArgumentNullException("visitor");
            }
            if (null == startVertex)
            {
                throw new ArgumentNullException("startVertex");
            }
            var visitedVertices = new List <Vertex <T> >(graphVertices.Count);

            DepthFirstTraversal(visitor, startVertex, ref visitedVertices);
        }
Пример #11
0
 /// <summary>
 ///   Performs a depth first traversal on this tree with the specified visitor.
 /// </summary>
 /// <param name="orderedVisitor"> The ordered visitor. </param>
 public void DepthFirstTraversal(OrderedVisitor <T> orderedVisitor)
 {
     if (orderedVisitor.HasCompleted)
     {
         return;
     }
     orderedVisitor.VisitPreOrder(Data);
     for (var i = 0; i < Degree; i++)
     {
         if (GetChild(i) != null)
         {
             GetChild(i).DepthFirstTraversal(orderedVisitor);
         }
     }
     orderedVisitor.VisitPostOrder(Data);
 }
Пример #12
0
        public void InnerVisitorShouldBeCalledOnEachVisi_Method()
        {
            var innerVisitor   = new Mock <IVisitor <int> >();
            var orderedVisitor = new OrderedVisitor <int>(innerVisitor.Object);


            orderedVisitor.Visit(1);
            orderedVisitor.VisitInOrder(0);
            orderedVisitor.VisitPostOrder(-3);
            orderedVisitor.VisitPreOrder(5);

            innerVisitor.Verify(x => x.Visit(1));
            innerVisitor.Verify(x => x.Visit(0));
            innerVisitor.Verify(x => x.Visit(-3));
            innerVisitor.Verify(x => x.Visit(5));
        }
Пример #13
0
        /// <summary>
        /// Visits the node in an in-order fashion.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="visitor">The visitor.</param>
        private static void VisitNode(BinaryTree <T> node, OrderedVisitor <T> visitor)
        {
            if (node != null)
            {
                var pair = node.Data;

                visitor.VisitPreOrder(pair);

                VisitNode(node.Left, visitor);

                visitor.VisitInOrder(pair);

                VisitNode(node.Right, visitor);

                visitor.VisitPostOrder(pair);
            }
        }
Пример #14
0
        public void InnerVisitorShouldBeCalledOnEachVisi_Method()
        {
            var mockRepository = new MockRepository();
            var innerVisitor = mockRepository.StrictMock<IVisitor<int>>();

            var orderedVisitor = new OrderedVisitor<int>(innerVisitor);

            innerVisitor.Visit(1);
            innerVisitor.Visit(0);
            innerVisitor.Visit(-3);
            innerVisitor.Visit(5);

            mockRepository.ReplayAll();

            orderedVisitor.Visit(1);
            orderedVisitor.VisitInOrder(0);
            orderedVisitor.VisitPostOrder(-3);
            orderedVisitor.VisitPreOrder(5);

            mockRepository.VerifyAll();
        }
Пример #15
0
        public void InnerVisitorShouldBeCalledOnEachVisi_Method()
        {
            var mockRepository = new MockRepository();
            var innerVisitor   = mockRepository.StrictMock <IVisitor <int> >();

            var orderedVisitor = new OrderedVisitor <int>(innerVisitor);

            innerVisitor.Visit(1);
            innerVisitor.Visit(0);
            innerVisitor.Visit(-3);
            innerVisitor.Visit(5);

            mockRepository.ReplayAll();

            orderedVisitor.Visit(1);
            orderedVisitor.VisitInOrder(0);
            orderedVisitor.VisitPostOrder(-3);
            orderedVisitor.VisitPreOrder(5);

            mockRepository.VerifyAll();
        }
Пример #16
0
        /// <summary>
        ///   Performs a depth first traversal on this tree with the specified visitor.
        /// </summary>
        /// <param name="orderedVisitor"> The ordered visitor. </param>
        public void DepthFirstTraversal(OrderedVisitor <T> orderedVisitor)
        {
            if (orderedVisitor.HasCompleted)
            {
                return;
            }

            // Preorder visit
            orderedVisitor.VisitPreOrder(Data);
            if (Left != null)
            {
                Left.DepthFirstTraversal(orderedVisitor);
            }
            // Inorder visit
            orderedVisitor.VisitInOrder(Data);
            if (Right != null)
            {
                Right.DepthFirstTraversal(orderedVisitor);
            }
            // PostOrder visit
            orderedVisitor.VisitPostOrder(Data);
        }
Пример #17
0
        public void DepthFirstTraversal(OrderedVisitor <T> visitor)
        {
            Guard.ArgumentNotNull(visitor, "visitor");

            VisitNode(tree, visitor);
        }
Пример #18
0
 DepthFirstTraversal(OrderedVisitor <T> orderedVisitor);