gpt4 book ai didi

c# - 简单二叉树

转载 作者:太空狗 更新时间:2023-10-29 20:05:13 26 4
gpt4 key购买 nike

所以,在过去的一个月里,我一直在学习 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; }
}
}

还有其他方法可以调用我的树吗?还是我做错了什么?

最佳答案

之所以只显示 54 是因为那是 (int)'6' 的意思!

您正在调用 tree.Data,在这种情况下返回 '6' 转换为 int


我想你想做的是返回 6 ,你可以使用

new Node<char>('6'); 

或通过

new Node<int>(6);

( More in separate answer, removed for clarity )

关于c# - 简单二叉树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17950546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com