當前位置: 首頁>>代碼示例>>C#>>正文


C# BinarySearchTree.ContainsKey方法代碼示例

本文整理匯總了C#中BinarySearchTree.ContainsKey方法的典型用法代碼示例。如果您正苦於以下問題:C# BinarySearchTree.ContainsKey方法的具體用法?C# BinarySearchTree.ContainsKey怎麽用?C# BinarySearchTree.ContainsKey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在BinarySearchTree的用法示例。


在下文中一共展示了BinarySearchTree.ContainsKey方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Simple

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

            Assert.AreEqual(tree.Count, 0);

            tree = GetTestTree();
            Assert.IsTrue(tree.ContainsKey(19));

            tree.Clear();
            Assert.AreEqual(tree.Count, 0);
            Assert.IsFalse(tree.ContainsKey(19));
        }
開發者ID:GTuritto,項目名稱:ngenerics,代碼行數:14,代碼來源:Clear.cs

示例2: Simple

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

            var dictionary = new Dictionary<int, string>();

            var rand = new Random(Convert.ToInt32(DateTime.Now.Ticks % Int32.MaxValue));

            for (var i = 0; i < 50; i++)
            {
                var gen = rand.Next(2000);

                while (dictionary.ContainsKey(gen))
                {
                    gen = rand.Next(2000);
                }

                dictionary.Add(gen, null);

                tree.Add(gen, gen.ToString());

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

            using (var enumerator = dictionary.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    Assert.IsTrue(tree.Remove(enumerator.Current));
                }
            }
        }
開發者ID:havok,項目名稱:ngenerics,代碼行數:33,代碼來源:Remove.cs

示例3: KeyValuePair

        public void KeyValuePair()
        {
            var tree = new BinarySearchTree<int, string>();

            var dictionary = new Dictionary<int, string>();

            var rand = new Random(Convert.ToInt32(DateTime.Now.Ticks % Int32.MaxValue));

            for (var i = 0; i < 50; i++)
            {
                var gen = rand.Next(2000);

                while (dictionary.ContainsKey(gen))
                {
                    gen = rand.Next(2000);
                }

                dictionary.Add(gen, null);

                tree.Add(new KeyValuePair<int, string>(gen, gen.ToString()));

                Assert.AreEqual(tree.Count, i + 1);
                Assert.IsTrue(tree.ContainsKey(gen));
            }
        }
開發者ID:GTuritto,項目名稱:ngenerics,代碼行數:25,代碼來源:Add.cs

示例4: AddKeyValuePairExample

        public void AddKeyValuePairExample()
        {
            // Create a simple tree
            BinarySearchTreeBase<string, int> tree = new BinarySearchTree<string, int>
                                                         {
                                                             new KeyValuePair<string, int>("cat", 1),
                                                             new KeyValuePair<string, int>("dog", 2),
                                                             new KeyValuePair<string, int>("canary", 3)
                                                         };

            // There should be 3 items in the tree.
            Assert.AreEqual(3, tree.Count);

            // The tree should contain all three keys
            Assert.IsTrue(tree.ContainsKey("cat"));
            Assert.IsTrue(tree.ContainsKey("dog"));
            Assert.IsTrue(tree.ContainsKey("canary"));

            // The value of the item with key "dog" must be 2.
            Assert.AreEqual(2, tree["dog"]);
        }
開發者ID:havok,項目名稱:ngenerics,代碼行數:21,代碼來源:BinarySearchTreeBaseExamples.cs

