繁体   English   中英

如何在 AVL 树中查找特定值并返回 Node

[英]How to find a specific value in the AVL tree and return the Node

public Node search_data_var2(Comparable searchable, Node T){
        if(T.getInfo()==searchable){
            return(T);
        }
        else{
            if(T.getInfo()==null){
                return null;
            }
            if(T.getInfo().compareTo(searchable)>0){
                search_data_var2(searchable,T.getLeft());
            }
            if(T.getInfo().compareTo(searchable)<0){
                search_data_var2(searchable,T.getRight());
            }
        }
}

我需要创建一个方法来查找具有特定值“可搜索”的节点并在它包含节点时返回节点“T”。 如果不存在这样的值,则 function 应返回“null”。 但是我遇到了麻烦,不知道如何用一种方法来实现这一点。 上面的function就是我写的。 问题是该方法不能以相同的方式返回Node和null。

不禁止使用外部 function 来实现这一点,但目前我不知道如何实现这一点。

问题是该方法不能以相同的方式返回Node和null。

是的,你可以,但你需要调整你的代码以适应

public Node search_data_var2(Comparable searchable, Node T){
       ....
            if(T.getInfo().compareTo(searchable) > 0){
                return search_data_var2(searchable,T.getLeft()); // <-- add return
            }
            if(T.getInfo().compareTo(searchable) < 0){
                return search_data_var2(searchable,T.getRight()); // <-- add return
            }
       ...
    }

返回将由search_data_var2递归方法调用返回的值。 顺便说一句,您不应该使用T作为变量名,通常此类名称(单个大写字母)用于泛型类型。 此外,您应该使用compareTo方法而不是==T.getInfo().compareTo(searchable) == 0 )。

最后,您的代码可能会抛出NPE ,因为您首先要检查

if(T.getInfo()==searchable) 

在实际检查T.getInfo()是否为nullT是否为null 因此,您需要重新排列条件,如下所示:

public Node search_data_var2(Comparable searchable, Node node){
        if(node == null || node.getInfo() == null){
            return null;
        }
        else{
            if(node.getInfo().compareTo(searchable) == 0){
                return node;
            }
            else if(node.getInfo().compareTo(searchable) > 0 ){
                return search_data_var2(searchable, node.getLeft());
            }
            else{
                return search_data_var2(searchable, node.getRight());
            }
        }
    }

出于查找目的,AVL 树与普通的二叉搜索树相同。

您的代码几乎就在那里! 大多数情况下,您只需要在递归调用之前添加return关键字。

以下是我还将进行的其他一些更改:

  1. 按照 Java 中的约定, T用于泛型类型。 我会将节点重命名为更具描述性的名称(例如node )。
  2. 我不会检查引用相等( == ),而是使用compareTo方法检查值相等,因为无论如何您都在使用compareTo 这使您无需参考即可找到所需的值。
  3. 我会将 null 检查移到方法的顶部,以避免出现NullPointerException的任何可能性。
public Node search_data_var2(Comparable searchable, Node node) {
    // If node is null, we've run off the end of the tree
    // Therefore, the value is not contained in the tree - return null
    if (node == null || node.getInfo() == null) {
        return null;
    }

    if (node.getInfo().compareTo(searchable) == 0) {
        // This node has info equal to the search condition - return it!
        return node;
    } else if (node.getInfo().compareTo(searchable) > 0) {
        // The sought value must be in the left subtree - start the search again there
        return search_data_var2(searchable, node.getLeft());
    } else if (node.getInfo().compareTo(searchable) < 0) {
        // The sought value must be in the right subtree - start the search again there
        return search_data_var2(searchable, node.getRight());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM