本文整理匯總了C#中BinarySearchTree.Clone方法的典型用法代碼示例。如果您正苦於以下問題:C# BinarySearchTree.Clone方法的具體用法?C# BinarySearchTree.Clone怎麽用?C# BinarySearchTree.Clone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類BinarySearchTree
的用法示例。
在下文中一共展示了BinarySearchTree.Clone方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: 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());
}
示例2: 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);
}
示例3: 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);
}
示例4: Main
static void Main()
{
BinarySearchTree<int> tree = new BinarySearchTree<int>();
for (int i = 1; i < 10; i+=2)
tree.Add(i);
Console.WriteLine(tree);
Console.WriteLine((tree.Clone() as BinarySearchTree<int>) == tree);
}
示例5: 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);
}
示例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(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));
}
示例8: 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!");
}
}
示例9: Main
static void Main(string[] args)
{
TreeNode root = new TreeNode(1);
root.LeftChildren.Add(new TreeNode(2));
root.LeftChildren.Add(new TreeNode(3));
root.RightChildren.Add(new TreeNode(4));
root.RightChildren.Add(new TreeNode(5));
root.LeftChildren.Find(x => x.Value == 2).LeftChildren.Add(new TreeNode(6));
root.LeftChildren.Find(x => x.Value == 2).LeftChildren.Add(new TreeNode(7));
BinarySearchTree customTree = new BinarySearchTree(root);
Console.WriteLine(customTree);
BinarySearchTree clonedTree = customTree.Clone() as BinarySearchTree;
TreeNode temp = clonedTree.Search(5);
clonedTree.Search(5).LeftChildren.Add(new TreeNode(10));
Console.WriteLine();
Console.WriteLine("Cloned tree after modification:");
Console.WriteLine(clonedTree);
}
示例10: Main
static void Main(string[] args)
{
BinarySearchTree<int> tree1 = new BinarySearchTree<int>();
// test the add method
tree1.Add(2);
tree1.Add(1);
tree1.Add(3);
tree1.Add(7);
tree1.Add(5);
// test the Clone() method
BinarySearchTree<int> tree2 = (BinarySearchTree<int>)tree1.Clone();
// test the ToString() method
Console.WriteLine("Tree 1: {0}", tree1);
Console.WriteLine("Tree 2: {0}", tree2);
// test the Equals() method
Console.WriteLine("The two trees are equal: {0}",
(tree1 == tree2));
// test the Delete() method
tree2.Delete(7);
Console.WriteLine("After deleting 5 from tree 2: {0}", tree2);
// test the Find() method
var node = tree1.Find(3);
if(node != null)
{
Console.WriteLine("3 was found in tree 1");
}
else
{
Console.WriteLine("3 was NOT found in tree 1");
}
}
示例11: Main
static void Main()
{
BinarySearchTree<int> tree = new BinarySearchTree<int>(new int[]
{
3, 1, 5, 2, 4
});
var cloning = tree.Clone() as BinarySearchTree<int>;
Console.WriteLine("The tree: \n{0}", tree);
Console.WriteLine("The cloning: \n{0}", cloning);
Console.WriteLine("tree == cloning -> {0}", tree == cloning);
Console.WriteLine("tree != cloning -> {0}", tree != cloning);
Console.WriteLine("Deleting 3 and 4 from the cloning and Inserting 20 and -3.");
cloning.Remove(3);
cloning.Remove(4);
Console.WriteLine("After deleting 3 and 4 from the cloning the cloning is: \n{0}", cloning);
cloning.Insert(20);
cloning.Insert(-3);
Console.WriteLine("Now inserting 20 and -3 to the cloning. \nThe cloning is now: \n{0}", cloning);
Console.WriteLine("tree == cloning -> {0}", tree == cloning);
Console.WriteLine("tree != cloning -> {0}", tree != cloning);
Console.WriteLine("\nHash codes:");
Console.WriteLine("tree -> {0} \ncloning -> {1}", tree.GetHashCode(), cloning.GetHashCode());
Console.WriteLine("\ncloning Min = {0} \ncloning Max = {1}", cloning.Min, cloning.Max);
Console.WriteLine("\ncloning.ContainsKey(100) -> {0}", cloning.ContainsKey(100));
Console.WriteLine("cloning.ContainsKey(20) -> {0}", cloning.ContainsKey(20));
Console.WriteLine();
}
示例12: TestBinarySearchTreeClone2
public void TestBinarySearchTreeClone2()
{
BinarySearchTree<int> tree1 = new BinarySearchTree<int>(12, 4, 8, 6, 10, 2);
BinarySearchTree<int> tree2 = tree1.Clone();
tree2.Add(1);
Assert.AreEqual(false, tree1.Contains(1));
}
示例13: Main
static void Main(string[] args)
{
Console.Title = "Binary search tree";
// Initialize a BST which will contain integers
BinarySearchTree<int> intTree = new BinarySearchTree<int>();
Random randomGenerathor = new Random(DateTime.Now.Millisecond);
StringBuilder trace = new StringBuilder();
// Insert 10 random integers into the intTree
for (int i = 0; i < 10; i++)
{
int randomInt = randomGenerathor.Next(1, 500);
intTree.Insert(randomInt);
trace.Append(randomInt);
trace.Append(" ");
}
// Clone the intTree
dynamic newTree = intTree.Clone();
StringBuilder newTrace = new StringBuilder(trace.ToString());
// Insert 3 new random integers into the newTree
for (int i = 0; i < 3; i++)
{
int randomInt = randomGenerathor.Next(1, 500);
newTree.Insert(randomInt); // May throw Argument exception if the element is already exist in the tree
newTrace.Append(randomInt);
newTrace.Append(" ");
}
// Find the largest value in the intTree
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Max element: {0}", intTree.FindMax());
// Find the largest value in the newTree
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("\nMax element: {0}", newTree.FindMax());
// Find the smallest value in the intTree
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("\nMin element: {0}", intTree.FindMin());
// Find the smallest value in the newTree
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("\nMin element: {0}", newTree.FindMin());
// Find the hash code of the intTree
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("\nHashcode: {0}", intTree.GetHashCode());
// Find the hash code of the newTree
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("\nHashcode: {0}", newTree.GetHashCode());
// Find the root of the intTree
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("\nRoot: {0}", intTree.Root.Element);
// Find the root of the newTree
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("\nRoot: {0}", newTree.Root.Element);
// The order in which the elements were added to the intTree
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nTrace: {0}", trace);
// The order in which the elements were added to the newTree
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("\nTrace: {0}", newTrace);
// A textual representation of the intTree
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\nFirst Tree: \n{0}", intTree);
// A textual representation of the newTree
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("\nSecond Tree: \n{0}", newTree);
Console.WriteLine();
Console.ResetColor();
}
示例14: 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();
}
示例15: 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);
}
}