簡體   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