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


C# RedBlackTree.Add方法代码示例

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


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

示例1: AddTest

        public void AddTest()
        {
            //Arrange
            var bst = new RedBlackTree<int, string>();

            // 33 28 63 20 31 39 79 10 35 60          ( red links = 10 39 )

            var testData = new[]
            {
                new KeyValuePair<int, string>(33, "33"),
                new KeyValuePair<int, string>(28, "28"),
                new KeyValuePair<int, string>(63, "63"),
                new KeyValuePair<int, string>(20, "20"),
                new KeyValuePair<int, string>(31, "31"),
                new KeyValuePair<int, string>(39, "39"),
                new KeyValuePair<int, string>(79, "79"),
                new KeyValuePair<int, string>(10, "10"),
                new KeyValuePair<int, string>(35, "35"),
                new KeyValuePair<int, string>(60, "60"),
            };

            //Act
            Array.ForEach(testData, t =>
            {
                bst.Add(t.Key, t.Value);
            });

            bst.Add(75, "75");
            bst.Add(38, "38");
            bst.Add(12, "12");

            var result = bst.LevelOrderTraversal();

            //Assert
            Assert.AreEqual(result.ElementAt(0).Key, 33);
            Assert.AreEqual(result.ElementAt(1).Key, 28);
            Assert.AreEqual(result.ElementAt(2).Key, 63);
            Assert.AreEqual(result.ElementAt(3).Key, 12);
            Assert.AreEqual(result.ElementAt(4).Key, 31);
            Assert.AreEqual(result.ElementAt(5).Key, 39);
            Assert.AreEqual(result.ElementAt(6).Key, 79);
            Assert.AreEqual(result.ElementAt(7).Key, 10);
            Assert.AreEqual(result.ElementAt(8).Key, 20);
            Assert.AreEqual(result.ElementAt(9).Key, 38);
            Assert.AreEqual(result.ElementAt(10).Key, 60);
            Assert.AreEqual(result.ElementAt(11).Key, 75);
            Assert.AreEqual(result.ElementAt(12).Key, 35);

            //Assert are red nodes
            Assert.IsTrue(result.ElementAt(3).IsRed);
            Assert.IsTrue(result.ElementAt(5).IsRed);
            Assert.IsTrue(result.ElementAt(11).IsRed);
        }
开发者ID:GrigoryBushuev,项目名称:Algo,代码行数:53,代码来源:LLRBTreeTest.cs

示例2: BuildStorageEntry

        private static int BuildStorageEntry(DirectoryEntry storageEntry)
        {
            // direct members of each storage are organised in a separate red-black tree
            RedBlackTree<DirectoryEntry> rbTree = new RedBlackTree<DirectoryEntry>();
            foreach (DirectoryEntry entry in storageEntry.Members.Values)
            {
                rbTree.Add(entry);
            }

            foreach (RedBlackTreeNode<DirectoryEntry> node in rbTree.InorderTreeWalk(rbTree.Root))
            {
                DirectoryEntry entry = node.Data;
                entry.NodeColor = GetNodeColor(node.Color);
                entry.LeftChildDID = GetNodeID(node.Left);
                entry.RightChildDID = GetNodeID(node.Right);

                if (entry.Members.Count > 0)
                {
                    entry.EntryType = EntryType.Storage;
                    entry.MembersTreeNodeDID = BuildStorageEntry(entry);
                }
                else
                {
                    entry.EntryType = EntryType.Stream;
                    entry.MembersTreeNodeDID = -1;
                }
            }

            return rbTree.Root.Data.ID;
        }
开发者ID:Johnnyfly,项目名称:source20131023,代码行数:30,代码来源:DirectoryTree.cs

