7

So, I have been learning C# over the past month and at the moment I am struggling with Binary Trees.

My question is that How am I able to call my tree to the Console Window? I've Tried Console.WriteLine(tree.Data); But this seems to Write 54 to my Console Window.

Here is my code if you need to check it out:

Main File

static void Main(string[] args)
{
    //Creating the Nodes for the Tree
    Node<int> tree = new Node<int>('6');
    tree.Left = new Node<int>('2');
    tree.Right = new Node<int>('5');  

    Console.WriteLine("Binary Tree Display");
    Console.WriteLine(tree.Data);
    Console.ReadLine();
}

Node Class

class Node<T> where T : IComparable
{
    private T data;
    public Node<T> Left, Right;

    public Node(T item)
    {
        data = item;
        Left = null;
        Right = null;
    }
    public T Data
    {
        set { data = value; }
        get { return data; }
    }
}

Are there any other methods of calling my Tree? or am I doing something wrong?

1
  • Just to clarify the answers below, you are converting a char type which you created by using the literal '6' with apostrophes. The char type is being implicitly converted to the equivalent int value, whereby integer value "54" represents the character 6. See msdn.microsoft.com/en-us/library/x9h8tsay%28v=vs.110%29.aspx Commented Jul 30, 2013 at 15:06

4 Answers 4

8

The reason why it's just showing 54 is because that is the what (int)'6' is!

You're calling tree.Data which returns in this case '6' cast to int.


I imagine what you're trying to do is either return 6 which you could do by using

new Node<char>('6'); 

or by

new Node<int>(6);

(More in separate answer, removed for clarity)

Sign up to request clarification or add additional context in comments.

2 Comments

[to be continued..] sounds better :)
@lazyberezovsky: Alternatively... even more thrilling!
2
Node<int> tree = new Node<int>(6);

6, and not '6'. Now expected value will be printed. Your code is silently casts char value '6' to integer, which gives result 54.

Comments

2

(Moved from previous answer for clarity)

If you're trying to return all the data for your Node<T> I think a better way of going about it would be to override the ToString method in your Node<T> class like so:

public override string ToString()
{
    var leftString = this.Left != null ? this.Left.ToString() : "null";
    var rightString = this.Right != null ? this.Right.ToString() : "null";
    var dataString = this.Data != null ? this.Data.ToString() : "null";

    leftString = String.Join("\n", leftString.Split('\n').Select(a => "\t" + a));
    rightString = String.Join("\n", rightString.Split('\n').Select(a => "\t" + a));

    return String.Format("\nData: {0}\n"
                        + "Left: {1}\n"
                        + "Right: {2}",
                        dataString, leftString, rightString);
}

Then call Console.WriteLine(tree.ToString()); which results in the following:

Data: 54
Left:   
  Data: 50
  Left:   null
  Right:   null
Right:   
  Data: 53
  Left:   null
  Right:   null

This isn't the prettiest implementation but I think proves the point.

For a prettier implementation see this answer

Comments

0

I believe the best way to do this would be to implement a concise recursive tree traversal algorithm which prints out the value of each node in the particular order you choose to encounter them. As for there being a pre-written method to do so within the C# libraries, I am not aware of it. Best of luck!

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.