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


C# RedBlackTree.Insert方法代码示例

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


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

示例1: RotationTests

        public void RotationTests()
        {
            var tree = new RedBlackTree<Int32>();

            tree.Insert(35);
            tree.Insert(25);
            tree.Insert(70);
            tree.Insert(10);
            tree.Insert(100);
            tree.Insert(20);
            tree.Insert(5);
            tree.Insert(85);
            tree.Insert(55);
            tree.Insert(40);

            Assert.AreEqual(35, tree.RootNode.NodeValue);
            Assert.AreEqual(20, tree.RootNode.LeftNode.NodeValue);
            Assert.AreEqual(25, tree.RootNode.LeftNode.RightNode.NodeValue);
            Assert.AreEqual(10, tree.RootNode.LeftNode.LeftNode.NodeValue);
            Assert.AreEqual(5, tree.RootNode.LeftNode.LeftNode.LeftNode.NodeValue);

            Assert.AreEqual(85, tree.RootNode.RightNode.NodeValue);
            Assert.AreEqual(100, tree.RootNode.RightNode.RightNode.NodeValue);
            Assert.AreEqual(55, tree.RootNode.RightNode.LeftNode.NodeValue);
            Assert.AreEqual(40, tree.RootNode.RightNode.LeftNode.LeftNode.NodeValue);
            Assert.AreEqual(70, tree.RootNode.RightNode.LeftNode.RightNode.NodeValue);
        }
开发者ID:jramey,项目名称:BinaryTree,代码行数:27,代码来源:RedBlackTreeTests.cs

示例2: ContainsFailureTest

        public void ContainsFailureTest()
        {
            var tree = new RedBlackTree<Int32>();

            tree.Insert(10);
            tree.Insert(25);
            tree.Insert(7);

            Assert.IsFalse(tree.BredthContains(2));
        }
开发者ID:jramey,项目名称:BinaryTree,代码行数:10,代码来源:RedBlackTreeTests.cs

示例3: ContainsSuccessTest

        public void ContainsSuccessTest()
        {
            var tree = new RedBlackTree<Int32>();

            tree.Insert(10);
            tree.Insert(25);
            tree.Insert(7);

            Assert.IsTrue(tree.BredthContains(25));
        }
开发者ID:jramey,项目名称:BinaryTree,代码行数:10,代码来源:RedBlackTreeTests.cs

示例4: RootNodeTest

        public void RootNodeTest()
        {
            var tree = new RedBlackTree<Int32>();
            tree.Insert(10);

            Assert.IsFalse(tree.IsEmpty);
            Assert.IsTrue(tree.BredthContains(10));
            Assert.AreEqual(10, tree.RootNode.NodeValue);
        }
开发者ID:jramey,项目名称:BinaryTree,代码行数:9,代码来源:RedBlackTreeTests.cs

示例5: InsertMultipleNodes

 private void InsertMultipleNodes(RedBlackTree<Int32> tree, Int32 numberOfNodes)
 {
     for (var i = 0; i < numberOfNodes; i++)
     {
         tree.Insert(i);
     }
 }
开发者ID:jramey,项目名称:BinaryTree,代码行数:7,代码来源:RedBlackTreeTests.cs

