Пример #1
0
        public void TestWrap()
        {
            /*
             *  Orignal
             *          1
             *         / \
             *        2   3
             *           / \
             *          4   5
             */
            var s    = "1,2,X,X,3,4,X,X,5";
            var obj  = new Codec();
            var root = obj.Deserialize(s);

            /*
             *  Inverted
             *          1
             *         / \
             *        3   2
             *       / \
             *      5   4
             */

            s = "1,3,5,X,X,4,X,X,2";
            var expected = obj.Deserialize(s);
            var res      = InvertTree(root);
            var treeTest = new SameTree();

            Assert.True(treeTest.IsSameTree(expected, res));

            /*
             * Original
             *         1
             *        / \
             *       2   3
             *      / \   \
             *     4   5   6
             */

            s    = "1,2,4,X,X,5,X,X,3,X,6";
            root = obj.Deserialize(s);

            /*
             *  Inverted
             *          1
             *         / \
             *        3   2
             *       /   / \
             *      6   5   4
             *
             */

            s        = "1,3,6,X,X,X,2,5,X,X,4";
            expected = obj.Deserialize(s);
            res      = InvertTree(root);
            Assert.True(treeTest.IsSameTree(expected, res));

            /*
             *  Original
             *          1
             *         / \
             *        3   2
             *       /
             *      5
             */

            s    = "1,3,5,X,X,X,2";
            root = obj.Deserialize(s);

            /*
             *  Inverted
             *          1
             *         / \
             *        2   3
             \
             \              5
             */

            s        = "1,2,X,X,3,X,5";
            expected = obj.Deserialize(s);
            res      = InvertTree(root);
            Assert.True(treeTest.IsSameTree(expected, res));

            /*
             *  Original
             *          1
             *         /
             *        3
             *       /
             *      5
             */

            s    = "1,3,5,X,X,X";
            root = obj.Deserialize(s);

            /*
             *  Inverted
             *         1
             \
             \           3
             \
             \             5
             */

            s        = "1,X,3,X,5";
            expected = obj.Deserialize(s);
            res      = InvertTree(root);
            Assert.True(treeTest.IsSameTree(expected, res));

            /*
             *  Original
             *          1
             *         / \
             *        3   2
             *       /     \
             *      5       6
             *     /         \
             *    7           8
             */

            s    = "1,3,5,7,X,X,X,X,2,X,6,X,8";
            root = obj.Deserialize(s);

            /*
             *  Inverted
             *          1
             *         / \
             *        2   3
             *       /     \
             *      6       5
             *     /         \
             *    8           7
             */


            s        = "1,2,6,8,X,X,X,X,3,X,5,X,7";
            expected = obj.Deserialize(s);
            res      = InvertTree(root);
            Assert.True(treeTest.IsSameTree(expected, res));
        }
Пример #2
0
        public void TestWrap()
        {
            /*
             *          1                     3
             *         / \                   / \
             *        2   3                 4   5
             *           / \
             *          4   5
             */
            var s1    = "1,2,X,X,3,4,X,X,5";
            var s2    = "3,4,X,X,5";
            var obj   = new Codec();
            var root1 = obj.Deserialize(s1);
            var root2 = obj.Deserialize(s2);

            Assert.True(IsSubtree(root1, root2));

            /*
             *         1                     2
             *        / \                   / \
             *       2   3                 4   5
             *      / \   \
             *     4   5   6
             */

            s1    = "1,2,4,X,X,5,X,X,3,X,6";
            s2    = "2,4,X,X,5";
            root1 = obj.Deserialize(s1);
            root2 = obj.Deserialize(s2);
            Assert.True(IsSubtree(root1, root2));

            /*
             *          1                   3
             *         / \                 /
             *        3   2               5
             *       /
             *      5
             */

            s1    = "1,3,5,X,X,X,2";
            s2    = "3,5";
            root1 = obj.Deserialize(s1);
            root2 = obj.Deserialize(s2);
            Assert.True(IsSubtree(root1, root2));

            /*
             *          1                   3
             *         /                   /
             *        3                   5
             *       /
             *      5
             */

            s1    = "1,3,5,X,X,X";
            s2    = "3,5";
            root1 = obj.Deserialize(s1);
            root2 = obj.Deserialize(s2);
            Assert.True(IsSubtree(root1, root2));

            /*
             *          1                      6
             *         / \                      \
             *        3   2                      8
             *       /     \
             *      5       6
             *     /         \
             *    7           8
             */

            s1    = "1,3,5,7,X,X,X,X,2,X,6,X,8";
            s2    = "6,X,X,8";
            root1 = obj.Deserialize(s1);
            root2 = obj.Deserialize(s2);
            Assert.True(IsSubtree(root1, root2));
        }