当前位置: 首页>>代码示例>>C#>>正文


C# RedBlackTree.check方法代码示例

本文整理汇总了C#中RedBlackTree.check方法的典型用法代码示例。如果您正苦于以下问题:C# RedBlackTree.check方法的具体用法?C# RedBlackTree.check怎么用?C# RedBlackTree.check使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RedBlackTree的用法示例。


在下文中一共展示了RedBlackTree.check方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: duplicates

        private static void duplicates(int n)
        {
            var rand = Accord.Math.Tools.Random;

            RedBlackTree<int> t = new RedBlackTree<int>();

            // Create a vector of random numbers with duplicates
            int[] k = new int[n];
            for (int i = 0; i < k.Length; i++) 
                k[i] = i;

            Vector.Shuffle(k);

            int[] sorted = (int[])k.Clone();
            Array.Sort(sorted);

            // Populate the tree with numbers
            for (int i = 0; i < k.Length; i++)
            {
                var node = t.Add(k[i]);

                Assert.IsNotNull(node);
                Assert.AreEqual(k[i], node.Value);

                Assert.IsTrue(t.check());
            }

            Assert.AreEqual(k.Length, t.Count);


            // Check that all elements are in the tree
            for (int i = 0; i < k.Length; i++)
            {
                var node = t.Find(k[i]);

                Assert.IsNotNull(node);
                Assert.AreEqual(k[i], node.Value);

                Assert.IsTrue(t.Contains(k[i]));
                Assert.IsTrue(t.Contains(node));
            }

            // Enumerate the values (must be in order)
            int arrayIndex = 0;
            foreach (var node in t)
                Assert.AreEqual(sorted[arrayIndex++], node.Value);



            // Populate the tree with the same numbers
            for (int i = 0; i < k.Length; i++)
            {
                var node = t.Add(k[i]);

                Assert.IsNotNull(node);
                Assert.AreEqual(k[i], node.Value);

                Assert.IsTrue(t.check());
            }

            Assert.IsTrue(t.check());

            // Enumerate the values (must be in order)
            arrayIndex = 0;
            foreach (var node in t)
                Assert.AreEqual(sorted[arrayIndex++], node.Value);
        }
开发者ID:CanerPatir,项目名称:framework,代码行数:67,代码来源:RedBlackTreeTest.cs

示例2: run

        private static void run(int n)
        {
            var rand = Accord.Math.Tools.Random;

            RedBlackTree<int> t = new RedBlackTree<int>(allowDuplicates: true);

            // Create a vector of random numbers
            int[] k = new int[n];
            for (int i = 0; i < k.Length; i++)
                k[i] = rand.Next(k.Length);

            int[] sorted = (int[])k.Clone();
            Array.Sort(sorted);

            // Populate the tree with numbers
            for (int i = 0; i < k.Length; i++)
            {
                var node = t.Add(k[i]);

                Assert.IsNotNull(node);
                Assert.AreEqual(k[i], node.Value);

                Assert.IsTrue(t.check());
            }

            Assert.AreEqual(k.Length, t.Count);


            // Check that all elements are in the tree
            for (int i = 0; i < k.Length; ++i)
            {
                var node = t.Find(k[i]);

                Assert.IsNotNull(node);
                Assert.AreEqual(k[i], node.Value);

                Assert.IsTrue(t.Contains(k[i]));
                Assert.IsTrue(t.Contains(node));
            }

            // Enumerate the values (must be in order)
            int arrayIndex = 0;
            foreach (var node in t)
                Assert.AreEqual(sorted[arrayIndex++], node.Value);



            // Start from min and go navigating up to max
            var min = t.Min();
            Assert.IsNotNull(min);
            Assert.AreEqual(k.Min(), min.Value);

            for (int i = 0; i < k.Length; i++)
            {
                Assert.IsNotNull(min);
                min = t.GetNextNode(min);
            }
            Assert.IsNull(min); // the last should be null.

            // Start from max and go navigating down to min
            var max = t.Max();
            Assert.AreEqual(k.Max(), max.Value);
            for (int i = 0; i < k.Length; i++)
            {
                Assert.IsNotNull(max);
                max = t.GetPreviousNode(max);
            }
            Assert.IsNull(max); // the last should be null.


            // Exercise the tree
            for (int M = k.Length; M > 0; M--)
            {
                int knew = rand.Next(k.Length); // random new key 
                int j = rand.Next(M); // random original key to replace 
                int i;

                for (i = 0; i < k.Length; i++)
                    if (k[i] >= 0)
                        if (j-- == 0)
                            break;


                if (i >= k.Length)
                    Assert.Fail();

                int kd = k[i];

                var node = t.Find(kd);
                Assert.IsNotNull(node);

                node.Value = knew;

                Assert.IsNotNull(t.Resort(node));
                Assert.IsTrue(t.check());

                k[i] = -1 - knew;

                Assert.AreEqual(k.Length, t.Count);
            }
//.........这里部分代码省略.........
开发者ID:CanerPatir,项目名称:framework,代码行数:101,代码来源:RedBlackTreeTest.cs


注:本文中的RedBlackTree.check方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。