2017-03-16 174 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