本文整理汇总了C#中BinarySearchTree.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree.Clear方法的具体用法?C# BinarySearchTree.Clear怎么用?C# BinarySearchTree.Clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.Clear方法的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: ClearTest
public void ClearTest(string[] elements)
{
BinarySearchTree<string> bst = new BinarySearchTree<string>(elements);
bst.Clear();
PexAssert.IsNull(bst.Root);
PexAssert.AreEqual(0, bst.Count);
}
示例3: ClearTest
public void ClearTest()
{
BinarySearchTree<int> bst = new BinarySearchTree<int> {10, 5, 15};
bst.Clear();
Assert.IsNull(bst.Root);
Assert.AreEqual(0, bst.Count);
}
示例4: Simple
public void Simple()
{
var tree = new BinarySearchTree<int, string>();
Assert.IsTrue(tree.IsEmpty);
tree = GetTestTree();
Assert.IsFalse(tree.IsEmpty);
tree.Clear();
Assert.IsTrue(tree.IsEmpty);
}
示例5: Main
static void Main(string[] args)
{
const int itemsCount = 10000;
// Adding consecutive items
Console.Write("Adding {0} consecutive items: ", itemsCount);
var tree = new BinarySearchTree<int>(Enumerable.Range(0, itemsCount));
if (tree.Count == itemsCount)
Console.WriteLine("Success!");
else
Console.WriteLine("Failed!");
Console.WriteLine();
// Clearing tree
Console.Write("Clearing the tree: ");
tree.Clear();
if (tree.Count == 0)
Console.WriteLine("Success!");
else
Console.WriteLine("Failed!");
Console.WriteLine();
// Adding random numbers
Console.Write("Adding {0} random numbers: ", itemsCount);
var random = new Random();
var numbersAdded = new List<int>();
for (int i = 0; i < itemsCount; i++)
{
try
{
int number = random.Next(0, 99999999);
tree.Add(number);
numbersAdded.Add(number);
}
catch { }
}
var diff = numbersAdded.Except(tree);
if (diff.Count() == 0)
Console.WriteLine("Success!");
else
Console.WriteLine("Failed!");
Console.WriteLine();
// Deleting
int item = tree.ElementAt(500);
Console.Write("Deleting item {0}: ", item);
tree.Remove(item);
if (tree.Contains(item))
Console.WriteLine("Failed!");
else
Console.WriteLine("Success!");
int item2 = tree.ElementAt(300);
Console.Write("Deleting item {0}: ", item2);
tree.Remove(item2);
if (tree.Contains(item2))
Console.WriteLine("Failed!");
else
Console.WriteLine("Success!");
int item3 = tree.ElementAt(600);
Console.Write("Deleting item {0}: ", item3);
tree.Remove(item3);
if (tree.Contains(item3))
Console.WriteLine("Failed!");
else
Console.WriteLine("Success!");
int item4 = tree.ElementAt(463);
Console.Write("Deleting item {0}: ", item4);
tree.Remove(item4);
if (tree.Contains(item4))
Console.WriteLine("Failed!");
else
Console.WriteLine("Success!");
Console.WriteLine();
// Check the count now
Console.Write("Check for consistency: ");
var diff1 = numbersAdded
.Where(x => x != item && x != item2 && x != item3 && x != item4)
.Except(tree);
if (diff1.Count() > 0)
Console.WriteLine("Failed!");
else
Console.WriteLine("Success!");
Console.WriteLine();
// Clone
Console.Write("Cloning tree: ");
var clone = (BinarySearchTree<int>)tree.Clone();
if (clone.Count == tree.Count && !Object.ReferenceEquals(clone, tree))
Console.WriteLine("Success!");
else
Console.WriteLine("Failed!");
Console.ReadLine();
}
示例6: ClearTest
public void ClearTest()
{
var bst = new BinarySearchTree<int>() { 10, 15, 5, 4, 7, 20, 14, 0, -5, -8, -2, -1 };
bst.Clear();
Assert.AreEqual(0, bst.Count, "Clearing the bst did not work");
}
示例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: IsEmptyExample
public void IsEmptyExample()
{
BinarySearchTreeBase<string, int> tree = new BinarySearchTree<string, int>();
// Tree is empty.
Assert.IsTrue(tree.IsEmpty);
// Add a cat.
tree.Add(new KeyValuePair<string, int>("cat", 1));
// Tree is not empty.
Assert.IsFalse(tree.IsEmpty);
// Clear the tree - thereby removing all items contained.
tree.Clear();
// Tree is empty again with count = 0.
Assert.IsTrue(tree.IsEmpty);
}
示例9: CountExample
public void CountExample()
{
BinarySearchTreeBase<string, int> tree = new BinarySearchTree<string, int>();
// Tree count is 0.
Assert.AreEqual(0, tree.Count);
// Add a cat.
tree.Add(new KeyValuePair<string, int>("cat", 1));
// Tree count is 1.
Assert.AreEqual(1, tree.Count);
// Add a dog
tree.Add(new KeyValuePair<string, int>("dog", 2));
// Tree count is 2.
Assert.AreEqual(2, tree.Count);
// Clear the tree - thereby removing all items contained.
tree.Clear();
// Tree is empty again with 0 count.
Assert.AreEqual(0, tree.Count);
}
示例10: TestClear
public void TestClear()
{
var tree = new BinarySearchTree<int>();
var size = 10;
for (int i = 0; i < size; i++)
{
tree.Add(i);
}
tree.Clear();
Assert.AreEqual(tree.Count, 0);
Assert.IsTrue(tree.IsEmpty);
}