所以,我在过去的一个月里一直在学习C#,目前我正在苦恼于二叉树。
我的问题是,我该如何将我的树呈现在控制台窗口中?
我尝试了Console.WriteLine(tree.Data);
,但这似乎会向我的控制台窗口写入54。
如果需要检查,以下是我的代码:
主文件
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();
}
节点类
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; }
}
}
还有其他调用我的Tree的方法吗?还是我做错了什么?
char
类型进行转换。char
类型被隐式转换为等效的int
值,其中整数值“54”表示字符6
。请参见http://msdn.microsoft.com/en-us/library/x9h8tsay%28v=vs.110%29.aspx。 - Chris Sinclair