当前位置: 首页>>代码示例>>C#>>正文


C# BinarySearchTree.GetHashCode方法代码示例

本文整理汇总了C#中BinarySearchTree.GetHashCode方法的典型用法代码示例。如果您正苦于以下问题:C# BinarySearchTree.GetHashCode方法的具体用法?C# BinarySearchTree.GetHashCode怎么用?C# BinarySearchTree.GetHashCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BinarySearchTree的用法示例。


在下文中一共展示了BinarySearchTree.GetHashCode方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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());
        }
开发者ID:Rokata,项目名称:TelerikAcademy,代码行数:29,代码来源:TreeDataStructure.cs

示例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);
    }
开发者ID:jesconsa,项目名称:Telerik-Academy,代码行数:55,代码来源:Program.cs

示例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);
    }
开发者ID:MarKamenov,项目名称:TelerikAcademy,代码行数:61,代码来源:Program.cs

示例4: 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));
    }
开发者ID:shnogeorgiev,项目名称:Software-University-Courses,代码行数:41,代码来源:CustomTreeExample.cs

示例5: 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();
    }
开发者ID:nikolay-spasov,项目名称:TelerikAcademy_OOP_Homeworks,代码行数:36,代码来源:BSTDemo.cs

示例6: 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();
        }
开发者ID:nikolay-radkov,项目名称:Telerik-Academy,代码行数:83,代码来源:Demo.cs

示例7: TestBinarySearchTreeGetHashCode

        public void TestBinarySearchTreeGetHashCode()
        {
            BinarySearchTree<int> tree1 = new BinarySearchTree<int>(5, 7, 9);

            BinarySearchTree<int> tree2 = new BinarySearchTree<int>();
            tree2.Add(7);
            tree2.Add(5);
            tree2.Add(9);

            Assert.AreEqual(true, tree1.GetHashCode() != tree2.GetHashCode());
        }
开发者ID:nikolaynikolov,项目名称:Telerik,代码行数:11,代码来源:BinarySearchTreeTest.cs

示例8: Main


//.........这里部分代码省略.........
				tree.Add(4);
				Console.WriteLine("{0} nodes were added in the tree.\n", tree.Nodes.Count);

				// Printing the tree
				Thread.Sleep(1500);
				Console.Write("The tree: ");
				Console.ForegroundColor = ConsoleColor.Gray;
				Console.WriteLine(tree);
				Console.ResetColor();

				// Printing 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();

				// Searching for some node in the tree
				Console.WriteLine();
				Search(tree, 2);
				Search(tree, 7);
				Search(tree, 14);
				Search(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 tree
				BinarySearchTree<int> clone = tree.Clone() as BinarySearchTree<int>;
				Console.WriteLine("\n\nA clone was created...");

				// Printing the clone
				Thread.Sleep(1500);
				Console.Write("The clone: ");
				Console.ForegroundColor = ConsoleColor.Gray;
				Console.WriteLine(clone);
				Console.ResetColor();

				// Cloning check
				CloningCheck(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();

				// Deleting 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.");

				// Printing 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
				CloningCheck(tree, clone, true);

				// GetHashCode for the tree
				Thread.Sleep(1500);
				Console.WriteLine("\nThe HashCode for the tree: {0}", tree.GetHashCode());

				// GetHashCode for the clone
				Thread.Sleep(1000);
				Console.WriteLine("The HashCode for the clone: {0}\n", clone.GetHashCode());
				#endregion
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
			}
		}
开发者ID:GeorgiNik,项目名称:TelerikAcademy,代码行数:101,代码来源:Test.cs

示例9: Main

        static void Main()
        {
            string decorationLine = new string('-', 80);
            Console.Write(decorationLine);
            Console.WriteLine("***Presenting the functionality of a binary search tree***");
            Console.Write(decorationLine);

            // Defining two binary search trees
            BinarySearchTree<int, string> firstTree = new BinarySearchTree<int, string>();
            BinarySearchTree<char, byte> secondTree = new BinarySearchTree<char, byte>();

            // Testing the operation "adding new element"
            firstTree.Add(19, "nineteen");
            firstTree.Add(11, "eleven");
            firstTree.Add(35, "three-five");
            firstTree.Add(7, "seven");
            firstTree.Add(16, "sixteen");
            firstTree.Add(23, "two-tree");
            firstTree.Add(13, "thirteen");
            firstTree.Add(17, "seventeen");
            firstTree.Add(6, "six");

            secondTree.Add('o', 222);
            secondTree.Add('a', 5);
            secondTree.Add('t', 211);
            secondTree.Add('p', 12);
            secondTree.Add('e', 77);

            // Printing the trees on the console
            Console.WriteLine("---The first binary search tree---");
            Console.WriteLine(firstTree);
            Console.WriteLine("---The second binary search tree---");
            Console.WriteLine(secondTree);

            // Testing the operation "searching new element" (used in the indexer of the class)
            Console.WriteLine("---Testing searching an element from a binary search tree---");
            Console.WriteLine("From first tree the value of the key '{0}' is '{1}'", 16, firstTree[16]);
            Console.WriteLine("From second tree the value of the key '{0}' is '{1}'", 'o', secondTree['o']);
            Console.WriteLine();

            // Testing the operation "deleting element"
            Console.WriteLine("---Testing deleting an element from a binary search tree---");
            firstTree.Remove(7);
            Console.WriteLine("After removing the node with key '{0}' the first tree is:\n{1}", 7, firstTree);
            secondTree.Remove('p');
            Console.WriteLine("After removing the node with key '{0}' the second tree is:\n{1}", 'p', secondTree);
            
            // Testing the method GetHashCode
            Console.WriteLine("---Testing the method GetHashCode---");
            Console.WriteLine("The hash code of the first tree is: " + firstTree.GetHashCode());
            Console.WriteLine("The hash code of the second tree is: " + secondTree.GetHashCode());
            Console.WriteLine();

            // Testing the implementation of IEnumerable methods -> foreach uses IEnumerable
            Console.WriteLine("---Testing the implementation of the IEnumerable interface---");
            foreach (int key in firstTree)
            {
                if (key % 2 == 0)
                {
                    Console.WriteLine("The key of the value '{0}' is even.", firstTree[key]);
                }
                else
                {
                    Console.WriteLine("The key of the value '{0}' is odd.", firstTree[key]);
                }
            }
            Console.WriteLine();

            // Testing the Clone method from the interface 'ICloneable'
            Console.WriteLine("---Testing the implementation of the ICloneable interface---");
            BinarySearchTree<char, byte> secondTreeClone = secondTree.Clone();
            secondTreeClone.Add('r', 2);
            Console.WriteLine("After adding node with key '{0}' and value '{1}' to the second tree clone it is:", 'r', 2);
            Console.WriteLine(secondTreeClone);

            // Testing the method Equals which is used in the operators '==' and '!='
            Console.WriteLine("---Testing Equals method which is used in '==' and '!=' operators---");
            Console.WriteLine("The clone of the second tree is equal to the second tree? " + secondTreeClone.Equals(secondTree));
        }
开发者ID:vladislav-karamfilov,项目名称:TelerikAcademy,代码行数:79,代码来源:BinarySearchTreeDemo.cs


注:本文中的BinarySearchTree.GetHashCode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。