【发布时间】:2019-08-30 03:15:47
【问题描述】:
我开始以 c/c++ 的背景学习 C#。我正在创建一个简单的 BST,但我的插入功能不起作用。任何帮助将不胜感激。
当我没有在 c/c++ 中通过引用传递时,我得到了这种错误。既然我创建了两个类Node和BST,它们不应该通过引用传递吗?我已经在这个问题上工作了几个小时,并试图更改我的代码,但没有成功。
public Node(int data)
{
this.data = data;
this.right = null;
this.left = null;
}
public Node Left
{
get { return left; }
set { left = value; }
}
public Node Right
{
get { return right; }
set { right = value; }
}
public int Data
{
get { return data; }
set { data = value; }
}
}
class BST
{
private Node root;
public BST()
{
root = null;
}
public Node Root
{
get { return root; }
set { root = value; }
}
public void Insert(int data)
{
if (root == null)
{
root = new Node(data);
}
else
{
InsertHelper(root, data);
}
}
public void InsertHelper( Node root, int data)
{
if (root == null)
{
root = new Node(data);
//return root;
}
if (root.Data > data)
{
InsertHelper(root.Left, data);
}
if (root.Data < data)
{
InsertHelper(root.Right, data);
}
}
【问题讨论】:
-
C#与C/C++如此不同,就你的问题而言:看看你的InsertHelper- 它实际上从来没有 insert 数据(只是没有代码插入)
标签: c# binary-search-tree nodes