Пример #1
0
        private void ConstructTreeGraph(ITreeAlgorithms <TNode> algorithm = null)
        {
            Stack <TNode> nodes = new Stack <TNode>();

            Action <TNode> nodeAction = (node) =>
            {
                AddNode(node, algorithm);
                nodes.Push(node);
            };

            Action <TNode> postAction = (node) =>
            {
                nodes.Pop();

                if (nodes.Count > 0)
                {
                    AddNewTreeEdge(nodes.Peek(), node, algorithm);
                }
            };

            var alg = new TreeAlgorithms <TNode>();

            alg.DiscoverNode += nodeAction;
            alg.FinishNode   += postAction;
            alg.DFS(tree);
        }
Пример #2
0
        public void Test1()
        {
            List <NodeListItem> nodeList = new List <NodeListItem>();

            nodeList.Add(new NodeListItem {
                Id = 2, ParentId = 4
            });
            nodeList.Add(new NodeListItem {
                Id = 1, ParentId = 2
            });
            nodeList.Add(new NodeListItem {
                Id = 6, ParentId = 4
            });
            nodeList.Add(new NodeListItem {
                Id = 4, ParentId = null
            });
            nodeList.Add(new NodeListItem {
                Id = 8, ParentId = 6
            });
            nodeList.Add(new NodeListItem {
                Id = 7, ParentId = 2
            });
            nodeList.Add(new NodeListItem {
                Id = 3, ParentId = 6
            });

            Node rootNode = TreeAlgorithms.ProcessAndPrintTree(nodeList);

            Assert.AreEqual(rootNode.Id, 4);
        }
Пример #3
0
        public Graph Prim(FromVertex from)
        {
            Graph graph = from.graph;
            AdjacencyList <string> adjacencyList = graph;

            return(TreeAlgorithms <string> .Prim(adjacencyList, from.orig));
        }
Пример #4
0
        public Graph Kruskal(FromVertex from)
        {
            Graph graph = from.graph;
            AdjacencyList <char> adjacencyList = graph;

            return(TreeAlgorithms <char> .Kruskal(adjacencyList));
        }
Пример #5
0
        public Graph BellmanFord(FromVertex from)
        {
            Graph graph = from.graph;
            AdjacencyList <string> adjacencyList = graph;

            TreeAlgorithms <string> .BellmanFord(adjacencyList, from.orig, out GraphLib.Model.Graph <string> q);

            AdjacencyMatrix <string> output = (AdjacencyMatrix <string>)q;

            return(output);
        }
        public void BinaryTreeAlgorithms_Test()
        {
            var tree1 = new BinarySearchTree <int>();

            tree1.Add(24);
            tree1.Add(12);
            tree1.Add(15);
            tree1.Add(67);
            tree1.Add(32);
            tree1.Add(13);
            tree1.Add(89);
            tree1.Add(9);



            tree1.Remove(13);
            tree1.Remove(24);
            //Assert.IsTrue(tree1.PathSum(tree1.Root(), 45));
            //Assert.IsTrue(tree1.PathSum(tree1.Root(), 51));
            //Assert.IsFalse(tree1.PathSum(tree1.Root(), 42));

            //Assert.AreEqual(8, tree1.Size(tree1.Root()));

            //Assert.AreEqual(9, tree1.Minimum(tree1.Root()));

            //Assert.AreEqual(89, tree1.Maximum(tree1.Root()));

            //Assert.IsTrue(BinaryTree.AreTwoTreesEqual(tree1.Root(), tree2.Root()));

            Console.WriteLine("Inorder traversal ");
            tree1.Inorder(tree1.Root);
            Console.WriteLine(" ");

            TreeAlgorithms <int> .Mirror(tree1.Root);

            Console.WriteLine("Inorder traversal of the Mirror tree");
            tree1.Inorder(tree1.Root);
            Console.WriteLine(" ");

            Console.WriteLine();
            Console.WriteLine("Preorder traversal");
            tree1.Preorder(tree1.Root);
            Console.WriteLine(" ");

            Console.WriteLine();
            Console.WriteLine("Postorder traversal");
            tree1.Postorder(tree1.Root);
            Console.WriteLine(" ");
        }