示例3: NonIComparable

        public void NonIComparable()
        {
            var redBlackTree = new RedBlackTree<NonComparableTClass, string>();

            for (var i = 0; i < 100; i++)
            {
                redBlackTree.Add(new NonComparableTClass(i), i.ToString());
            }

            var newTree = SerializeUtil.BinarySerializeDeserialize(redBlackTree);

            Assert.AreNotSame(redBlackTree, newTree);
            Assert.AreEqual(redBlackTree.Count, newTree.Count);

            var redBlackTreeEnumerator = redBlackTree.GetEnumerator();
            var newTreeEnumerator = newTree.GetEnumerator();

            while (redBlackTreeEnumerator.MoveNext())
            {
                Assert.IsTrue(newTreeEnumerator.MoveNext());
                Assert.AreEqual(redBlackTreeEnumerator.Current.Key.Number, newTreeEnumerator.Current.Key.Number);
                Assert.AreEqual(redBlackTreeEnumerator.Current.Value, newTreeEnumerator.Current.Value);

                Assert.IsTrue(newTree.ContainsKey(redBlackTreeEnumerator.Current.Key));
                Assert.AreEqual(newTree[redBlackTreeEnumerator.Current.Key], redBlackTreeEnumerator.Current.Value);
            }

            Assert.IsFalse(newTreeEnumerator.MoveNext());
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:29,代码来源:Serialization.cs

示例4: GetTestTree

        internal static RedBlackTree<int, string> GetTestTree(int noOfItems)
        {
            var redBlackTree = new RedBlackTree<int, string>();

            for (var i = 0; i < noOfItems; i++)
            {
                redBlackTree.Add(i, i.ToString());
            }

            return redBlackTree;
        }
开发者ID:havok,项目名称:ngenerics,代码行数:11,代码来源:RedBlackTreeTest.cs

示例5: AddItem

        public void AddItem()
        {
            RedBlackTree<int, int> tree = new RedBlackTree<int, int>();
            int temp;

            tree.Add(1, 1);

            Assert.AreEqual(tree.Count, 1);
            Assert.IsTrue(tree.TryGetValue(1, out temp));
            Assert.IsFalse(tree.TryGetValue(2, out temp));
        }
开发者ID:CedricDumont,项目名称:NMemory.Next,代码行数:11,代码来源:RedBlackTreeFixture.cs

示例6: DuplicateTest

        public void DuplicateTest()
        {
            var t = new RedBlackTree<int, string>();
            var collection = t as ICollection<KeyValuePair<int, string>>;

            var values = collection.ToArray();
            Assert.AreEqual(0, values.Length);

            t.Add(new KeyValuePair<int, string>(1, "1"));
            values = collection.ToArray();
            Assert.AreEqual(1, values.Length);

            t.Add(new KeyValuePair<int, string>(2, "2"));
            values = collection.ToArray();
            Assert.AreEqual(2, values.Length);
            Assert.AreEqual("1", values[0].Value);
            Assert.AreEqual("2", values[1].Value);


            t.Add(new KeyValuePair<int, string>(1, "bla"));
            values = collection.ToArray();
            Assert.AreEqual(2, values.Length);
            Assert.AreEqual("bla", values[0].Value);
            Assert.AreEqual("2", values[1].Value);

            var node1 = t.Remove(new KeyValuePair<int, string>(1, "-"));
            Assert.IsNotNull(node1);
            values = collection.ToArray();
            Assert.AreEqual(1, values.Length);
            Assert.AreEqual("2", values[0].Value);

            var node2 = t.Remove(new KeyValuePair<int, string>(1, "-"));
            Assert.IsNull(node2);
            values = collection.ToArray();
            Assert.AreEqual(1, values.Length);
            Assert.AreEqual("2", values[0].Value);

            var node3 = t.Remove(new KeyValuePair<int, string>(2, "-"));
            values = collection.ToArray();
            Assert.AreEqual(0, values.Length);
        }
开发者ID:CanerPatir,项目名称:framework,代码行数:41,代码来源:RedBlackTreeTest.cs

示例7: Simple

        public void Simple()
        {
            var redBlackTree = new RedBlackTree<int, string>();

            for (var i = 0; i < 100; i++)
            {
                redBlackTree.Add(i, i.ToString());

                Assert.IsTrue(redBlackTree.ContainsKey(i));
                Assert.AreEqual(redBlackTree.Count, i + 1);
            }

            for (var i = 300; i > 200; i--)
            {
                redBlackTree.Add(i, i.ToString());

                Assert.IsTrue(redBlackTree.ContainsKey(i));
                Assert.AreEqual(redBlackTree.Count, 100 + (300 - i) + 1);
            }

            for (var i = 100; i < 200; i++)
            {
                redBlackTree.Add(i, i.ToString());

                Assert.IsTrue(redBlackTree.ContainsKey(i));
                Assert.AreEqual(redBlackTree.Count, 100 + i + 1);
            }

            for (var i = 301; i < 400; i++)
            {
                redBlackTree.Add(new KeyValuePair<int, string>(i, i.ToString()));

                Assert.IsTrue(redBlackTree.ContainsKey(i));
                Assert.AreEqual(redBlackTree[i], i.ToString());
                Assert.IsTrue(redBlackTree.Contains(new KeyValuePair<int, string>(i, i.ToString())));
            }

            Assert.IsFalse(redBlackTree.Contains(new KeyValuePair<int, string>(500, "500")));
            Assert.IsFalse(redBlackTree.Contains(new KeyValuePair<int, string>(300, "301")));
            Assert.IsTrue(redBlackTree.Contains(new KeyValuePair<int, string>(300, "300")));
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:41,代码来源:Add.cs

示例8: TestUsage1

        public void TestUsage1()
        {
            RedBlackTree<Guid, string> tree = new RedBlackTree<Guid, string>();
            Guid item1 = Guid.NewGuid();
            Guid item2 = Guid.NewGuid();
            for (int i = 0; i < 100; i++)
            {
                tree.Add(Guid.NewGuid(), DateTime.UtcNow.ToString(CultureInfo.InvariantCulture));
            }
            tree.Add(item1, DateTime.UtcNow.ToString(CultureInfo.InvariantCulture));
            tree.Add(item2, DateTime.UtcNow.ToString(CultureInfo.InvariantCulture));

            Assert.AreEqual(102, tree.Count, "Should be 102 items in the tree");
            Assert.IsTrue(tree.ContainsKey(item1), "Should contain item 1");
            Assert.IsTrue(tree.ContainsKey(item2), "Should contain item 2");
            tree.Remove(item1);
            Assert.AreEqual(101, tree.Count, "Should be 101 items in the tree");
            Assert.IsFalse(tree.ContainsKey(item1), "Should not contain item 1");
            tree.Remove(item2);
            Assert.AreEqual(100, tree.Count, "Should be 100 items in the tree");
            Assert.IsFalse(tree.ContainsKey(item2), "Should not contain item 2");
        }
开发者ID:stemarie,项目名称:redblacktree,代码行数:22,代码来源:UsageTests.cs

示例9: TestRemoveBug

		public void TestRemoveBug ()
		{
			var tree = new RedBlackTree<TestNode> ();
			TestNode t1 = new TestNode (1);
			TestNode t2 = new TestNode (2);
			TestNode t3 = new TestNode (3);
			
			tree.Add (t1);
			tree.InsertRight (t1, t2);
			tree.InsertLeft (t1, t3);
			tree.Remove (t1);
			Assert.AreEqual (2, tree.Count);
		} 
开发者ID:Kalnor,项目名称:monodevelop,代码行数:13,代码来源:RedBlackTreeTests.cs

示例10: RemoveNonExistingItem

        public void RemoveNonExistingItem()
        {
            RedBlackTree<int, int> tree = new RedBlackTree<int, int>();
            tree.Add(1, 1);
            int temp;

            
            bool removed = tree.Remove(2);

            Assert.IsFalse (removed);
            Assert.AreEqual(tree.Count, 1);
            Assert.IsTrue(tree.TryGetValue(1, out temp));
            Assert.IsFalse(tree.TryGetValue(2, out temp));
        }
开发者ID:CedricDumont,项目名称:NMemory.Next,代码行数:14,代码来源:RedBlackTreeFixture.cs

示例11: Main

        static void Main(string[] args)
        {
            var stack = new Stack<string>();
            var tree = new RedBlackTree<string, string>();
            var dict = new Dictionary<string, string>();

            for (int i = 0; i < 1000000; i++)
            {
                stack.Push(Guid.NewGuid().ToString());
            }

            var sw = new Stopwatch();
            sw.Start();
            foreach (string item in stack)
            {
                tree.Add(item, item);
            }
            sw.Stop();
            System.Console.WriteLine("{0}", sw.Elapsed);

            sw.Reset();
            sw.Start();
            foreach (string item in stack)
            {
                dict.Add(item, item);
            }
            sw.Stop();
            System.Console.WriteLine("{0}", sw.Elapsed);

            sw.Reset();
            sw.Start();
            foreach (string item in stack)
            {
                tree.ContainsKey(item);
            }
            sw.Stop();
            System.Console.WriteLine("{0}", sw.Elapsed);

            sw.Reset();
            sw.Start();
            foreach (string item in stack)
            {
                dict.ContainsKey(item);
            }
            sw.Stop();
            System.Console.WriteLine("{0}", sw.Elapsed);

            System.Console.ReadKey();
        }
开发者ID:stemarie,项目名称:performance,代码行数:49,代码来源:Program.cs

示例12: Main

        static void Main(string[] args)
        {
            var stack = new Stack<string>();
            var tree = new RedBlackTree<string, string>();

            for (int i = 0; i < 10000; i++)
            {
                stack.Push(Guid.NewGuid().ToString());
            }

            foreach (string item in stack)
            {
                tree.Add(item, item);
            }
        }
开发者ID:stemarie,项目名称:redblacktree,代码行数:15,代码来源:Program.cs

示例13: Simple

        public void Simple()
        {
            var redBlackTree = new RedBlackTree<int, string>();

            string value;

            for (var i = 0; i < 100; i++)
            {
                redBlackTree.Add(i, i.ToString());

                Assert.AreEqual(redBlackTree.Count, i + 1);
                Assert.IsTrue(redBlackTree.TryGetValue(i, out value));
                Assert.AreEqual(value, i.ToString());
            }

            Assert.IsFalse(redBlackTree.TryGetValue(101, out value));
            Assert.IsNull(value);

            Assert.IsFalse(redBlackTree.TryGetValue(102, out value));
            Assert.IsNull(value);
        }
开发者ID:havok,项目名称:ngenerics,代码行数:21,代码来源:TryGetValue.cs

示例14: Simple

        public void Simple()
        {
            var tree = new RedBlackTree<int, string>();

            for (var i = 0; i < 50; i++)
            {
                tree.Add(i, i.ToString());
            }

            var visitor = new KeyTrackingVisitor<int, string>();
            tree.AcceptVisitor(visitor);

            Assert.IsFalse(visitor.HasCompleted);

            Assert.AreEqual(visitor.TrackingList.Count, 50);

            var list = new List<int>(visitor.TrackingList);

            for (var i = 0; i < 50; i++)
            {
                Assert.IsTrue(list.Contains(i));
            }
        }
开发者ID:havok,项目名称:ngenerics,代码行数:23,代码来源:Visit.cs

示例15: TestAddBug

		public void TestAddBug ()
		{
			var tree = new RedBlackTree<TestNode> ();
			TestNode t1 = new TestNode (1);
			TestNode t2 = new TestNode (2);
			TestNode t3 = new TestNode (3);
			
			tree.Add (t1);
			tree.Add (t2);
			tree.Add (t3);
			Assert.AreEqual (3, tree.Count);
		} 
开发者ID:Kalnor,项目名称:monodevelop,代码行数:12,代码来源:RedBlackTreeTests.cs


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