我需要在二叉树中找到给定的单词。我有这个代码
if (n !=null)
{
if((name.compareTo(n.getVehicleName()) < 0) && n.left())
{
return find(name.hasLeft, n);
}
if((name.compareTo(n.getVehicleName()) > 0) && n.hasRight())
{
return find(name.right, n);
}
if(name.compareTo(n.getVehicleName()) == 0)
{
return Vehicle;
}
}
我不工作,我不知道如何解决它。
我得到的唯一帮助是:
* @param name The name of the vehicle to search for
* @param n The current node in the tree to search from
* @return A reference to the node that was found or null if not found
int order = name.compareTo(n.getVehicleName());
if(n==null) return null
return null;
答案 0 :(得分:0)
我会通过修复一些重大问题并添加一个案例来启动你,如果你还没有找到这个词,那么它仍会找不到它。
if (n != null)
{
if(name.compareTo(n.getVehicleName()) < 0))
{
if(name.hasLeft()){
return find(name.left, n);
}else{
return null
}
}
else if((name.compareTo(n.getVehicleName()) > 0))
{
if(name.hasRight()){
return find(name.right, n);
}else{
return null
}
}
else if(name.compareTo(n.getVehicleName()) == 0)
{
return Vehicle;
}
}