2017-03-16 179 views
1

因此,我創建了一個包含單詞的二叉搜索樹。它需要一個文本文件並打印出單詞和行號。然而,我有一些代碼中的空值,我不知道爲什麼。我已經發布了輸出窗口以向您展示,但我很困惑,爲什麼它會打印單詞,行號,然後爲空。任何幫助非常讚賞這個初學者的編碼器!Java二進制搜索詞樹

public class WordTree { 

    public static String word; 
    //a set does not allow duplicates 
    public static Set<Integer> lineNumbers; 
    public static WordTree left; 
    public static WordTree right; 

    /** Constructs a tree consisting of a single node, with 
    * the given word and line number. 
    * 
    * @param w   the word 
    * @param lineNo  the line number 
    * @pre    true 
    * @post    word tree containing word w on line lineNo has been constructed 
    */ 
    public WordTree(String w, int lineNo) { 
     word = w; 
     lineNumbers = new TreeSet<Integer>(); 
     lineNumbers.add(lineNo); 
     left = null; 
     right = null; 
    } 

public static WordTree recordWord(WordTree tree, String word2, int lineNo) { 

    if (tree == null) { 
     tree = new WordTree(word, lineNo); 

     return tree; 
     } 
     else if (word.compareToIgnoreCase((String)(word2)) == 0) { 
       return tree; //duplicate word found - do nothing 
      } 
     else if (word.compareToIgnoreCase((String)(word2)) > 0){ 
      if (left != null){ 
        WordTree.left = recordWord(tree, word2, lineNo); 
       } 
      } 

     else if (word.compareToIgnoreCase(word2) < 0) { 
      if (right != null) { 
      WordTree.right = recordWord(tree, word2, lineNo); 
           } 
        } 


         return tree; 
      } 

    /** 
    * Displays all the words in a WordTree. 
    * @param right2 
    * 
    * @param tree the WordTree whose contents are to be displayed 
    * PRECONDITION: tree is a well formed binary search tree 
    * POSTCONDITION words have been written out in alphabetical order, each 
    * followed by ascending list of line numbers on which the word occurs 
    * @param lineNo 
    * @param s 
    * @return 
    * @return 
    */ 

public static WordTree display(WordTree tree, String word, int lineNo) { 

    if (left != null) { 
      WordTree.display(tree, word, lineNo); 
     } 
     System.out.println(word + lineNumbers); 

     if (right != null) { 
      WordTree.display(tree, word, lineNo); 
     } 
     return tree; 

    } 


    /** 
    * Counts how many different words there are in a WordTree 
    * 
    * @param tree the WordTree whose words are to be counted 
    * @return the number of different words in tree 
    * PRECONDITION: tree is a well formed binary search tree 
    */ 
    public int numberOfEntries() { 

     int count = 1; 
      if (left != null) { 
       count += WordTree.left.numberOfEntries(); 
      } 
      if (right != null) { 
       count += WordTree.right.numberOfEntries(); 
      } 
     return count; 


    } 




} 

TreeUtils類

public class TreeUtils { 

public static WordTree tree; 
public static String word2; 



    public static void main(String[] args){ 

     WordTree wt = new WordTree(null, 0); 

     int lineNo = 1; 
     Scanner sc2 = null; 
     try { 
      sc2 = new Scanner(new File("C:/macavity.txt")); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     while (sc2.hasNextLine()) { 
       Scanner s2 = new Scanner(sc2.nextLine()); 
      while (s2.hasNext()) { 
       String s = s2.next(); 
       if (s.contains("@")){ 
        lineNo++; 
       } 
       else{ 


        WordTree.recordWord(tree, word2, lineNo); 

       } 



       System.out.println(WordTree.display(tree, s, lineNo)); 



      } 

     } 

     System.out.println("Count" + wt.numberOfEntries()); 

} 


} 

輸出窗口

Macavity's[1] 
null 
a[1] 
null 
Mystery[1] 
null 
Cat:[1] 
null 
he's[1] 
null 
called[1] 
null 
the[1] 
null 
Hidden[1] 
null 
Paw[1] 
null 
@[1] 
null 
For[2] 
null 
he's[2] 
null 
the[2] 
null 
master[2] 
null 
criminal[2] 
null 
who[2] 
null 
can[2] 
null 
defy[2] 
null 
the[2] 
null 
Law.[2] 
null 
@[2] 
null 
He's[3] 
null 
the[3] 
null 
bafflement[3] 
null 
of[3] 
null 
Scotland[3] 
null 
Yard,[3] 
null 
the[3] 
null 
Flying[3] 
null 
Squad's[3] 
null 
despair[3] 
null 
@[3] 
null 
For[4] 
null 
when[4] 
null 
they[4] 
null 
reach[4] 
null 
the[4] 
null 
scene[4] 
null 
of[4] 
null 
crime[4] 
null 
Macavity's[4] 
null 
not[4] 
null 
there![4] 
null 
@[4] 
null 
[T.S.Eliot][5] 
null 
Count1 

回答

0

相反的:

System.out.println(WordTree.display(tree, s, lineNo)); 

只要做到:

WordTree.display(tree, s, lineNo); 

由於display返回一個WordTree對象,當沒有更多的節點可顯示時,它將最終成爲null。並且您已經打印display方法中的值,因此不需要打印WordTree對象。

+0

非常感謝你!有效!!我不能贊成你的評論,因爲我的聲望不到15 – Strawberry