我一直在尝试使用Java创建一个integer binary search tree
,出于某种原因,我一直在向树中添加新节点时出错。未添加到树中的节点
这里是NODE
类。
class NODE
{
NODE left = null, right = null;
int info;
public NODE(int x)
{
info = x;
}
}
,这里是与insert()
方法BST
(二进制Seatch树)类。
class BST
{
NODE tree = null;
public void insert(int x)
{
NODE node = new NODE(x);
NODE temp = tree;
while(true)
{
if(temp == null)
{
temp = node;
break;
}
else if(temp.info > x) temp = temp.left;
else temp = temp.right;
}
}
//other methods present here
}
由于我无法弄清楚的原因,insert()
方法出错了。
即使在调用insert()
方法后,对象tree
也会携带null
。
你能在代码中发现点点滴滴吗?
谢谢!
您想命名以大写字母开头但不完全在大写字母中的类 - 保存最终变量的全部大写值。 –
@ La-comadreja我会牢记这一点。谢谢。 –