示例#1
0
        public void Fun()
        {
            var tree = new RedBlackTree <int>();

            tree.AddNode(10);
            tree.AddNode(5);
            tree.AddNode(15);
            tree.AddNode(77);

            Assert.IsTrue(tree.Validate());
            Assert.IsTrue(VerifyTree(tree,
                                     "            10B",
                                     "      5B            15B",
                                     "   -      -       -      77R",
                                     " -   -  -   -   -   -   -   - "));

            tree.AddPair(20, 25);
            Assert.IsTrue(tree.Validate());
            Assert.IsTrue(VerifyTree(tree,
                                     "            10B",
                                     "      5B            20R",
                                     "   -      -      15B     77B",
                                     " -   -  -   -   -   -  25R  - "));

            tree.AddPair(27, 29);
            Assert.IsTrue(tree.Validate());
            Assert.IsTrue(VerifyTree(tree,
                                     "            20B",
                                     "     10R            27R",
                                     "   5B    15B     25B     77B",
                                     " -   -  -   -   -   -  29R  - "));
        }
示例#2
0
        private static RedBlackTree <int> CreateBalancedFourLevel()
        {
            var tree  = new RedBlackTree <int>();
            var items = new[]
            {
                20,
                10, 30,
                4, 15, 24, 35,
                2, 7, 13, 18, 22, 27, 33, 38,
            };

            foreach (var item in items)
            {
                tree.AddNode(item);
            }

            Assert.IsTrue(VerifyTree(tree,
                                     "20B",
                                     "10R 30R",
                                     "4B 15B 24B 35B",
                                     "2R 7R 13R 18R 22R 27R 33R 38R"));
            Assert.AreEqual("2 4 7 10 13 15 18 20 22 24 27 30 33 35 38", string.Join(" ", tree.Items));

            return(tree);
        }
示例#3
0
        private static RedBlackTree <int> CreateBalancedFiveLevel()
        {
            var tree  = new RedBlackTree <int>();
            var items = new[]
            {
                20,
                10, 30,
                4, 15, 24, 35,
                2, 7, 13, 18, 22, 27, 33, 38,
                1, 3, 6, 8, 11, 14, 16, 19, 21, 23, 26, 28, 31, 34, 36, 39
            };

            foreach (var item in items)
            {
                tree.AddNode(item);
            }

            Assert.IsTrue(VerifyTree(tree,
                                     "20B",
                                     "10B 30B",
                                     "4R 15R 24R 35R",
                                     "2B 7B 13B 18B 22B 27B 33B 38B",
                                     "1R 3R 6R 8R 11R 14R 16R 19R 21R 23R 26R 28R 31R 34R 36R 39R"));

            Assert.AreEqual("1 2 3 4 6 7 8 10 11 13 14 15 16 18 19 20 21 22 23 24 26 27 28 30 31 33 34 35 36 38 39", string.Join(" ", tree.Items));

            return(tree);
        }
示例#4
0
        public void ResolveDoubleBlackUpToRoot()
        {
            var tree = new RedBlackTree <int>();

            tree.AddNode(20);
            tree.AddNode(10);
            tree.AddNode(30);
            tree.MarkBlack(0, 1);

            Assert.IsTrue(VerifyTree(tree,
                                     "         20B",
                                     "   10B         30B"));

            tree.Remove(30);
            Assert.IsTrue(VerifyTree(tree,
                                     "         20B",
                                     "   10R          - "));
        }
示例#5
0
        public void MultiInsertOnRight()
        {
            var tree  = new RedBlackTree <int>();
            var items = new[] { 18, 7, 15, 16, 30, 25, 40, 60, 2, 1, 70 };

            foreach (var item in items)
            {
                tree.AddNode(item);
            }

            Assert.AreEqual("1 2 7 15 16 18 25 30 40 60 70", string.Join(" ", tree.Items));
        }