【问题标题】:Finding a Word in a Binary Search Tree Java?在二叉搜索树 Java 中查找单词?
【发布时间】:2013-04-12 01:03:49
【问题描述】:

我遇到的问题是我有一个二分搜索三,并且所有节点都包含字符串值而不是整数值。它从 txt 文件中获取这些字符串,并将文件中的各个文本行作为节点放入树中。这没有问题。

我的问题是我需要一种方法来遍历我的树并找到一个特定的单词。我已经有单独的类 BinarySearchTree 和 BinaryTreeNode 作为我正在尝试制作的树的基础。它需要查找的单词位于一个名为“lookup test file copy.txt”的文件中,它需要将找到的单词写入另一个名为“SearchResults.txt”的文件中

我只是不知道该怎么做,所以我正在寻找建议。

这是我需要帮助的方法:

public boolean SearchWord(String target){
    //returns true if the target string exists in the dictionary
    // otherwise it returns false
    //ALSO you need to write the results of Search 
    //in an output file called "SearchResults.txt" 

    return false;
}

这是我的所有代码,不包括上面提到的其他两个类,如果有帮助的话。

public class Dictionary {
    public BinaryTreeNode theBinaryTree; 
    /**
     * I need to read one string by one string
     * and then insert it into a tree. 
     * I need to read all of the strings in the source file, line by line, 
     * and insert them into my dictionary. 
     * After readinga  single string, it needs to put it into a tree. 
     * I first need to create the dictionary tree, 
     * and then implement these methods on the tree. 
     * The Dictionary type is string. 
     * @throws FileNotFoundException 
     */

    private BinaryTreeNode dictionaryTree;   // this is the tree within your dictionary class

       public Dictionary(String filePath) throws IOException{
             BufferedReader br = new BufferedReader(new FileReader("Dictionary.txt"));
             this.dictionaryTree = readFile(br);
             br.close();
       }


       public BinaryTreeNode readFile(BufferedReader reader) throws IOException{
             String word = reader.readLine();         
             if(word!=null){
                 return new BinaryTreeNode(word, readFile(reader), readFile(reader));            
             }else{
                 return new BinaryTreeNode() ;  // empty node or null?
             }      
         }


    /**
     * @return
     * Once again, I already have this method written and modified
     * within the BinarySearchTree class. 
     * I'm simply going to call it over here. 
     */
    public int CountWords(){
        //returns the number of words in the dictionary
        //This is just counting nodes. 

        BinarySearchTree Aria = new BinarySearchTree();
        return Aria.countNodes(dictionaryTree);     
    }

    public boolean SearchWord(String target){
        //returns true if the target string exists in the dictionary
        // otherwise it returns false
        //ALSO you need to write the results of Search 
        //in an output file called "SearchResults.txt" 

        return false;
    }

    /**
     * I already modified the print order method
     * in BinarySearchTree
     * to work with strings. 
     * So I just called it here on the dictionaryTree. 
     * @PrintOrderedDict()
     * 
     * However, I also had to modify the method, 
     * so that it wrote whatever values the method recieved
     * to teh output file. 
     */

    public void PrintOrderedDict() throws IOException{
        //Print the dictionary words 
        //in order in a new file called "OrderedDictionary.txt". 
        //Just modify print order to work with strings. 
        try {
        BinarySearchTree jojo = new BinarySearchTree();
        FileWriter fstream = new FileWriter("OrderedDictionary.txt");

        BufferedWriter out = new BufferedWriter(fstream);
        out.write(jojo.inorderPrint(dictionaryTree));

        out.close();}
        catch (Exception e) {
            System.err.println("Error");
        }

    }

    public boolean DeleteWord(String target){
        //delete the target word if it exits in the dictionary and return true
        //otherwise return false
        return false;
    }
}

任何帮助或建议将不胜感激。

----编辑----

这也是“dictionary.txt”文件的一个小样本(放全篇太长了)

ourselves
out
over
own
same
shan't
she 
all

这是“查找测试文件copy.txt”文件:

the
program
a
ours
house
ME
ours
main
java
whom
with

【问题讨论】:

  • 如果我没记错的话,这是一个简单的例子,“如果您要搜索的单词按字母顺序低于光标所在的单词,则向左移动,否则向右移动。 "递归执行此操作,直到找到单词并返回 true,或者根本找不到单词并返回 false。
  • 试着用铅笔和纸做,看看效果如何。

标签: java string search tree binary-search-tree


【解决方案1】:

您还没有包含最相关的代码,即 BinaryTreeNode,因此此处使用的字段名称是猜测,但可以这样做:

字典中的方法:

public boolean SearchWord(String target){
    boolean found = theBinaryTree.contains(word);
    // write the values of "target" and "found" to file (code omitted)
    return found;
}

BinaryTreeNode 中的方法:

private String word;
private BinaryTreeNode left;
private BinaryTreeNode right;

public boolean contains(String target) {
    if (target.equals(word))
        return true;
    BinaryTreeNode next = target.compareTo(word) < 0 ? left : right;
    return next != null && next.contains(target);
}

【讨论】:

  • +1 但我希望 OP 能够理解三元运算符的使用:)
  • 这很有帮助,但我不断收到在 Dictionary 方法中使用“word”的错误消息。
  • @MariaStewart 你读过波希米亚说“这里使用的字段名称是猜测”的部分吗?
【解决方案2】:

这显然是一个家庭作业,所以我不会从你那里窃取解决它的可能性,但我会给你一些提示,你可以用来轻松解决你的问题:

  1. 如果我理解正确,你已经知道算法,所以你知道你与数字有什么关系。你需要对字符串做同样的事情,只是你不知道如何比较字符串。

  2. 使用compareTo 方法。 s1.compareTo(s2) 是:

    • 阳性,如果 s1 > s2

    • 负数,如果 s1

    • 0,如果 s1.equals(s2)

  3. Comparable 是一个接口。如果一个类实现了 Comparable,它将有一个 compareTo 方法。 String恰好实现了Comparable,如你所见here

【讨论】:

    【解决方案3】:

    将问题分解为多个部分。

    1) 搜索如何读取文件。读取文件,并将结果回显到标准输出。这很容易,大约需要您完成 1/3 的工作。

    2) 将一些随机单词写入文件。打开文件,写下单词,然后检查你的工作,也不难做到,离你越来越近了。

    3) 加载您的二叉搜索树并编写代码进行查找,这很容易。如果您的单词 .equals 当前节点,则返回 true 否则如果小于当前节点,则向左走,如果更大则向右走。如果您的指针为空,则返回 false;

    4) 把它们放在一起。

    部分地克服这一点并不那么令人难以抗拒。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多