Пример #7
0
        static void Main(string[] args)
        {
            AdjacencyMatrix <char> t = new AdjacencyMatrix <char>((u, v) => u.CompareTo(v), true);

            t.AddVertex('s');
            t.AddVertex('t');
            t.AddVertex('x');
            t.AddVertex('y');
            t.AddVertex('z');

            t.AddEdge('s', 't', 10);
            t.AddEdge('s', 'y', 5);

            t.AddEdge('t', 'x', 1);
            t.AddEdge('t', 'y', 2);

            t.AddEdge('x', 'z', 4);

            t.AddEdge('y', 't', 3);
            t.AddEdge('y', 'z', 2);
            t.AddEdge('y', 'x', 9);

            t.AddEdge('z', 'x', 6);

            AdjacencyList <char> l = (AdjacencyList <char>)t;

            Console.WriteLine(t);
            Console.WriteLine(l);

            Console.WriteLine(TreeAlgorithms <char> .Dijkstra(t, 's'));
            Console.WriteLine((AdjacencyList <char>)TreeAlgorithms <char> .Dijkstra(l, 's'));

            TreeAlgorithms <char> .BellmanFord(t, 's', out Graph <char> to);

            TreeAlgorithms <char> .BellmanFord(t, 's', out Graph <char> lo);

            AdjacencyList <char> ttt = (AdjacencyMatrix <char>)lo;

            Console.WriteLine(to);
            Console.WriteLine(ttt);
        }
        static void Main(String[] args)
        {
            try
            {
                //   var s = string.Split("");
                // Regex.Split(source, @"(?<!^)(?=[A-Z])");
                //  Console.ReadLine();
                //var buff = Regex.Matches(input, @"(.)\1");
                SortingAlgorithms.QuickSortMethod();
                BinarySearchTree b = new BinarySearchTree(8);
                b.Insert(4);
                b.Insert(9);
                b.Insert(1);
                b.Insert(2);
                b.Insert(3);
                b.Insert(6);
                b.Insert(5);
                var list = LevelOrder(b.Root);

                var lcaNode  = TreeAlgorithms.LeastCommonAncestry(b.Root, 1, 2);
                var Distance = TreeAlgorithms.GetDistance(b.Root, 3);
                System.Console.WriteLine(string.Format("LCA : {0}\nDistance: {1}", lcaNode.Data, Distance));
                System.Console.ReadLine();

                int[] inOrder  = { 4, 2, 5, 1, 3 };
                int[] preOrder = { 1, 2, 4, 5, 3 };
                int   len      = inOrder.Count() - 1;
                var   tree     = BinarySearchTree.BuildTree(inOrder, preOrder, 0, len);

                Display(tree);
                System.Console.WriteLine("Press any key...");
                var name = System.Console.ReadLine();
            }
            catch (Exception ex)
            {
                System.Console.Write(ex.Message + ex.StackTrace);
                System.Console.WriteLine("Press any key...");
                var name = System.Console.ReadLine();
            }
        }
        public void BinaryTreeAlgorithms_Mirror_Test()
        {
            var tree1 = new BinarySearchTree <int>();

            tree1.Add(24);
            tree1.Add(12);
            tree1.Add(15);
            tree1.Add(67);
            tree1.Add(32);
            tree1.Add(13);
            tree1.Add(89);
            tree1.Add(9);

            Console.WriteLine("Inorder traversal ");
            tree1.Inorder(tree1.Root);
            Console.WriteLine(" ");

            TreeAlgorithms <int> .Mirror(tree1.Root);

            Console.WriteLine("Inorder traversal of the Mirror tree");
            tree1.Inorder(tree1.Root);
            Console.WriteLine(" ");
        }
 static void Main(string[] args)
 {
     TreeAlgorithms treeTraversal = new TreeAlgorithms();
     treeTraversal.Execute();
     Console.ReadKey();
 }