예제 #1
0
        private void PrintTree(Node root, string pre)
        {
            LoopTraverse traverse = new LoopTraverse();
            var          inValues = traverse.In(root).Select(x => x.V).ToArray();

            Console.WriteLine("{0}{1}", pre, string.Join(",", inValues));
        }
예제 #2
0
        public void Run()
        {
            int[] inValues   = new int[] { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16 };
            int[] preValues  = new int[] { 10, 6, 4, 3, 5, 8, 7, 9, 14, 12, 11, 13, 16 };
            int[] postValues = new int[] { 3, 5, 4, 7, 9, 8, 6, 11, 13, 12, 16, 14, 10 };
            // preValues = new int[] { 2, 1, 3 };
            // inValues = new int[] { 1, 2, 3 };
            // postValues = new int[] { 3, 1, 2 };

            Construct    construct = new Construct();
            LoopTraverse traverse  = new LoopTraverse();
            // var root = construct.PreAndIn(preValues, inValues);
            // Match(preValues, root, traverse.Pre, "preandin");

            var root = construct.PreAndInWithRecur(preValues, inValues);

            Match(preValues, root, traverse.Pre, "PreAndInWithRecur");

            // root = construct.InAndPost(inValues, postValues);
            // Match(postValues, root, traverse.Post, "InAndPost");

            // var root = construct.InAndPostWithRecur(inValues, postValues);
            // Match(postValues, root, traverse.Post, "InAndPostWithRecur");

            // preValues = new int[] { 10, 6, 4, 3, 5, 8, 7, 9, 14, 12, 11, 13, 16 };
            // root = construct.Pre(preValues);
            // Match(preValues, root, traverse.Pre, "bst pre");

            // root = construct.Post(postValues);
            // Match(postValues, root, traverse.Post, "bst post");
        }
예제 #3
0
        public void Run()
        {
            var root = BuildTree();

            Console.WriteLine("Recursive: ");
            RecurTraverse recurTraverse = new RecurTraverse();

            IEnumerable <Node> nodes = recurTraverse.Pre(root);

            PrintNodes(nodes, "Pre");
            nodes = recurTraverse.In(root);
            PrintNodes(nodes, "In");
            nodes = recurTraverse.Post(root);
            PrintNodes(nodes, "Post");

            Console.WriteLine("Loop: ");
            LoopTraverse loopTraverse = new LoopTraverse();

            nodes = loopTraverse.PreWithSimple(root);
            PrintNodes(nodes, "PreWithSimple");
            nodes = loopTraverse.PreWithState(root);
            PrintNodes(nodes, "PreWithState");
            nodes = loopTraverse.Pre(root);
            PrintNodes(nodes, "Pre");
            nodes = loopTraverse.PreWithMorris(root);
            PrintNodes(nodes, "PreWithMorris");
            nodes = loopTraverse.InWithState(root);
            PrintNodes(nodes, "InWithState");
            nodes = loopTraverse.In(root);
            PrintNodes(nodes, "In");
            nodes = loopTraverse.InWithMorris(root);
            PrintNodes(nodes, "InWithMorris");
            nodes = loopTraverse.PostWithState(root);
            PrintNodes(nodes, "PostWithState");
            nodes = loopTraverse.Post(root);
            PrintNodes(nodes, "Post");
            nodes = loopTraverse.PostWithMorris(root);
            PrintNodes(nodes, "PostWithMorris");
            nodes = loopTraverse.Row(root);
            PrintNodes(nodes, "Row");

            var nodesList = loopTraverse.RowList(root);

            PrintNodes(nodesList, "RowList");
            nodesList = loopTraverse.ColumnList(root);
            PrintNodes(nodesList, "ColumnList");
        }