Java 二叉搜索词树



所以我创建了一个包含单词的二叉搜索树。它需要一个文本文件并打印出单词和行号。但是我在代码中有许多空值,我不确定为什么。我已经发布了输出窗口以向您展示,但我对为什么它打印单词、行号然后 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;

    }


}

树实用类

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

而不是:

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

只需做:

WordTree.display(tree, s, lineNo);

display返回一个WordTree对象时,当没有更多要显示的节点时,它最终会被null。并且您已经在display方法中打印值,因此您无需打印WordTree对象。