本文整理汇总了C#中BinarySearchTree.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree.Contains方法的具体用法?C# BinarySearchTree.Contains怎么用?C# BinarySearchTree.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.Contains方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ContainsItemNotPresentTest
public void ContainsItemNotPresentTest()
{
BinarySearchTree<int> bst = new BinarySearchTree<int> {12, 5, 3, 8, 42};
Assert.IsFalse(bst.Contains(99));
Assert.IsFalse(bst.Contains(1));
}
示例2: ContainsTest
public void ContainsTest()
{
BinarySearchTree<int> bst = new BinarySearchTree<int> {12, 5, 3, 8, 42};
Assert.IsTrue(bst.Contains(12));
Assert.IsTrue(bst.Contains(3));
Assert.IsTrue(bst.Contains(42));
}
示例3: ContainsTest
public void ContainsTest([PexAssumeUnderTest]List<int> elements, int newElement)
{
PexAssume.IsFalse(elements.Contains(newElement));
BinarySearchTree<int> bst = new BinarySearchTree<int>(elements);
foreach(int elem in elements)
PexAssert.IsTrue(bst.Contains(elem));
PexAssert.IsFalse(bst.Contains(newElement));
}
示例4: Main
// Define the data structure binary search tree with operations for
// "adding new element", "searching element" and "deleting elements".
// Implement the standard methods from System.Object – ToString(), Equals(…), GetHashCode()
// and the operators for comparison == and !=.
// Add and implement the ICloneable interface for deep copy of the tree.
// Implement IEnumerable<T> to traverse the tree.
internal static void Main()
{
// Create new binary search tree
BinarySearchTree<string> tree = new BinarySearchTree<string>();
// Test adding new element
tree.Insert("Telerik");
tree.Insert("Google");
tree.Insert("Microsoft");
tree.Insert("Facebook");
tree.Insert("Twitter");
// Test ToString()
Console.WriteLine("Tree: {0}", tree);
// Test searching element
Console.WriteLine("Tree contains Telerik? -> {0}", tree.Contains("Telerik"));
Console.WriteLine("Tree contains IBM? -> {0}", tree.Contains("IBM"));
// Test deleting element
tree.Remove("Telerik");
Console.WriteLine("Tree: remove Telerik");
Console.WriteLine("Tree: {0}", tree);
// Test GetHashCode
Console.WriteLine("Tree hash code = {0}", tree.GetHashCode());
// Test Equals
var otherTree = new BinarySearchTree<string>();
otherTree.Insert("Google");
Console.WriteLine("\nOther tree: {0}", otherTree);
Console.WriteLine("tree.Equals(otherTree) -> {0}", tree.Equals(otherTree));
// Test '=='
Console.WriteLine("Other tree: insert Microsoft, Facebook, Twitter");
otherTree.Insert("Microsoft");
otherTree.Insert("Facebook");
otherTree.Insert("Twitter");
Console.WriteLine("Other tree: {0}", otherTree);
Console.WriteLine("tree.Equals(otherTree) -> {0}", tree.Equals(otherTree));
// Test IEnumerable<T> to traverse the tree
Console.WriteLine("\nforeach (var node in tree):");
foreach (var node in tree)
{
Console.WriteLine(node);
}
// Test deep copy of the tree.
var treeClone = tree.Clone();
Console.WriteLine("\ntreeClone: {0}", treeClone);
tree.Remove("Google");
Console.WriteLine("Original tree: remove Google");
Console.WriteLine("treeClone: {0}", treeClone);
}
示例5: RemoveTest
public void RemoveTest()
{
var bst = new BinarySearchTree<int>() { 10, 15, 5, 4, 7, 20, 14, 0, -5, -8, -2, -1 };
bst.Remove(20);
bst.Remove(0);
bst.Remove(10);
// TODO: Dump as array then compare against expected array
Assert.IsFalse(bst.Contains(20), "Did not remove element correctly");
Assert.IsFalse(bst.Contains(0), "Did not remove element correctly");
Assert.IsFalse(bst.Contains(10), "Did not remove root element correctly");
}
示例6: AddTest
public void AddTest()
{
var bst = new BinarySearchTree<int>() { 10, 15, 5, 4, 7, 20, 14, 0, -5, -8, -2, -1 };
bst.Add(55);
bst.Add(-55);
bst.Add(35);
// TODO: Dump as array then compare against expected array
Assert.IsTrue(bst.Contains(55), "Did not add element correctly");
Assert.IsTrue(bst.Contains(-55), "Did not add element correctly");
Assert.IsTrue(bst.Contains(35), "Did not add element correctly");
}
示例7: KeyValuePair
public void KeyValuePair()
{
var tree = new BinarySearchTree<int, string>();
Assert.IsFalse(tree.Contains(new KeyValuePair<int, string>(5, "5")));
tree.Add(4, "4");
Assert.IsTrue(tree.Contains(new KeyValuePair<int, string>(4, "4")));
Assert.IsFalse(tree.Contains(new KeyValuePair<int, string>(4, "5")));
tree.Add(6, "6");
Assert.IsTrue(tree.Contains(new KeyValuePair<int, string>(6, "6")));
Assert.IsFalse(tree.Contains(new KeyValuePair<int, string>(6, "5")));
}
示例8: Contains
public void Contains()
{
var tree = new BinarySearchTree<int>() { 0, 6, 4, 2, -1, 3, 1, 5 };
Assert.IsFalse(tree.Contains(-2));
Assert.IsTrue(tree.Contains(-1));
Assert.IsTrue(tree.Contains(0));
Assert.IsTrue(tree.Contains(1));
Assert.IsTrue(tree.Contains(2));
Assert.IsTrue(tree.Contains(3));
Assert.IsTrue(tree.Contains(4));
Assert.IsTrue(tree.Contains(5));
Assert.IsTrue(tree.Contains(6));
Assert.IsFalse(tree.Contains(7));
}
示例9: Main
static void Main()
{
var sb = new StringBuilder();
var tree = new BinarySearchTree<int>(15);
for (int i = 0; i < 30; i++)
{
tree.Add(i);
}
var clonedNode = (TreeNode<int>)tree.Root.Clone();
sb.AppendLine(tree.ToString())
.AppendLine("Tree root: " + tree.Root.ToString())
.AppendLine("Tree contains 7? " + tree.Contains(7).ToString())
.AppendLine("Cloned root: " + clonedNode.ToString())
.AppendLine("Cloned Equals root? " + (clonedNode.Equals(tree.Root)).ToString())
.AppendLine("Cloned == root? " + (clonedNode == tree.Root).ToString())
.AppendLine("Cloned != root? " + (clonedNode != tree.Root).ToString())
.AppendLine("12 deleted. New tree:");
Console.Write(sb.ToString());
tree.Delete(12);
Console.WriteLine(tree.ToString());
}
示例10: UnitTest
public static void UnitTest()
{
Console.WriteLine ("----- Testing BinarySearchTree<int> -----");
Console.Write ("Creating new BinarySearchTree of type <int>...");
BinarySearchTree<int> test = new BinarySearchTree<int> (); d ();
Console.Write ("Adding value 50...");
test.Add (50); d ();
Console.Write ("Adding value 25...");
test.Add (25); d ();
Console.Write ("Adding value 75...");
test.Add (75); d ();
Console.Write ("Checking for correct structure (25/50\\75)...");
Debug.Assert (test.root.value == 50);
Debug.Assert (test.root.nodes[0].value == 25);
Debug.Assert (test.root.nodes[1].value == 75);
d ();
Console.Write ("Checking if BinarySearchTree.Contains(75) works...");
Debug.Assert (test.Contains(75)); d ();
Console.Write ("Testing BinarySearchTree.TraversePre()...");
Debug.Assert( test.TraversePre ((x) => Console.Write (x.value + " ")) == "50 25 75 "); d ();
Console.Write ("Testing BinarySearchTree.TraverseInOrder()...");
Debug.Assert( test.TraverseInOrder ((x) => Console.Write (x.value + " ")) == "25 50 75 "); d ();
Console.Write ("Testing BinarySearchTree.TraversePost()...");
Debug.Assert( test.TraversePost ((x) => Console.Write (x.value + " ")) == "25 75 50 "); d ();
Console.WriteLine ("----- Testing BinarySearchTree<string> -----");
Console.Write ("Creating new BinarySearchTree of type <string> with custom IComparer...");
BinarySearchTree<string> foo = new BinarySearchTree<string> ( new StringComparer() ); d ();
Console.Write ("Adding value Urist McSmashes...");
foo.Add ("Urist McSmashes"); d ();
Console.Write ("Adding value Gordon Freeman...");
foo.Add ("Gordon Freeman"); d ();
Console.Write ("Adding value Winston Smith...");
foo.Add ("Winston Smith"); d ();
Console.Write ("Checking for correct structure (Gordon Freeman/Urist McSmashes\\Winston Smith)...");
Debug.Assert (foo.root.value == "Urist McSmashes");
Debug.Assert (foo.root.nodes[0].value == "Gordon Freeman");
Debug.Assert (foo.root.nodes[1].value == "Winston Smith");
d ();
Console.WriteLine ("----- Testing BinaryExpressionTree -----");
Console.Write ("Creating new BinaryExpressionTree with expression \"5 + 2 * 8 - 6 / 4\"...");
BinaryExpressionTree bar = new BinaryExpressionTree ("5 + 2 * 8 - 6 / 4"); d ();
Console.Write ("Checking for correct structure...");
Debug.Assert( bar.TraverseInOrder ((x) => Console.Write (x.value + " ")) == "4 47 6 45 8 42 2 43 5 ");
Debug.Assert( bar.root.value == 45 ); // -
Debug.Assert( bar.root.nodes[0].value == 47 ); // /
Debug.Assert( bar.root.nodes[0].nodes[0].value == 4 );
Debug.Assert( bar.root.nodes[0].nodes[1].value == 6 );
Debug.Assert( bar.root.nodes[1].value == 43 ); // +
Debug.Assert( bar.root.nodes[1].nodes[1].value == 5 );
Debug.Assert( bar.root.nodes[1].nodes[0].value == 42 ); // *
Debug.Assert( bar.root.nodes[1].nodes[0].nodes[0].value == 8 );
Debug.Assert( bar.root.nodes[1].nodes[0].nodes[1].value == 2 );
d ();
Console.Write ("Checking for correct answer \"5 + 2 * 8 - 6 / 4\" = " + (5.0 + 2.0 * 8.0 - 6.0 / 4.0) + "...");
Debug.Assert (bar.Eval () == (5.0 + 2.0 * 8.0 - 6.0 / 4.0)); d ();
Console.WriteLine ("Everything appears to be in order! Although asserts in C# don't actually end execution so there's no real way for me to know!");
}
示例11: Main
static void Main()
{
BinarySearchTree<string> tree =
new BinarySearchTree<string>();
tree.Insert("Telerik");
tree.Insert("Google");
tree.Insert("Microsoft");
tree.Insert("ATNT");
tree.Insert("Blizoo");
tree.Insert("MTel");
tree.Insert("ZaraLab");
tree.Insert("Yahoo");
tree.PrintTreeDFS(); // Google Microsoft Telerik
Console.WriteLine(tree.Contains("Telerik")); // True
Console.WriteLine(tree.Contains("IBM")); // False
tree.Remove("Telerik");
Console.WriteLine(tree.Contains("Telerik")); // False
tree.PrintTreeDFS(); // Google Microsoft
}
示例12: Main
static void Main()
{
BinarySearchTree<int> intTree = new BinarySearchTree<int>();
intTree.Add(3);
intTree.Add(1);
intTree.Add(6);
Console.WriteLine("Call intTree.ToString();");
Console.WriteLine(intTree);
Console.WriteLine("\nUse foreach:");
foreach (var item in intTree)
{
Console.WriteLine(item);
}
BinarySearchTree<int> compareTree = intTree;
Console.WriteLine("\nIs intTree equals with compareTree: {0}", compareTree.Equals(intTree));
compareTree = new BinarySearchTree<int>();
compareTree.Add(7);
Console.WriteLine("\nIs intTree != with compareTree: {0}", compareTree != intTree);
Console.WriteLine("\nIs intTree equals with compareTree: {0}", compareTree.Equals(intTree));
Console.WriteLine("\nTest Clone");
compareTree = intTree.Clone();
Console.WriteLine("intTree: {0}", intTree);
Console.WriteLine("compareTree: {0}", compareTree);
int searchedNumber = -3;
Console.WriteLine("\nintTree contains {0} = {1}", searchedNumber, intTree.Contains(searchedNumber));
int deleteNumber = 3;
intTree.Add(3);
Console.WriteLine("\nintTree before deleting: {0}", intTree);
Console.WriteLine("\nintTree Delete {0}, deleted elements are: {1}", deleteNumber, intTree.Delete(deleteNumber));
Console.WriteLine("\nintTree after deleting: {0}", intTree);
}
示例13: Main
/// <summary>
/// Runs a basic test of a Binary Search Tree
/// </summary>
static void Main(string[] args)
{
// ==== Assign ==== //
BinarySearchTree<int> bTree = new BinarySearchTree<int>();
// ==== Act ==== //
{
// Insert
bTree.Insert(50);
bTree.Insert(30);
bTree.Insert(70);
bTree.Insert(15);
bTree.Insert(45);
bTree.Insert(55);
bTree.Insert(85);
bTree.Insert(19);
bTree.Insert(75);
bTree.Insert(77);
bTree.Insert(17);
// Remove
bTree.Remove(85);
bTree.Remove(70);
}
// ==== Assert ==== //
{
Console.WriteLine("Does it contain 55: " + bTree.Contains(55));
// Draw Tree
Console.WriteLine("\nDisplay the tree:");
Console.WriteLine(bTree.Display());
PrintOrdered(bTree);
}
// Wait for input before closing.
Console.ReadLine();
}
示例14: ContainsExample
public void ContainsExample()
{
// 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 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)));
}
示例15: ShouldReturnTrueWhenItemExistInTheLeftSubtree
public void ShouldReturnTrueWhenItemExistInTheLeftSubtree()
{
var binaryTree = new BinarySearchTree<int>(new BinaryTreeNode<int>(null, 20));
binaryTree.Root.LeftChild = new BinaryTreeNode<int>(binaryTree.Root, 15);
Assert.IsTrue(binaryTree.Contains(new BinaryTreeNode<int>(null, 15)));
}