示例5: Simple2

        public void Simple2()
        {
            var tree = new BinarySearchTree<int, string>
                           {
                               {4, "4"},
                               {6, "6"},
                               {2, "2"},
                               {5, "5"},
                               {19, "19"},
                               {1, "1"}
                           };

            Assert.AreEqual(tree.Count, 6);

            Assert.IsTrue(tree.ContainsKey(4));
            Assert.IsTrue(tree.ContainsKey(6));
            Assert.IsTrue(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(19));
            Assert.IsTrue(tree.ContainsKey(1));

            Assert.IsFalse(tree.Remove(20));

            Assert.IsTrue(tree.Remove(4));
            Assert.AreEqual(tree.Count, 5);

            Assert.IsFalse(tree.ContainsKey(4));
            Assert.IsTrue(tree.ContainsKey(6));
            Assert.IsTrue(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(19));
            Assert.IsTrue(tree.ContainsKey(1));

            Assert.IsTrue(tree.Remove(2));
            Assert.AreEqual(tree.Count, 4);

            Assert.IsFalse(tree.ContainsKey(4));
            Assert.IsTrue(tree.ContainsKey(6));
            Assert.IsFalse(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(19));
            Assert.IsTrue(tree.ContainsKey(1));

            Assert.IsTrue(tree.Remove(19));
            Assert.AreEqual(tree.Count, 3);

            Assert.IsFalse(tree.ContainsKey(4));
            Assert.IsTrue(tree.ContainsKey(6));
            Assert.IsFalse(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(5));
            Assert.IsFalse(tree.ContainsKey(19));
            Assert.IsTrue(tree.ContainsKey(1));

            Assert.IsFalse(tree.Remove(20));

            Assert.IsTrue(tree.Remove(1));
            Assert.AreEqual(tree.Count, 2);

            Assert.IsFalse(tree.ContainsKey(4));
            Assert.IsTrue(tree.ContainsKey(6));
            Assert.IsFalse(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(5));
            Assert.IsFalse(tree.ContainsKey(19));
            Assert.IsFalse(tree.ContainsKey(1));

            Assert.IsTrue(tree.Remove(6));
            Assert.AreEqual(tree.Count, 1);

            Assert.IsFalse(tree.ContainsKey(4));
            Assert.IsFalse(tree.ContainsKey(6));
            Assert.IsFalse(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(5));
            Assert.IsFalse(tree.ContainsKey(19));
            Assert.IsFalse(tree.ContainsKey(1));

            Assert.IsTrue(tree.Remove(5));
            Assert.AreEqual(tree.Count, 0);

            Assert.IsFalse(tree.ContainsKey(4));
            Assert.IsFalse(tree.ContainsKey(6));
            Assert.IsFalse(tree.ContainsKey(2));
            Assert.IsFalse(tree.ContainsKey(5));
            Assert.IsFalse(tree.ContainsKey(19));
            Assert.IsFalse(tree.ContainsKey(1));

            Assert.IsFalse(tree.Remove(1));
        }
開發者ID:havok,項目名稱:ngenerics,代碼行數:87,代碼來源:Remove.cs

示例6: KeyValuePair

        public void KeyValuePair()
        {
            var tree = new BinarySearchTree<int, string>
                           {
                               {4, "4"},
                               {6, "6"},
                               {2, "2"},
                               {5, "5"},
                               {19, "19"},
                               {1, "1"}
                           };

            Assert.AreEqual(tree.Count, 6);

            Assert.IsTrue(tree.ContainsKey(4));
            Assert.IsTrue(tree.ContainsKey(6));
            Assert.IsTrue(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(19));
            Assert.IsTrue(tree.ContainsKey(1));

            Assert.IsFalse(tree.Remove(new KeyValuePair<int, string>(20, "20")));

            Assert.IsTrue(tree.Remove(new KeyValuePair<int, string>(4, "4")));
            Assert.AreEqual(tree.Count, 5);

            Assert.IsFalse(tree.ContainsKey(4));
            Assert.IsTrue(tree.ContainsKey(6));
            Assert.IsTrue(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(19));
            Assert.IsTrue(tree.ContainsKey(1));
        }
開發者ID:havok,項目名稱:ngenerics,代碼行數:33,代碼來源:Remove.cs

