当前位置: 首页>>代码示例>>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;未经允许,请勿转载。