示例6: button4_Click

        private void button4_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            Stopwatch sw = new Stopwatch();
            TimeSpan slTime = TimeSpan.MinValue;
            TimeSpan treeTime = TimeSpan.MaxValue;
            Random rng = new Random();

            SortedDictionary<int, int> sl = new SortedDictionary<int, int>();
            RedBlackTree<int, int> tree = new RedBlackTree<int, int>();

            sb.Append("1 tick = ");
            sb.Append((int)(1000000000d / ((double)Stopwatch.Frequency)));
            sb.AppendLine(" nano seconds");

            sb.AppendLine(" Entries | SL in ms | RBT adj || SL ticks | RBT adj | % Time Spent");

            const long MaxRotations = 1000;
            const long MinInsert = 99999;
            const long MaxInsert = 100000;
            long[,] slData = new long[MaxInsert - MinInsert + 1, MaxRotations];
            long[,] treeData = new long[MaxInsert - MinInsert + 1, MaxRotations];
            double[] slData2 = new double[MaxInsert - MinInsert + 1];
            double[] treeData2 = new double[MaxInsert - MinInsert + 1];

            for (long j = MinInsert; j <= MaxInsert; j++)
                for (long k = 0L; k < MaxRotations; k++)
                {
                    sl.Clear();
                    tree.Clear();
                    int rngValue = rng.Next();

                    sw.Start();
                    for (int i = 0; i < j; i++)
                        sl[rngValue] = rngValue;
                    sw.Stop();
                    slTime = sw.Elapsed;
                    sw.Reset();
                    sw.Start();
                    for (int i = 0; i < j; i++)
                        tree.Insert(rngValue, rngValue, true);
                    sw.Stop();
                    treeTime = sw.Elapsed;
                    sw.Reset();

                    slData[j - MinInsert, k] = slTime.Ticks;
                    treeData[j - MinInsert, k] = treeTime.Ticks;
                }

            for (long j = MinInsert; j <= MaxInsert; j++)
            {
                long slSum = 0, treeSum = 0;
                for (int k = 0; k < MaxRotations; k++)
                {
                    slSum += slData[j - MinInsert, k];
                    treeSum += treeData[j - MinInsert, k];
                }
                slData2[j - MinInsert] = (double)slSum / (double)MaxRotations;
                treeData2[j - MinInsert] = (double)treeSum / (double)MaxRotations;
            }

            for (long j = MinInsert; j <= MaxInsert; j++)
            {

                sb.Append(j.ToString().PadLeft(8));
                sb.Append(" |");
                sb.Append(string.Empty.PadLeft(9));
                sb.Append(" |");
                sb.Append(string.Empty.PadLeft(8));
                sb.Append(" ||");
                sb.Append(slData2[j - MinInsert].ToString("F2").PadLeft(9));
                sb.Append(" |");
                sb.Append((treeData2[j - MinInsert] - slData2[j - MinInsert]).ToString("F2").PadLeft(8));
                sb.Append(" |");
                sb.Append((treeData2[j - MinInsert] / slData2[j - MinInsert] * 100d).ToString("F2").PadLeft(13));
                sb.AppendLine();
            }

            tbOut.Text = sb.ToString();
        }
开发者ID:Finibhire,项目名称:PersonalProjects,代码行数:80,代码来源:Form1.cs

示例7: button3_Click

        private void button3_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            Stopwatch sw = new Stopwatch();
            TimeSpan slTime = TimeSpan.MinValue;
            TimeSpan treeTime = TimeSpan.MaxValue;
            //int[] testValue = new int[500000];

            Random rng = new Random();
            //for (int i = 0; i < testValue.Length; i++)
            //{
            //    testValue[i] = rng.Next();
            //}

            SortedDictionary<int, int> sl = new SortedDictionary<int, int>();
            RedBlackTree<int, int> tree = new RedBlackTree<int, int>();

            sb.Append("1 tick = ");
            sb.Append((int)(1000000000d / ((double)Stopwatch.Frequency)));
            sb.AppendLine(" nano seconds");

            sb.AppendLine(" Entries | SL in ms | RBT adj || SL ticks | RBT adj | % Time Spent");

            const int MaxInsert = 100001;
            for (int k = 1; k < MaxInsert; k *= 10)
                for (int j = k; j < k * 10 && j < MaxInsert; j += k)
                {
                    sl.Clear();
                    tree.Clear();
                    int rngValue = rng.Next();

                    sw.Start();
                    for (int i = 0; i < j; i++)
                        sl[rngValue] = rngValue;
                    sw.Stop();
                    slTime = sw.Elapsed;
                    sw.Reset();
                    sw.Start();
                    for (int i = 0; i < j; i++)
                        tree.Insert(rngValue, rngValue, true);
                    sw.Stop();
                    treeTime = sw.Elapsed;
                    sw.Reset();

                    sb.Append(j.ToString().PadLeft(8));
                    sb.Append(" |");
                    sb.Append(slTime.Milliseconds.ToString().PadLeft(9));
                    sb.Append(" |");
                    sb.Append((treeTime.Milliseconds - slTime.Milliseconds).ToString().PadLeft(8));
                    sb.Append(" ||");
                    sb.Append(slTime.Ticks.ToString().PadLeft(9));
                    sb.Append(" |");
                    sb.Append((treeTime.Ticks - slTime.Ticks).ToString().PadLeft(8));
                    sb.Append(" |");
                    sb.Append(((double)treeTime.Ticks / (double)slTime.Ticks * 100d).ToString("F2").PadLeft(13));
                    sb.AppendLine();
                }
            tbOut.Text = sb.ToString();
        }
开发者ID:Finibhire,项目名称:PersonalProjects,代码行数:59,代码来源:Form1.cs

