【发布时间】:2013-07-30 14:59:37
【问题描述】:
所以,过去一个月我一直在学习 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; }
}
}
还有其他调用我的树的方法吗?还是我做错了什么?
【问题讨论】:
-
只是为了澄清下面的答案,您正在转换一个
char类型,它是通过使用带撇号的文字'6'创建的。char类型被隐式转换为等效的int值,其中整数值“54”表示字符6。见msdn.microsoft.com/en-us/library/x9h8tsay%28v=vs.110%29.aspx
标签: c# binary-tree