简体   繁体   English

C#二叉搜索树

[英]C# binary search tree

I was making a test case for some code on binary search tree my professor gave 我正在为我教授给出的二叉搜索树上的一些代码做一个测试用例

public static void Main(string [] args)
{
    //on my prof's code, public class BinSearchTree<T>
    BinSearchTree<int> myTree = new BinSearchTree<int>();

    myTree.Insert(10);
    myTree.Insert(15);
    myTree.Insert(5);
    myTree.Insert(2);
    myTree.Insert(1);

    Console.WriteLine(myTree.ToString());
    Console.ReadKey();
}

It compiles, but it displays 它编译,但它显示

BinSearchTree`1[System.Int32]

Can somebody tell me why it displays that? 有人能告诉我为什么会显示出来吗?

my prof's code: 我教授的代码:

public class BinSearchTree<T> where T : IComparable<T>
{
private class OurTreeNode<T>
{
    public T Data { get; set; }
    public OurTreeNode<T> Left;
    public OurTreeNode<T> Right;
    public OurTreeNode(T d = default(T), OurTreeNode<T> leftnode = null, OurTreeNode<T> rightnode = null)
    {
        Data = d;
        Left = leftnode;
        Right = rightnode;
    }

    public override string ToString()
    {
        return Data.ToString();
    }
}
//...other methods

//prof's Insert method
public void Insert(T newItem)
{
    mRoot = Insert(newItem, mRoot);
}
private OurTreeNode<T> Insert(T newItem, OurTreeNode<T> pTmp)
{
    if (pTmp == null)
        return new OurTreeNode<T>(newItem, null, null);
    else if (newItem.CompareTo(pTmp.Data) < 0)
        pTmp.Left = Insert(newItem, pTmp.Left);
    else if (newItem.CompareTo(pTmp.Data) > 0)
        pTmp.Right = Insert(newItem, pTmp.Right);
    else
        throw new ApplicationException("...");

    return pTmp;
}
}

I tried adding a ToString() method after the Insert method but it gives me an error when I used foreach. 我尝试在Insert方法之后添加ToString()方法,但是当我使用foreach时它给了我一个错误。 Is there a way of displaying it without making too much extra methods? 有没有办法显示它而不需要太多的额外方法?

The class is using the default (Object's) ToString() implementation. 该类使用默认(Object)的ToString()实现。 You have 2 options: 你有2个选择:

  • walk though the elements of the tree and print it yourself 走过树的元素并自己打印
  • ask the author to implement/override the ToString() method 要求作者实现/覆盖ToString()方法

Can somebody tell me why it displays that? 有人能告诉我为什么会显示出来吗?

It displays that because ToString() prints the type definition. 它显示因为ToString()打印了类型定义。

Default implementations of the Object.ToString method return the fully qualified name of the object's type. Object.ToString方法的默认实现返回对象类型的完全限定名称。 (from the docs) (来自文档)

For instance, the following short program prints System.Collections.Generic.List`1[System.Int32] , which is the type of List<int> . 例如,以下短程序打印System.Collections.Generic.List`1[System.Int32] ,它是List<int>的类型。

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        List<int> myTree = new List<int>();
        myTree.Add(10);
        Console.WriteLine(myTree.ToString());
    }
}

Here are the rudiments of how to override the ToString() method to produce some meaningful output. 以下是如何覆盖ToString()方法以产生一些有意义的输出的基础知识。

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        BinSearchTree<int> myTree = new BinSearchTree<int>();
        myTree.Insert(10);
        myTree.Insert(15);
        Console.WriteLine(myTree.ToString());
    }
}

public class BinSearchTree<T> where T : IComparable<T>
{
    private List<T> values = new List<T>();

    // rest of class omitted for clarity

    public void Insert(T val) {
        values.Add(val);
    }

    public override string ToString() {
        var result = string.Empty;
        foreach(var v in values)
        {
            result += v + ", ";
        }

        return result;
    }
}

Output 产量

10, 15,

As you have created the object of BinaryTree Class and have not overridden the ToString() method inside BinaryTree Class. 由于您已经创建了BinaryTree类的对象,并且没有覆盖BinaryTree类中的ToString()方法。 You have not created object of OurTreeNode class and not calling ToString() method overriden inside it. 您还没有创建OurTreeNode类的对象,也没有在其中调用ToString()方法覆盖。 Hence it is giving you the default ToString() method output of BinaryTree Class. 因此它为您提供了BinaryTree类的默认ToString()方法输出。

BinSearchTree<int> myTree = new BinSearchTree<int>();

You are calling 你在打电话

Console.WriteLine(myTree.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM