本文整理汇总了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));
}
示例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));
}
}
}
示例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));
}
}
示例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"]);
}
示例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));
}
示例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));
}
示例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"));
}
示例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"));
}
示例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"));
}
示例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));
}
}