java—在对象的通用bst中搜索具有给定对象的匹配值的对象

wsewodh2  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(232)

我有一个泛型binarysearchtree类,它接收泛型项类型并扩展可比较项。我构建了一个包含10个字符串的对象,这些字符串定义在一个单独的类中,该类还扩展了可比较的。
我希望能够在树中搜索第8个字符串与给定字符串匹配的对象。
到目前为止,我已经创建了一个临时对象,其中包含所有值的空字符串,除了从文本字段读入的第8个值。
我传递虚拟对象以供bst的搜索方法用于查找匹配的对象,但得到一个nullpointer异常。
以下是搜索方法:

// --------------------------------------------------------------
// Method searches for existence of target in tree.  If matching 
// (i.e. compareTo method returns zero) object from list is returned.
public T retrieve(T target)
{
    // Call the private recursive method.
    return retrieve(target, root);
}

// The method contains checks whether an item is in a binary search tree.
// Param searchItem The item to check for.
// Param bstree The binary tree to look in.
// Return node value if found; null otherwise.
private T retrieve(T searchItem, Node bstree) 
{
    if (bstree == null) 
        return null;

    if (searchItem.compareTo(bstree.value) == 0)
        return bstree.value;
    if (searchItem.compareTo(bstree.value) < 0)
        // Recursively look in left subtree.
        return retrieve(searchItem, bstree.left);
    else 
        // Recursively look in right subtree.
        return retrieve(searchItem, bstree.right);
}

下面是我在patient类中编写的compareto方法(email是patient的第8个槽中的字符串):

public int compareTo(Patient p) {
      int returnValue = email.compareTo(p.email);
      return returnValue;
   }

以下是驱动程序中调用它的方法:

searchBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {   

        Patient tempPatient = new Patient("","","","","","","",emailField.getText(),"","");
        currentPatient = patientSearchTree.retrieve(tempPatient);
     }
});

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题