在二叉树中搜索特定单词

问题描述 投票:-2回答:1

我需要在二叉树中找到给定的单词。我有这个代码

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;
java binary-search-tree
1个回答
0
投票

我将通过修复一些主要问题并添加一个案例来启动你,如果你在一片叶子但仍然没有找到这个词,它返回null。

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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.