Esempio n. 1
0
        static void Main(string[] args)
        {
            int   V = 5;
            Graph g = new Graph(5);

            g.AddEdge(0, 1);
            g.AddEdge(0, 4);
            g.AddEdge(1, 2);
            g.AddEdge(1, 3);
            g.AddEdge(1, 4);
            g.AddEdge(2, 3);
            g.AddEdge(3, 4);
            g.PrintList();

            Thread.Sleep(5000);

            GraphAsMatrix g1 = new GraphAsMatrix(5);

            g1.AddEdge(0, 1);
            g1.AddEdge(0, 4);
            g1.AddEdge(1, 2);
            g1.AddEdge(1, 3);
            g1.AddEdge(1, 4);
            g1.AddEdge(2, 3);
            g1.AddEdge(3, 4);
            g1.PrintGraph();

            Thread.Sleep(5000);

            GraphTraval graph = new GraphTraval(4);

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 0);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 3);
            //Print adjacency matrix
            graph.PrintAdjacecnyMatrix();

            Console.WriteLine("BFS traversal starting from vertex 2:");
            graph.BFS(2);
            Console.WriteLine("DFS traversal starting from vertex 2:");
            graph.DFS(2);

            Thread.Sleep(5000);

            BinaryTree tree = new BinaryTree();

            tree.root            = new Node(1);
            tree.root.left       = new Node(2);
            tree.root.right      = new Node(3);
            tree.root.left.left  = new Node(4);
            tree.root.left.right = new Node(5);
            tree.Inorder();

            Console.ReadLine();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Graph AdjList  ");
            int   V = 5;
            Graph g = new Graph(5);

            g.AddEdge(0, 1);
            g.AddEdge(0, 4);
            g.AddEdge(1, 2);
            g.AddEdge(1, 3);
            g.AddEdge(1, 4);
            g.AddEdge(2, 3);
            g.AddEdge(3, 4);
            g.PrintList();

            Thread.Sleep(5000);

            GraphAsMatrix g1 = new GraphAsMatrix(5);

            g1.AddEdge(0, 1);
            g1.AddEdge(0, 4);
            g1.AddEdge(1, 2);
            g1.AddEdge(1, 3);
            g1.AddEdge(1, 4);
            g1.AddEdge(2, 3);
            g1.AddEdge(3, 4);
            g1.PrintGraph();

            Thread.Sleep(5000);
            Console.WriteLine("Graph AdjMatrix  ");
            GraphTraval graph = new GraphTraval(4);

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 0);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 3);
            //Print adjacency matrix
            graph.PrintAdjacecnyMatrix();

            Console.WriteLine("BFS traversal starting from vertex 2:");
            graph.BFS(2);
            Console.WriteLine("DFS traversal starting from vertex 2:");
            graph.DFS(2);

            Thread.Sleep(5000);

            Console.WriteLine("Tree InOrder input as 1 2 3 4 5  ");
            BinaryTree tree = new BinaryTree();

            tree.root            = new Node(1);
            tree.root.left       = new Node(2);
            tree.root.right      = new Node(3);
            tree.root.left.left  = new Node(4);
            tree.root.left.right = new Node(5);
            tree.Inorder();



            Thread.Sleep(5000);
            Console.WriteLine("PreOrder input 10 8 2 3 5 2");
            //BinaryTree tree1 = new BinaryTree();
            tree.root            = new Node(10);
            tree.root.left       = new Node(8);
            tree.root.right      = new Node(2);
            tree.root.left.left  = new Node(3);
            tree.root.left.right = new Node(5);
            tree.root.right.left = new Node(2);
            tree.PreOrder();

            Thread.Sleep(5000);
            Console.WriteLine("PostOrder input 10 8 2 3 5 2");
            //BinaryTree tree1 = new BinaryTree();
            tree.root            = new Node(10);
            tree.root.left       = new Node(8);
            tree.root.right      = new Node(2);
            tree.root.left.left  = new Node(3);
            tree.root.left.right = new Node(5);
            tree.root.right.left = new Node(2);
            tree.PostOrder();

            Thread.Sleep(5000);

            Node root = new Node(1);

            root.left        = new Node(2);
            root.right       = new Node(3);
            root.left.left   = new Node(4);
            root.left.right  = new Node(5);
            root.right.right = new Node(6);

            tree.LevelOrder(root);

            Console.ReadLine();
        }