示例8: button2_Click

        private void button2_Click(object sender, EventArgs e)
        {
            //int[] testValue = new int[] { 50, 20, 50 };
            int[] testValue = new int[500];

            Random rng = new Random();
            for (int i = 0; i < testValue.Length; i++)
            {
                testValue[i] = rng.Next(10, 100);
            }

            RedBlackTree<int, int> tree = new RedBlackTree<int, int>();
            for (int i = 0; i < testValue.Length; i++)
            {
                tree.Insert(testValue[i], testValue[i], true);
            }

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < testValue.Length; i++)
            {
                sb.Append(testValue[i]);
                sb.Append(' ');
            }
            sb.Length = sb.Length - 1;
            sb.AppendLine();
            sb.AppendLine();
            foreach (int i in tree.Keys)
            {
                sb.Append(i);
                sb.Append(' ');
            }

            tbOut.Text = sb.ToString();
        }
开发者ID:Finibhire,项目名称:PersonalProjects,代码行数:34,代码来源:Form1.cs

示例9: DoTest

        public static void DoTest()
        {
            // Test against the worst case of insertion
            // Case: insert increasing numbers and check if it leads to a linked list
            var redBlackTree = new RedBlackTree<int>(allowDuplicates: false);

            redBlackTree.Insert(1);
            redBlackTree.Insert(2);
            redBlackTree.Insert(3);
            redBlackTree.Insert(4);
            redBlackTree.Insert(5);
            redBlackTree.Insert(6);
            redBlackTree.Insert(7);
            redBlackTree.Insert(8);
            redBlackTree.Insert(9);
            redBlackTree.Insert(10);

            Debug.Assert(redBlackTree.Height < redBlackTree.Count, "Fail! Tree doesn't rebalance against sorted elements!");

            Console.WriteLine("********************");
            Console.WriteLine(" [*] RED-BLACK TREE:\r\n");
            Console.WriteLine("********************");
            Console.WriteLine(redBlackTree.DrawTree());
            Console.WriteLine("\r\n");


            //
            // Test against re-shuffled insertions (not like above order)
            redBlackTree = new RedBlackTree<int>(allowDuplicates: false);

            redBlackTree.Insert(4);
            redBlackTree.Insert(5);
            redBlackTree.Insert(7);
            redBlackTree.Insert(2);
            redBlackTree.Insert(1);
            redBlackTree.Insert(3);
            redBlackTree.Insert(6);
            redBlackTree.Insert(0);
            redBlackTree.Insert(8);
            redBlackTree.Insert(10);
            redBlackTree.Insert(9);

            //
            // ASSERT INSERTING DUPLICATES WOULD BREAK
            var insert_duplicate_passed = true;
            try
            {
                // 2 already exists in tree
                redBlackTree.Insert(2);
                insert_duplicate_passed = true;
            }
            catch
            {
                insert_duplicate_passed = false;
            }

            Debug.Assert(insert_duplicate_passed == false, "Fail! The tree doesn't allow duplicates");


            //
            // PRINT TREE
            Console.WriteLine("********************");
            Console.WriteLine(" [*] RED-BLACK TREE:\r\n");
            Console.WriteLine("********************");
            Console.WriteLine(redBlackTree.DrawTree());
            Console.WriteLine("\r\n");


            // Assert count
            Debug.Assert(redBlackTree.Count == 11);

            // Assert existence and nonexistence of some items
            Debug.Assert(redBlackTree.Contains(1) == true);
            Debug.Assert(redBlackTree.Contains(3) == true);
            Debug.Assert(redBlackTree.Contains(999) == false);

            // ASSERT THAT EACH LEVEL HAS A DIFFERENT COLOR
            Asset_Levels_Different_Colors(redBlackTree);

            // Do some deletions
            redBlackTree.Remove(7);
            redBlackTree.Remove(1);
            redBlackTree.Remove(3);

            // Assert count
            Debug.Assert(redBlackTree.Count == 8);

            // Assert nonexistence of previously existing items
            Debug.Assert(redBlackTree.Contains(1) == false);
            Debug.Assert(redBlackTree.Contains(3) == false);

            // Remove root value
            var oldRootVal = redBlackTree.Root.Value;
            redBlackTree.Remove(redBlackTree.Root.Value);

            // Assert count
            Debug.Assert(redBlackTree.Count == 7);

            // Assert nonexistence of old root's value
            Debug.Assert(redBlackTree.Contains(oldRootVal) == false);
//.........这里部分代码省略.........
开发者ID:ChijunShen,项目名称:C-Sharp-Algorithms,代码行数:101,代码来源:RedBlackTreeTest.cs


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