static void Main(string[] args) { BinarySearchTree<int> myTree = new BinarySearchTree<int>(); myTree.Add(86); myTree.Add(12); myTree.Add(45); myTree.Add(34); myTree.Add(100); myTree.Add(89); myTree.Add(14); myTree.Add(55); myTree.Find(34); // myTree.RemoveNode(45); // myTree.RemoveNode(34); myTree.RemoveNode(89); foreach (var item in myTree) { Console.WriteLine(item); } Console.WriteLine(); BinarySearchTree<int> myNewTree = (BinarySearchTree<int>)myTree.Clone(); myTree.Root.value = 87; foreach (var item in myNewTree) { Console.WriteLine(item); } Console.WriteLine(); foreach (var item in myTree) { Console.WriteLine(item); } Console.WriteLine(myTree.Equals(myNewTree)); Console.WriteLine(myNewTree.ToString()); Console.WriteLine(myNewTree == myTree); Console.WriteLine(myNewTree != myTree); Console.WriteLine(myTree.GetHashCode()); }
static void Main(string[] args) { BinarySearchTree <int> myTree = new BinarySearchTree <int>(); myTree.Add(86); myTree.Add(12); myTree.Add(45); myTree.Add(34); myTree.Add(100); myTree.Add(89); myTree.Add(14); myTree.Add(55); myTree.Find(34); // myTree.RemoveNode(45); // myTree.RemoveNode(34); myTree.RemoveNode(89); foreach (var item in myTree) { Console.WriteLine(item); } Console.WriteLine(); BinarySearchTree <int> myNewTree = (BinarySearchTree <int>)myTree.Clone(); myTree.Root.value = 87; foreach (var item in myNewTree) { Console.WriteLine(item); } Console.WriteLine(); foreach (var item in myTree) { Console.WriteLine(item); } Console.WriteLine(myTree.Equals(myNewTree)); Console.WriteLine(myNewTree.ToString()); Console.WriteLine(myNewTree == myTree); Console.WriteLine(myNewTree != myTree); Console.WriteLine(myTree.GetHashCode()); }
static void Main() { try { #region Test1: Creating of nodes Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Test1: Creating of nodes"); Console.ResetColor(); Console.Write("Press any key to start the test..."); Console.ReadKey(); // Creates new empty tree var tree = new BinarySearchTree<int>(); Console.WriteLine("\n\nA new tree was created..."); // Adds some nodes in the tree Thread.Sleep(1500); tree.Add(2); tree.Add(7); tree.Add(1); tree.Add(5); tree.Add(22); tree.Add(-7); tree.Add(4); Console.WriteLine("{0} nodes were added in the tree.\n", tree.Nodes.Count); // Prints the tree Thread.Sleep(1500); Console.Write("The tree: "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(tree); Console.ResetColor(); // Prints the nodes of the tree Thread.Sleep(1500); for (int i = 0; i < tree.Nodes.Count; i++) { Console.Write("Node{0}: ", i); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("{0}", tree.Nodes[i]); Console.ResetColor(); Thread.Sleep(400); } #endregion #region Test2: Searching Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("\nTest2: Searching"); Console.ResetColor(); Console.Write("Press any key to start the test..."); Console.ReadKey(); // Search for some node in the tree Console.WriteLine(); TreeNodeSearch(tree, 2); TreeNodeSearch(tree, 7); TreeNodeSearch(tree, 14); TreeNodeSearch(tree, 22); #endregion #region Test3: Cloning Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("\nTest3: Cloning"); Console.ResetColor(); Console.Write("Press any key to start the test..."); Console.ReadKey(); // Cloning of the tree var clone = tree.Clone() as BinarySearchTree<int>; Console.WriteLine("\n\nA clone was created..."); // Prints the clone Thread.Sleep(1500); Console.Write("The clone: "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(clone); Console.ResetColor(); // Cloning check TreeCloningCheck(tree, clone, false); #endregion #region Test4: Deleting Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("\nTest4: Deleting"); Console.ResetColor(); Console.Write("Press any key to start the test..."); Console.ReadKey(); // Delete two nodes from the tree tree.Delete(2); tree.Delete(5); Console.WriteLine("\n\nTwo nodes with value 2 and 5 were deleted from the tree."); // Prints the tree Thread.Sleep(1500); Console.Write("\nThe tree: "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(tree); Console.ResetColor(); Thread.Sleep(1000); Console.Write("The clone: "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(clone); Console.ResetColor(); // Cloning check TreeCloningCheck(tree, clone, true); // Gets a HashCode for the tree Thread.Sleep(1500); Console.WriteLine("\nThe HashCode for the tree: {0}", tree.GetHashCode()); // Gets a HashCode for the clone Thread.Sleep(1000); Console.WriteLine("The HashCode for the clone: {0}\n", clone.GetHashCode()); #endregion } catch (Exception ex) { Console.WriteLine(ex.Message); } }