本文整理汇总了C#中RedBlackTree.Remove方法的典型用法代码示例。如果您正苦于以下问题:C# RedBlackTree.Remove方法的具体用法?C# RedBlackTree.Remove怎么用?C# RedBlackTree.Remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RedBlackTree
的用法示例。
在下文中一共展示了RedBlackTree.Remove方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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");
}
示例2: 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));
}
示例3: 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);
}
示例4: StressTestRandomData
public void StressTestRandomData()
{
var data = new List<int>();
var redBlackTree = new RedBlackTree<int, string>();
var rand = new Random(Convert.ToInt32(DateTime.Now.Ticks % Int32.MaxValue));
for (var i = 0; i < 2000; i++)
{
var randomNumber = rand.Next(100000);
while (data.Contains(randomNumber))
{
randomNumber = rand.Next(100000);
}
data.Add(randomNumber);
redBlackTree.Add(randomNumber, randomNumber.ToString());
Assert.AreEqual(redBlackTree.Count, i + 1);
foreach (var t in data)
{
Assert.IsTrue(redBlackTree.ContainsKey(t));
}
}
while (data.Count != 0)
{
Assert.IsTrue(redBlackTree.Remove(data[0]));
Assert.IsFalse(redBlackTree.ContainsKey(data[0]));
data.RemoveAt(0);
Assert.AreEqual(redBlackTree.Count, data.Count);
foreach (var t in data)
{
Assert.IsTrue(redBlackTree.ContainsKey(t));
}
}
}
示例5: RemoveExample
public void RemoveExample()
{
// Build 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)
};
// 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"));
}
示例6: run
//.........这里部分代码省略.........
// 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);
}
for (int i = 0; i < k.Length; i++)
k[i] = -1 - k[i]; // undo negation above
// check the localization functions
for (int i = 0; i < k.Length; i++)
{
int kd = (int)(0.01 * (rand.Next() % (k.Length * 150) - k.Length * 25));
var le = t.FindLessThanOrEqualTo(kd);
var gt = t.FindGreaterThan(kd);
var node = t.Min();
double lek = le != null ? le.Value : int.MinValue;
double gtk = gt != null ? gt.Value : int.MaxValue;
if (node.Value > kd)
{
Assert.IsNull(le);
Assert.IsNotNull(gt);
Assert.AreEqual(gt, node);
}
else
{
var succ = node;
do
{
node = succ;
succ = t.GetNextNode(node);
} while (succ != null && succ.Value <= kd);
Assert.AreEqual(node, le);
Assert.AreEqual(succ, gt);
}
}
// Remove elements from the tree
for (int M = k.Length; M > 0; M--)
{
int j = rand.Next() % M;
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 = t.Remove(node);
Assert.IsTrue(t.check());
k[i] = -1 - k[i];
}
// The tree should be empty
Assert.AreEqual(0, t.Count);
}
示例7: 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);
}
示例8: StructureIntegrity
public void StructureIntegrity()
{
RedBlackTree<int, int> tree = new RedBlackTree<int, int>();
int range = 10;
int[] order = new[] { 1, 3, 2, 5, 6 };
var q = order
.SelectMany(x => Enumerable.Range((x - 1) * range + 1, range));
foreach (int i in q)
{
Assert.IsFalse(tree.ContainsKey(i));
tree.Add(i, i);
AssertStructure(tree.RootElement);
Assert.IsTrue(tree.ContainsKey(i));
}
foreach (int i in q)
{
AssertStructure(tree.RootElement);
Assert.IsTrue(tree.ContainsKey(i));
tree.Remove(i);
Assert.IsFalse(tree.ContainsKey(i));
}
}
示例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);
//.........这里部分代码省略.........