Java二进制搜索词树

Java二进制搜索词树,java,binary-search-tree,Java,Binary Search Tree,所以我创建了一个包含单词的二叉搜索树。它获取一个文本文件并打印出单词和行号。然而,我在代码中有许多空值,我不知道为什么。我已经发布了输出窗口以便向您展示,但我不明白为什么它会打印单词、行号,然后是null。任何帮助非常感谢这个非常初学者的编码器 public class WordTree { public static String word; //a set does not allow duplicates public static Set<Integer&

所以我创建了一个包含单词的二叉搜索树。它获取一个文本文件并打印出单词和行号。然而,我在代码中有许多空值,我不知道为什么。我已经发布了输出窗口以便向您展示,但我不明白为什么它会打印单词、行号,然后是null。任何帮助非常感谢这个非常初学者的编码器

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;


    }




}
输出窗口

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
而不是:

System.out.println(WordTree.display(tree, s, lineNo));
只要做:

WordTree.display(tree, s, lineNo);
display
返回一个
WordTree
对象时,当没有更多的节点可显示时,该对象将变成
null
。而且您已经在打印
display
方法中的值,因此不需要打印
WordTree
对象。

而不是:

System.out.println(WordTree.display(tree, s, lineNo));
只要做:

WordTree.display(tree, s, lineNo);

display
返回一个
WordTree
对象时,当没有更多的节点可显示时,该对象将变成
null
。您已经在打印
display
方法中的值,因此不需要打印
WordTree
对象。

非常感谢!!成功了!!我不能对你的评论投赞成票,因为我的声誉不到15。非常感谢!!成功了!!我不能推翻你的评论,因为我的声誉不到15