示例7: ClearExample

        public void ClearExample()
        {
            // Create a simple tree
            BinarySearchTreeBase<string, int> tree = new BinarySearchTree<string, int>
                                                         {
                                                             new KeyValuePair<string, int>("cat", 1),
                                                             new KeyValuePair<string, int>("dog", 2),
                                                             new KeyValuePair<string, int>("canary", 3)
                                                         };

            // There should be 3 items in the tree.
            Assert.AreEqual(3, tree.Count);

            // Clear the tree
            tree.Clear();

            // The tree should be empty.
            Assert.AreEqual(0, tree.Count);

            // No cat here..
            Assert.IsFalse(tree.ContainsKey("cat"));
        }
開發者ID:havok,項目名稱:ngenerics,代碼行數:22,代碼來源:BinarySearchTreeBaseExamples.cs

示例8: RemoveExample

        public void RemoveExample()
        {
            // Build a simple tree.
            BinarySearchTreeBase<string, int> tree = new BinarySearchTree<string, int>
                                                         {
                                                             new KeyValuePair<string, int>("cat", 1),
                                                             new KeyValuePair<string, int>("dog", 2),
                                                             new KeyValuePair<string, int>("canary", 3)
                                                         };

            // There are three items in the tree
            Assert.AreEqual(3, tree.Count);

            // Let's remove the dog
            tree.Remove("dog");

            // Now the tree contains only two items, and dog isn't
            // in the tree.
            Assert.AreEqual(2, tree.Count);
            Assert.IsFalse(tree.ContainsKey("dog"));
        }
開發者ID:havok,項目名稱:ngenerics,代碼行數:21,代碼來源:BinarySearchTreeBaseExamples.cs

示例9: ContainsKeyExample

        public void ContainsKeyExample()
        {
            // Create a simple tree
            BinarySearchTreeBase<string, int> tree = new BinarySearchTree<string, int>
                                                         {
                                                             new KeyValuePair<string, int>("cat", 1),
                                                             new KeyValuePair<string, int>("dog", 2),
                                                             new KeyValuePair<string, int>("canary", 3)
                                                         };

            // The tree should contain a cat and a dog...
            Assert.IsTrue(tree.ContainsKey("cat"));
            Assert.IsTrue(tree.ContainsKey("dog"));

            // But definitely not an ostrich.
            Assert.IsFalse(tree.ContainsKey("ostrich"));
        }
開發者ID:havok,項目名稱:ngenerics,代碼行數:17,代碼來源:BinarySearchTreeBaseExamples.cs

示例10: Simple

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

            Assert.IsFalse(tree.ContainsKey(5));

            tree.Add(4, "4");
            Assert.AreEqual(tree[4], "4");
            Assert.IsTrue(tree.ContainsKey(4));
            Assert.IsFalse(tree.ContainsKey(5));

            tree.Add(6, "6");
            Assert.AreEqual(tree[6], "6");
            Assert.IsTrue(tree.ContainsKey(4));
            Assert.IsFalse(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(6));

            tree.Add(2, "2");
            Assert.AreEqual(tree[2], "2");
            Assert.IsTrue(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(4));
            Assert.IsFalse(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(6));

            tree.Add(5, "5");
            Assert.AreEqual(tree[5], "5");
            Assert.IsTrue(tree.ContainsKey(2));
            Assert.IsTrue(tree.ContainsKey(4));
            Assert.IsTrue(tree.ContainsKey(5));
            Assert.IsTrue(tree.ContainsKey(6));

            var rand = new Random();

            tree = new BinarySearchTree<int, string>();

            var list = new List<int>();

            for (var i = 0; i < 100; i++)
            {
                int r;

                do
                {
                    r = rand.Next(5000);
                }
                while (list.Contains(r));

                list.Add(r);

                tree.Add(r, null);

                Assert.IsTrue(tree.ContainsKey(r));
            }
        }
開發者ID:GTuritto,項目名稱:ngenerics,代碼行數:54,代碼來源:Contains.cs


注:本文中的BinarySearchTree.ContainsKey方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。