2013-04-12 21 views
-2

我遇到的問題是我有一個二進制搜索三,所有節點包含字符串值而不是整數。它從txt文件中獲取這些字符串,並將文件中的各行文本作爲節點放入樹中。這沒有問題。在二叉搜索樹Java中查找單詞?

我的問題是,我需要一種方法,遍歷我的樹,並找到一個特定的單詞。我已經有單獨的類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 
+3

如果我沒有弄錯,它就是一個簡單的例子:「如果您搜索的詞按字母順序低於光標所在的那一個,然後向左移動,否則向右移動。」直到找到該單詞並返回true,或者根本沒有找到該單詞並返回false,請遞歸執行此操作。 –

+0

嘗試用鉛筆和紙做的,看看它是如何工作的。 –

回答

2

您還沒有包括最相關的代碼,這是BinaryTreeNode,所以這裏使用的域名是猜測,但是這將做到這一點:

法詞典:在BinaryTreeNode

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

方法:

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); 
} 
+0

+1,但我希望OP會理解三元運算符的使用:) –

+0

這是非常有用的,但我一直在使用字典方法中的「單詞」使用錯誤消息。 –

+1

@MariaStewart您是否閱讀過波希米亞所說的「這裏使用的字段名稱是猜測」的部分? –

1

這顯然是一個家庭作業,所以我不會偷你解決它的可能性,但我會給你提示你可以用它來輕鬆解決你的問題:

  1. 你已經知道算法,如果我理解正確,那麼你k現在你需要做什麼與數字。你需要對Strings做同樣的事情,只是你不知道如何比較Strings。使用compareTo方法。 s1.compareTo(s2)是:

    • 正,如果S1> S2

    • 負,如果s1 < S2

    • 0,如果s1.equals(S2)

  2. 可比是一個接口。如果一個類實現了Comparable,它將有一個compareTo方法。字符串恰好實現了Comparable,如您所見here

0

將問題分解成部分。

1)搜索如何讀取文件。讀取文件,並將結果回顯到標準輸出。這很容易,大約需要做1/3的工作。

2)將一些隨機單詞寫入文件。打開文件,寫下文字,然後檢查你的工作,也不難做,你越來越近。

3)加載你的二叉搜索樹並編寫代碼來查找,這很容易。如果你的單詞等於當前節點,則返回true;否則,如果小於當前節點,則向左,如果更大,則向右。如果你的指針爲空,則返回false;

4)把它放在一起。

征服這部分是非常不容易。