【问题标题】:C# binary search treeC# 二叉搜索树
【发布时间】:2016-09-27 05:43:52
【问题描述】:

我正在为教授给出的二叉搜索树上的一些代码做一个测试用例

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();
}

它编译,但它显示

BinSearchTree`1[System.Int32]

谁能告诉我为什么会这样显示?

我的教授的代码:

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;
}
}

我尝试在 Insert 方法之后添加一个 ToString() 方法,但是当我使用 foreach 时它给了我一个错误。有没有一种无需过多额外方法即可显示它的方法?

【问题讨论】:

  • 未提供数据类。我必须做一个吗?
  • 只需循环所有 TreeNode 并打印存储值的 Data 属性。
  • @Angela - 看,你现在得到了答案。但是,为了理解它们,请准备自己的代码来打印二叉搜索树的节点。
  • @Angela - 另外,您没有显示 BinSearchTree 的 ToString(),而是显示了 OurTreeNode 的 ToString()!
  • 您需要向我们展示您“尝试添加”的内容以及它给您带来的错误。

标签: c# binary-search-tree


【解决方案1】:

该类使用默认的(对象的)ToString() 实现。您有 2 个选择:

  • 遍历树的元素并自己打印
  • 请作者实现/重写 ToString() 方法

【讨论】:

    【解决方案2】:

    谁能告诉我为什么会这样显示?

    显示是因为ToString() prints the type definition.

    Object.ToString 方法的默认实现返回对象类型的完全限定名称。 (来自文档)

    例如,下面的短程序打印System.Collections.Generic.List`1[System.Int32],这是List&lt;int&gt;的类型。

    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() 方法产生一些有意义的输出。

    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;
        }
    }
    

    输出

    10, 15,
    

    【讨论】:

    • 有什么办法可以解决吗?
    • @Angela 是的。您需要覆盖ToString()。查看修改。
    • 我编辑了上面的代码。我的教授已经在他的代码上有一个插入方法,我试图添加一个 ToString 方法,但是 foreach 给出了一个错误
    【解决方案3】:

    因为您已经创建了 BinaryTree 类的对象并且没有覆盖 BinaryTree 类中的 ToString() 方法。您还没有创建 OurTreeNode 类的对象,也没有调用其中覆盖的 ToString() 方法。因此,它为您提供 BinaryTree 类的默认 ToString() 方法输出。

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

    你在打电话

    Console.WriteLine(myTree.ToString());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 2011-01-31
      • 2013-05-03
      相关资源
      最近更新 更多