Beispiel #1
0
        public void DepthFirstTraversalExample()
        {
            // Initialize a new graph instance.
            var graph = new Graph <int>(true);

            // Add three vertices to the graph.
            var vertex1 = graph.AddVertex(1);
            var vertex2 = graph.AddVertex(2);
            var vertex3 = graph.AddVertex(3);

            // Add edges between the vertices.
            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex2, vertex3);

            // Create a new counting visitor, which will keep track
            // of the number of objects that are visited.
            var countingVisitor = new CountingVisitor <Vertex <int> >();

            // Define in which order the objects should be visited -
            // we choose to do a pre-order traversal, however, you can
            // also choose to visit post-order.  Note that In-Order is
            // only available on Binary Trees, and not defined on graphs.
            var orderedVisitor = new PreOrderVisitor <Vertex <int> >(countingVisitor);

            // Perform a depth first traversal on the graph with
            // the ordered visitor, starting from node vertex1.
            graph.DepthFirstTraversal(orderedVisitor, vertex1);

            // The visitor will have visited 3 items.
            Assert.AreEqual(3, countingVisitor.Count);
        }
Beispiel #2
0
        public void StopVisitor()
        {
            var binaryTree = GetTestTree();
            var visitor    = new CompletedTrackingVisitor <int>();
            var preVisitor = new PreOrderVisitor <int>(visitor);

            binaryTree.DepthFirstTraversal(preVisitor);
        }
Beispiel #3
0
        public void StopVisitor()
        {
            var binaryTree = GetTestTree();
            var visitor = new CompletedTrackingVisitor<int>();
            var preVisitor = new PreOrderVisitor<int>(visitor);

            binaryTree.DepthFirstTraversal(preVisitor);
        }
        public void DepthFirstTraversal_Stops_Once_Visitor_Completes()
        {
            var generalTree = GetTestTree();
            var visitor     = new StoppingVisitor <int>();
            var preVisitor  = new PreOrderVisitor <int>(visitor);

            generalTree.DepthFirstTraversal(preVisitor);
        }
        public void DepthFirstTraversal_VisitPre()
        {
            var generalTree     = GetTestTree();
            var trackingVisitor = new AssertionVisitor <int>();
            var preVisitor      = new PreOrderVisitor <int>(trackingVisitor);

            generalTree.DepthFirstTraversal(preVisitor);
            trackingVisitor.AssertTracked(5, 2, 9, 12, 3, 13, 1);
        }
Beispiel #6
0
        public void Pre()
        {
            var binaryTree      = GetTestTree();
            var trackingVisitor = new TrackingVisitor <int>();
            var preVisitor      = new PreOrderVisitor <int>(trackingVisitor);

            binaryTree.DepthFirstTraversal(preVisitor);

            var tracks = trackingVisitor.TrackingList;

            Assert.AreEqual(tracks[0], 5);
            Assert.AreEqual(tracks[1], 2);
            Assert.AreEqual(tracks[2], 9);
            Assert.AreEqual(tracks[3], 12);
            Assert.AreEqual(tracks[4], 3);
            Assert.AreEqual(tracks[5], 13);
        }
Beispiel #7
0
        public void Pre()
        {
            var binaryTree = GetTestTree();
            var trackingVisitor = new TrackingVisitor<int>();
            var preVisitor = new PreOrderVisitor<int>(trackingVisitor);

            binaryTree.DepthFirstTraversal(preVisitor);

            var tracks = trackingVisitor.TrackingList;

            Assert.AreEqual(tracks[0], 5);
            Assert.AreEqual(tracks[1], 2);
            Assert.AreEqual(tracks[2], 9);
            Assert.AreEqual(tracks[3], 12);
            Assert.AreEqual(tracks[4], 3);
            Assert.AreEqual(tracks[5], 13);
        }
Beispiel #8
0
        public void TestDepthFirstVisitPre()
        {
            BinaryTree <int>      t = GetTestTree();
            TrackingVisitor <int> trackingVisitor = new TrackingVisitor <int>();
            PreOrderVisitor <int> preVisitor      = new PreOrderVisitor <int>(trackingVisitor);

            t.DepthFirstTraversal(preVisitor);

            VisitableList <int> tracks = trackingVisitor.TrackingList;

            Assert.AreEqual(tracks[0], 5);
            Assert.AreEqual(tracks[1], 2);
            Assert.AreEqual(tracks[2], 9);
            Assert.AreEqual(tracks[3], 12);
            Assert.AreEqual(tracks[4], 3);
            Assert.AreEqual(tracks[5], 13);
        }
Beispiel #9
0
        public void StopVisitor()
        {
            var generalTree = GetTestTree();
            var visitor     = new ComparableFindingVisitor <int>(13);
            var preVisitor  = new PreOrderVisitor <int>(visitor);

            generalTree.DepthFirstTraversal(preVisitor);

            Assert.IsTrue(((ComparableFindingVisitor <int>)preVisitor.VisitorToUse).Found);
            Assert.IsTrue(((ComparableFindingVisitor <int>)preVisitor.VisitorToUse).HasCompleted);

            visitor    = new ComparableFindingVisitor <int>(99);
            preVisitor = new PreOrderVisitor <int>(visitor);

            generalTree.DepthFirstTraversal(preVisitor);
            Assert.IsFalse(((ComparableFindingVisitor <int>)preVisitor.VisitorToUse).Found);
            Assert.IsFalse(((ComparableFindingVisitor <int>)preVisitor.VisitorToUse).HasCompleted);
        }
Beispiel #10
0
        public void TestDepthFirstStopVisitor()
        {
            GeneralTree <int> t = GetTestTree();
            ComparableFindingVisitor <int> visitor    = new ComparableFindingVisitor <int>(13);
            PreOrderVisitor <int>          preVisitor = new PreOrderVisitor <int>(visitor);

            t.DepthFirstTraversal(preVisitor);

            Assert.AreEqual(((ComparableFindingVisitor <int>)preVisitor.VisitorToUse).Found, true);
            Assert.AreEqual(((ComparableFindingVisitor <int>)preVisitor.VisitorToUse).HasCompleted, true);

            visitor    = new ComparableFindingVisitor <int>(99);
            preVisitor = new PreOrderVisitor <int>(visitor);

            t.DepthFirstTraversal(preVisitor);
            Assert.AreEqual(((ComparableFindingVisitor <int>)preVisitor.VisitorToUse).Found, false);
            Assert.AreEqual(((ComparableFindingVisitor <int>)preVisitor.VisitorToUse).HasCompleted, false);
        }
Beispiel #11
0
        public void UndirectedPreVisit()
        {
            var graph   = new Graph <int>(false);
            var vertex1 = new Vertex <int>(1);
            var vertex2 = new Vertex <int>(2);
            var vertex3 = new Vertex <int>(3);
            var vertex4 = new Vertex <int>(4);
            var vertex5 = new Vertex <int>(5);
            var vertex6 = new Vertex <int>(6);
            var vertex7 = new Vertex <int>(7);

            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);
            graph.AddVertex(vertex3);
            graph.AddVertex(vertex4);
            graph.AddVertex(vertex5);
            graph.AddVertex(vertex6);
            graph.AddVertex(vertex7);

            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex1, vertex3);
            graph.AddEdge(vertex1, vertex5);
            graph.AddEdge(vertex5, vertex3);

            graph.AddEdge(vertex3, vertex6);
            graph.AddEdge(vertex3, vertex4);
            graph.AddEdge(vertex2, vertex7);

            var trackingVisitor = new TrackingVisitor <Vertex <int> >();
            var preOrderVisitor = new PreOrderVisitor <Vertex <int> >(trackingVisitor);

            graph.DepthFirstTraversal(preOrderVisitor, vertex1);

            Assert.AreEqual(trackingVisitor.TrackingList.Count, graph.Vertices.Count);

            Assert.AreEqual(trackingVisitor.TrackingList[0].Data, 1);
            Assert.AreEqual(trackingVisitor.TrackingList[1].Data, 2);
            Assert.AreEqual(trackingVisitor.TrackingList[2].Data, 7);
            Assert.AreEqual(trackingVisitor.TrackingList[3].Data, 3);
            Assert.AreEqual(trackingVisitor.TrackingList[4].Data, 5);
            Assert.AreEqual(trackingVisitor.TrackingList[5].Data, 6);
            Assert.AreEqual(trackingVisitor.TrackingList[6].Data, 4);
        }
Beispiel #12
0
        public void DepthFirstTraversalExample()
        {
            // Initialize a new graph instance.
            var graph = new Graph<int>(true);

            // Add three vertices to the graph.
            var vertex1 = graph.AddVertex(1);
            var vertex2 = graph.AddVertex(2);
            var vertex3 = graph.AddVertex(3);

            // Add edges between the vertices.
            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex2, vertex3);

            // Create a new counting visitor, which will keep track
            // of the number of objects that are visited.
            var countingVisitor = new CountingVisitor<Vertex<int>>();

            // Define in which order the objects should be visited -
            // we choose to do a pre-order traversal, however, you can
            // also choose to visit post-order.  Note that In-Order is
            // only available on Binary Trees, and not defined on graphs.
            var orderedVisitor = new PreOrderVisitor<Vertex<int>>(countingVisitor);

            // Perform a depth first traversal on the graph with
            // the ordered visitor, starting from node vertex1.
            graph.DepthFirstTraversal(orderedVisitor, vertex1);

            // The visitor will have visited 3 items.
            Assert.AreEqual(countingVisitor.Count, 3);
        }
        public void UndirectedPreVisit()
        {
            var graph = new Graph<int>(false);
            var vertex1 = new Vertex<int>(1);
            var vertex2 = new Vertex<int>(2);
            var vertex3 = new Vertex<int>(3);
            var vertex4 = new Vertex<int>(4);
            var vertex5 = new Vertex<int>(5);
            var vertex6 = new Vertex<int>(6);
            var vertex7 = new Vertex<int>(7);

            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);
            graph.AddVertex(vertex3);
            graph.AddVertex(vertex4);
            graph.AddVertex(vertex5);
            graph.AddVertex(vertex6);
            graph.AddVertex(vertex7);

            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex1, vertex3);
            graph.AddEdge(vertex1, vertex5);
            graph.AddEdge(vertex5, vertex3);

            graph.AddEdge(vertex3, vertex6);
            graph.AddEdge(vertex3, vertex4);
            graph.AddEdge(vertex2, vertex7);

            var trackingVisitor = new TrackingVisitor<Vertex<int>>();
            var preOrderVisitor = new PreOrderVisitor<Vertex<int>>(trackingVisitor);

            graph.DepthFirstTraversal(preOrderVisitor, vertex1);

            Assert.AreEqual(trackingVisitor.TrackingList.Count, graph.Vertices.Count);

            Assert.AreEqual(trackingVisitor.TrackingList[0].Data, 1);
            Assert.AreEqual(trackingVisitor.TrackingList[1].Data, 2);
            Assert.AreEqual(trackingVisitor.TrackingList[2].Data, 7);
            Assert.AreEqual(trackingVisitor.TrackingList[3].Data, 3);
            Assert.AreEqual(trackingVisitor.TrackingList[4].Data, 5);
            Assert.AreEqual(trackingVisitor.TrackingList[5].Data, 6);
            Assert.AreEqual(trackingVisitor.TrackingList[6].Data, 4);
        }