Beispiel #1
0
        public override void Run()
        {
            var root = Q4_02_CreateMinimalBSTfromSortedUniqueArray.Create(1, 2, 3);

            BTreePrinter.Print(root);
            var results = AllSequences(root);

            foreach (var list in results)
            {
                AssortedMethods.PrintList(list);
            }
        }
        // I wrote the solution based on Topological sort in the Algorithms Book,
        // and its code is here. https://github.com/kevin-wayne/algs4.
        // In topological sort, you are using a directed graph.
        // If a -> b, then it means, b depends on a,
        // in other words, 'a' needs to be built before 'b.'
        // After a topological sort, you expect all the nodes to be arranged such that it is flowing in one direction.
        // Since a cycle is not allowed, the code checks if a cycle exists and returns.
        // In real life, jobs cannot have circular dependencies.
        public override void Run()
        {
            var diGraph = new DirectedGraph('f');

            diGraph.AddEdge('a', 'd'); // d depends on a
            diGraph.AddEdge('f', 'b'); // b depends on f
            diGraph.AddEdge('b', 'd');
            diGraph.AddEdge('f', 'a');
            diGraph.AddEdge('d', 'c');

            var topological = new Topological(diGraph);
            var order       = topological.Order();

            AssortedMethods.PrintList(order);
        }