本文整理汇总了C#中RedBlackTree.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# RedBlackTree.Contains方法的具体用法?C# RedBlackTree.Contains怎么用?C# RedBlackTree.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RedBlackTree
的用法示例。
在下文中一共展示了RedBlackTree.Contains方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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")));
}
示例2: ContainsExample
public void ContainsExample()
{
// Create a simple tree
var tree = new RedBlackTree<string, int>
{
new KeyValuePair<string, int>("cat", 1),
new KeyValuePair<string, int>("dog", 2),
new KeyValuePair<string, int>("canary", 3)
};
// The tree should contain 1 cat and 2 dogs...
Assert.IsTrue(tree.Contains(new KeyValuePair<string, int>("cat", 1)));
Assert.IsTrue(tree.Contains(new KeyValuePair<string, int>("dog", 2)));
// But not 3 cats and 1 dog
Assert.IsFalse(tree.Contains(new KeyValuePair<string, int>("cat", 3)));
Assert.IsFalse(tree.Contains(new KeyValuePair<string, int>("dog", 1)));
}
示例3: 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);
}
//.........这里部分代码省略.........
示例4: 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);
}
示例5: 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);
//.........这里部分代码省略.........