本文整理汇总了C#中BinarySearchTree.Equals方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree.Equals方法的具体用法?C# BinarySearchTree.Equals怎么用?C# BinarySearchTree.Equals使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.Equals方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: Main
static void Main()
{
BinarySearchTree<int> tree = new BinarySearchTree<int>();
tree.AddElement(13);
tree.AddElement(17);
tree.AddElement(101);
tree.AddElement(44);
tree.AddElement(9);
tree.AddElement(11);
BinarySearchTree<int> tree2 = (BinarySearchTree<int>)tree.Clone();
//tree2.AddElement(45);
//tree2.AddElement(99);
Console.WriteLine("Tree: " + tree.ToString());
Console.WriteLine("Tree2: " + tree2.ToString());
Console.WriteLine("Tree equals Tree2: " + tree.Equals(tree2));
Console.WriteLine("Tree == Tree2: " + (tree == tree2));
Console.WriteLine("Tree != Tree2: " + (tree != tree2));
Console.Write("Traverse with foreach: ");
foreach (TreeNode<int> item in tree)
Console.Write(item.Value + " ");
Console.WriteLine();
Console.WriteLine("Tree hash: {0}", tree.GetHashCode());
Console.WriteLine("Tree2 hash: {0}", tree2.GetHashCode());
}
示例3: TestBinarySearchTreeEquals3
public void TestBinarySearchTreeEquals3()
{
BinarySearchTree<int> tree1 = new BinarySearchTree<int>(1, 9, 7);
BinarySearchTree<int> tree2 = tree1;
Assert.AreEqual(true, tree1.Equals(tree2));
}
示例4: Main
static void Main()
{
BinarySearchTree<int> tree = new BinarySearchTree<int>();
tree.Insert(23);
tree.Insert(45);
tree.Insert(16);
tree.Insert(37);
tree.Insert(3);
tree.Insert(99);
tree.Insert(22);
Console.Write("Inorder traversal: \nTree: ");
foreach (var node in tree)
Console.Write(node + " ");
Console.WriteLine("-> Root: {0}", tree.Root);
/* -------------- */
Console.WriteLine("\nSearch [9]: {0}", tree.Search(9)); // does not exist
Console.WriteLine("Search [Root]: {0}\n", tree.Search(tree.Root.Value));
/* -------------- */
BinarySearchTree<int> cloneTree = (BinarySearchTree<int>)tree.Clone();
Console.WriteLine("\nClone tree: {0} -> Root: {1}", cloneTree, cloneTree.Root);
/* -------------- */
Console.WriteLine("\n\ntree.Equals(cloneTree): {0}", tree.Equals(cloneTree));
Console.WriteLine("tree == cloneTree: {0}", tree == cloneTree);
Console.WriteLine("tree != cloneTree: {0}", tree != cloneTree);
/* -------------- */
tree.Insert(1); // this will change hash code of the tree
cloneTree.Insert(2); // this will change hash code of the tree
Console.WriteLine("\n\ntree.GetHashCode(): {0}", tree.GetHashCode());
Console.WriteLine("cloneTree.GetHashCode(): {0}\n", cloneTree.GetHashCode());
/* -------------- */
Console.WriteLine("\nTest TreeNode<T> internal class:");
var treeNode = new BinarySearchTree<int>.TreeNode<int>(25);
treeNode.LeftChild = new BinarySearchTree<int>.TreeNode<int>(23);
treeNode.RightChild = new BinarySearchTree<int>.TreeNode<int>(28);
Console.WriteLine("- Root value: {0}", treeNode.Value);
Console.WriteLine("- LeftChild: {0}", treeNode.LeftChild);
Console.WriteLine("- RightChild: {0}\n", treeNode.RightChild);
}
示例5: TestBinarySearchTreeEquals2
public void TestBinarySearchTreeEquals2()
{
BinarySearchTree<int> tree1 = new BinarySearchTree<int>(1, 9, 7);
BinarySearchTree<int> tree2 = new BinarySearchTree<int>();
tree2.Add(7);
tree2.Add(5);
tree2.Add(9);
Assert.AreEqual(false, tree1.Equals(tree2));
}
示例6: Main
static void Main()
{
BinarySearchTree tree = new BinarySearchTree();
tree.Add(2);
tree.Add(5);
tree.Add(10);
tree.Add(6);
tree.Add(4);
tree.Add(15);
tree.Delete(10);
tree.Delete(15);
Console.WriteLine(tree.Search(6));
Console.WriteLine(tree.Search(10));
foreach (var item in tree)
{
Console.WriteLine(item);
}
Console.WriteLine(tree);
BinarySearchTree secondTree = new BinarySearchTree();
secondTree.Add(2);
secondTree.Add(5);
secondTree.Add(10);
secondTree.Add(6);
secondTree.Add(4);
secondTree.Add(15);
secondTree.Delete(10);
secondTree.Delete(15);
Console.WriteLine(tree.Equals(secondTree));
Console.WriteLine(Object.ReferenceEquals(tree, secondTree));
Console.WriteLine(tree.GetHashCode() == secondTree.GetHashCode());
Console.WriteLine();
BinarySearchTree clonedTree = (BinarySearchTree)tree.Clone();
Console.WriteLine(tree.Equals(clonedTree));
Console.WriteLine(Object.ReferenceEquals(tree, clonedTree));
}
示例7: Main
static void Main()
{
var testTree = new BinarySearchTree<int>(); // constructor test
// addition test
testTree.Insert(10, 10);
testTree.Insert(5, 5);
testTree.Insert(2, 2);
testTree.Insert(7, 7);
testTree.Insert(6, 6);
testTree.Insert(8, 8);
testTree.Insert(3, 3);
testTree.Insert(15, 15);
// clone test
var clone = (BinarySearchTree<int>)testTree.Clone();
Console.WriteLine(testTree+"\n\n"); // print testTree
Console.WriteLine(clone);
Console.WriteLine(testTree.Equals(clone));
// deletion test
testTree.DeleteItemWithKey(10);
testTree.DeleteItemWithKey(3);
testTree.DeleteItemWithKey(5);
testTree.DeleteItemWithKey(15);
Console.WriteLine(testTree.Equals(clone));
// print testTree after
Console.WriteLine(testTree);
foreach (var item in testTree)
{
Console.WriteLine(item + 10 +" I am an enumerator test!");
}
}
示例8: 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);
}
示例9: Main
static void Main(string[] args)
{
BinarySearchTree<int> tree = new BinarySearchTree<int>();
tree.Add(4);
tree.Add(3);
tree.Add(1);
tree.Add(100);
tree.Add(5);
BinarySearchTree<int> newTree = (BinarySearchTree<int>)tree.Clone();
tree.Delete(4);
Console.WriteLine(tree);
Console.WriteLine(newTree);
Console.WriteLine(tree.Search(100));
Console.WriteLine(tree.Equals(newTree));
}
示例10: Main
public static void Main()
{
//Create binary search tree
BinarySearchTree<int> firstTree = new BinarySearchTree<int>();
firstTree.Root = new TreeNode<int>(10);
firstTree.Root.Left = new TreeNode<int>(5);
firstTree.Root.Right = new TreeNode<int>(20);
firstTree.Root.Left.Left = new TreeNode<int>(1);
firstTree.Root.Right.Right = new TreeNode<int>(24);
firstTree.Root.Left.Left.Right = new TreeNode<int>(3);
firstTree.Root.Right.Right.Right = new TreeNode<int>(35);
firstTree.Root.Right.Right.Right.Right = new TreeNode<int>(50);
//Test ToString() method
Console.WriteLine("Elements of firstTree");
Console.WriteLine(firstTree.ToString());
//Create another binary search tree
BinarySearchTree<int> secondTree = new BinarySearchTree<int>();
secondTree.Root = new TreeNode<int>(10);
secondTree.Root.Left = new TreeNode<int>(5);
secondTree.Root.Right = new TreeNode<int>(20);
secondTree.Root.Left.Left = new TreeNode<int>(1);
secondTree.Root.Right.Right = new TreeNode<int>(24);
secondTree.Root.Left.Left.Right = new TreeNode<int>(3);
secondTree.Root.Right.Right.Right = new TreeNode<int>(35);
secondTree.Root.Right.Right.Right.Right = new TreeNode<int>(49);
//Test Equals() method
Console.WriteLine("firstTree is equal to secondTree?:");
Console.WriteLine(firstTree.Equals(secondTree));
//Test Clone() method
Console.WriteLine("Elements of the clone of firstTree");
BinarySearchTree<int> clone = (BinarySearchTree<int>)firstTree.Clone();
Console.WriteLine(clone.ToString());
//Test Search() method
Console.WriteLine("firstTree contains 50?:");
Console.WriteLine(firstTree.Search(50));
Console.WriteLine("secondTree contains 50?:");
Console.WriteLine(secondTree.Search(50));
//Test Add() method
Console.WriteLine("Elements of firstTree after adding 22");
firstTree.Add(22);
Console.WriteLine(firstTree.ToString());
//Test Delete() method
Console.WriteLine("Elements of secondTree after deleting 24");
Console.WriteLine(secondTree.Delete(24));
Console.WriteLine(secondTree.ToString());
//Use foreach to test the implementation of IEnumerable<T> interface
clone.Add(100);
Console.WriteLine("firstTree after adding 100 to clone");
foreach (var item in firstTree)
{
Console.Write("{0} ", item);
}
Console.WriteLine("clone after adding 100 to clone");
foreach (var item in clone)
{
Console.Write("{0} ", item);
}
}
示例11: TestBinarySearchTreeConstructor
public void TestBinarySearchTreeConstructor()
{
BinarySearchTree<int> tree = new BinarySearchTree<int>(1, 9, 7);
Assert.AreEqual(false, tree.Equals(null));
}
示例12: TestBinarySearchTreeClone3
public void TestBinarySearchTreeClone3()
{
BinarySearchTree<int> tree1 = new BinarySearchTree<int>(12, 4, 8, 6, 10, 2);
BinarySearchTree<int> tree2 = tree1.Clone();
Assert.AreEqual(true, tree1.Equals(tree2));
}
示例13: CloningCheck
// Checking the cloning on some tree
private static void CloningCheck(BinarySearchTree<int> tree, BinarySearchTree<int> clone, bool b)
{
Thread.Sleep(1500);
Console.WriteLine("\nCloning check...{0,18}== test{0,8}!= test{0,8}Equal test", " ");
Console.Write("{0}", (b) ? "The cloning after deletion is: " : "The cloning is: ");
Thread.Sleep(1500);
if (tree == clone)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(b ? "{0,10}" : "{0,25}", "OK");
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(b ? "{0,10}" : "{0,25}", " X");
}
Thread.Sleep(1000);
if (tree == clone)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("{0,15}", "OK");
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0,15}", " X");
}
Thread.Sleep(1000);
if (tree.Equals(clone))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("{0,18}", "OK");
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("{0,18}", " X");
}
Console.ResetColor();
}