Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为二进制搜索树中存储的字符串编写查找前缀和查找单词方法?_Java_Binary Search Tree_Root_Contains_Prefix - Fatal编程技术网

Java 如何为二进制搜索树中存储的字符串编写查找前缀和查找单词方法?

Java 如何为二进制搜索树中存储的字符串编写查找前缀和查找单词方法?,java,binary-search-tree,root,contains,prefix,Java,Binary Search Tree,Root,Contains,Prefix,我有一个二叉搜索树,其中单词作为节点中的数据存储在中。我正在尝试编写一个findWord方法,如果BST中存在给定的单词,该方法将返回true/false。如果BST中至少存在一个具有给定前缀的单词,则我的findPrefix方法应返回true/false 现在我有这个 public boolean findWord ( String word) { return contains(word, root); } public boolean findPrefix (String pr

我有一个二叉搜索树,其中单词作为节点中的数据存储在中。我正在尝试编写一个findWord方法,如果BST中存在给定的单词,该方法将返回true/false。如果BST中至少存在一个具有给定前缀的单词,则我的findPrefix方法应返回true/false

现在我有这个

public boolean findWord ( String word) {
    return contains(word, root);

}

public boolean findPrefix (String prefix ) {

    return contains(prefix, root);

}
我的方法是

protected boolean contains(E item, BSTNode<E> tree) {
    if(tree==null)
        return false;
    else if(item.compareTo(tree.getInfo())<0)
        return contains(item, tree.getLeft());
    else if(item.compareTo(tree.getInfo())>=0)
        return contains(item, tree.getRight());
    else
        return true;
}
受保护的布尔包含(E项,节点树){
if(tree==null)
返回false;
如果(item.compareTo(tree.getInfo())=0,则为else
return包含(item,tree.getRight());
其他的
返回true;
}

我很确定我应该为findWord和findPrefix方法编写更多的代码,除非我的contains方法是错误的。谢谢

考虑到您的树和其他实现是正确的,以下更改将帮助您找到与BST中任何节点匹配的前缀或单词(if):

    // root passed from method call for a node
    public boolean findWord (String word,String root) {
        return root.equals(word);
    }

    public boolean findPrefix (String prefix,String root) {
        return root.contains(prefix);
    }

    protected boolean contains(E item, BSTNode<E> tree) {
        String lookingFor = "searchTHISword";
        if(tree==null)
            return false;
        if(findWord(lookingFor,tree.getInfo())) {
            System.out.println("Found the entire word here.");
            return true;
        }
        if(findPrefix(lookingFor,tree.getInfo())){
            System.out.println("Found the word as a prefix here.");
            return true;
        }
        if(item.compareTo(tree.getInfo())<0)
            return contains(item, tree.getLeft());
        else
            return contains(item, tree.getRight());
    }
    // Would return false, it what you are looking for is nowhere in the BST
//从节点的方法调用传递的根
公共布尔findWord(字符串字、字符串根){
返回root.equals(word);
}
公共布尔findPrefix(字符串前缀,字符串根){
返回root.contains(前缀);
}
受保护的布尔包含(E项、节点树){
字符串lookingFor=“searchTHISword”;
if(tree==null)
返回false;
if(findWord(lookingFor,tree.getInfo()){
System.out.println(“在这里找到了整个单词。”);
返回true;
}
if(findPrefix(lookingFor,tree.getInfo()){
System.out.println(“在这里找到作为前缀的单词。”);
返回true;
}

如果(item.compareTo(tree.getInfo())考虑到您的树和其他实现是正确的,以下更改将帮助您找到与BST中任何节点匹配的前缀或单词(if):

    // root passed from method call for a node
    public boolean findWord (String word,String root) {
        return root.equals(word);
    }

    public boolean findPrefix (String prefix,String root) {
        return root.contains(prefix);
    }

    protected boolean contains(E item, BSTNode<E> tree) {
        String lookingFor = "searchTHISword";
        if(tree==null)
            return false;
        if(findWord(lookingFor,tree.getInfo())) {
            System.out.println("Found the entire word here.");
            return true;
        }
        if(findPrefix(lookingFor,tree.getInfo())){
            System.out.println("Found the word as a prefix here.");
            return true;
        }
        if(item.compareTo(tree.getInfo())<0)
            return contains(item, tree.getLeft());
        else
            return contains(item, tree.getRight());
    }
    // Would return false, it what you are looking for is nowhere in the BST
//从节点的方法调用传递的根
公共布尔findWord(字符串字、字符串根){
返回root.equals(word);
}
公共布尔findPrefix(字符串前缀,字符串根){
返回root.contains(前缀);
}
受保护的布尔包含(E项、节点树){
字符串lookingFor=“searchTHISword”;
if(tree==null)
返回false;
if(findWord(lookingFor,tree.getInfo()){
System.out.println(“在这里找到了整个单词。”);
返回true;
}
if(findPrefix(lookingFor,tree.getInfo()){
System.out.println(“在这里找到作为前缀的单词。”);
返回true;
}

if(item.compareTo(tree.getInfo())在进行任何讨论之前,您应该将
=
替换为
中的
contains
方法,否则它永远不会到达
return true
语句树的原型请将其更改为>,但仍然不会返回true。我正在使用一个数组列表,其中包含了诸如…act、add、cat、dad、dog、god、home、tac等词,用于存储进入bst的节点。@noOneSpecial:protected boolean包含(E项…这是什么E项。您的树原型将有助于提供完整的解决方案在进行任何讨论之前,您应该将
=
替换为
包含
方法中的
,否则它永远不会到达
返回true
语句树原型请将其更改为>,但仍然不会返回true。我正在使用一个包含单词的数组列表,如…act、add、cat、dad、dog、god、home、tac,这些单词存储在bst的节点中。@noOneSpecial:protected boolean contains(E item…这是什么E…。您的树原型将有助于提供完整的解决方案