3

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. Is there a way of displaying it without making too much extra methods?

5
  • a Data class was not provided. Do I have to make one? Commented Sep 27, 2016 at 6:07
  • Just loop all of your TreeNodes and print the Data property in which you store your values. Commented Sep 27, 2016 at 6:07
  • @Angela - See, you got the answer for now. But, to understand them, please prepare your own code for how to print the nodes of a binary-search-tree. Commented Sep 27, 2016 at 6:09
  • @Angela - Also, you didn't show the ToString() of BinSearchTree<T>, instead you showed the ToString() of OurTreeNode<T>! Commented Sep 27, 2016 at 6:11
  • You need to show us what you “tried adding” and what error it gave you. Commented Sep 28, 2016 at 1:15

3 Answers 3

1

The class is using the default (Object's) ToString() implementation. You have 2 options:

  • walk though the elements of the tree and print it yourself
  • ask the author to implement/override the ToString() method
Sign up to request clarification or add additional context in comments.

Comments

0

Can somebody tell me why it displays that?

It displays that because ToString() prints the type definition.

Default implementations of the Object.ToString method return the fully qualified name of the object's type. (from the docs)

For instance, the following short program prints System.Collections.Generic.List`1[System.Int32], which is the type of 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.

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,

3 Comments

is there any way I can fix it?
@Angela Yes. You need to override ToString(). See edits.
I edited the code above. my prof already has an insert method on his code and I tried to add a ToString method but foreach gives an error
0

As you have created the object of BinaryTree Class and have not overridden the ToString() method inside BinaryTree Class. You have not created object of OurTreeNode class and not calling ToString() method overriden inside it. Hence it is giving you the default ToString() method output of BinaryTree Class.

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

You are calling

Console.WriteLine(myTree.ToString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.