/// <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); }
/// <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); }
/// <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); } }
/// <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); }
/// <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); }
public void Simple() { var visitor = new TrackingVisitor<int>(); var orderedVisitor = new OrderedVisitor<int>(visitor); Assert.IsFalse(orderedVisitor.HasCompleted); Assert.AreSame(orderedVisitor.VisitorToUse, visitor); }
public void Simple() { var visitor = new TrackingVisitor <int>(); var orderedVisitor = new OrderedVisitor <int>(visitor); Assert.IsFalse(orderedVisitor.HasCompleted); Assert.AreSame(orderedVisitor.VisitorToUse, visitor); }
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); }
/// <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); } }
/// <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); }
/// <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); }
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)); }
/// <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); } }
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(); }
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(); }
/// <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); }
public void DepthFirstTraversal(OrderedVisitor <T> visitor) { Guard.ArgumentNotNull(visitor, "visitor"); VisitNode(tree, visitor); }
DepthFirstTraversal(OrderedVisitor <T> orderedVisitor);