當前位置: 首頁>>代碼示例>>C#>>正文


C# BinarySearchTree.ToString方法代碼示例

本文整理匯總了C#中BinarySearchTree.ToString方法的典型用法代碼示例。如果您正苦於以下問題:C# BinarySearchTree.ToString方法的具體用法?C# BinarySearchTree.ToString怎麽用?C# BinarySearchTree.ToString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在BinarySearchTree的用法示例。


在下文中一共展示了BinarySearchTree.ToString方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: 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());
        }
開發者ID:AYankova,項目名稱:CSharp,代碼行數:27,代碼來源:Program.cs

示例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());
        }
開發者ID:Rokata,項目名稱:TelerikAcademy,代碼行數:29,代碼來源:TreeDataStructure.cs

示例3: 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);
        }
    }
開發者ID:vasilkrvasilev,項目名稱:ObjectOrientedProgramming,代碼行數:73,代碼來源:Start.cs

示例4: TestBinarySearchTreeToString

        public void TestBinarySearchTreeToString()
        {
            BinarySearchTree<int> tree = new BinarySearchTree<int>(1, 9, 7);

            Assert.AreEqual("1, 7, 9", tree.ToString());
        }
開發者ID:nikolaynikolov,項目名稱:Telerik,代碼行數:6,代碼來源:BinarySearchTreeTest.cs


注:本文中的BinarySearchTree.ToString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。