c# – 简单二叉树

所以,过去一个月我一直在学习C#,而目前我正在与Binary Trees进行斗争.

我的问题是,如何将我的树调用到控制台窗口?
我试过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; }
    }
}

有没有其他方法可以调用我的树?或者我做错了什么?

解决方法

它之所以显示54是因为那就是(int)’6’是什么!

你正在调用tree.Data,在这种情况下返回’6’强制转换为int.

我想你要做的就是返回6,你可以通过使用来做

new Node<char>('6');

或者

new Node<int>(6);

(More in separate answer,removed for clarity)

相关文章

C#项目进行IIS部署过程中报错及其一般解决方案_c#iis执行语句...
微信扫码登录PC端网站应用的案例(C#)_c# 微信扫码登录
原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...