Angelrawzz Angelrawzz - 10 months ago 98
C# Question

c# - Simple Binary Tree

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

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");

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?

Answer Source

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)