2017-03-16 2 views
1

그래서 단어가 포함 된 이진 검색 트리를 만듭니다. 그것은 텍스트 파일을 가져 와서 단어와 줄 번호를 출력합니다. 그러나 나는 왜 코드가 null인지 모르겠다. 출력 창을 게시 했으므로 표시해 드리지만 단어, 줄 번호 및 그 다음 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; 


    } 




} 

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); 

displayWordTree 개체를 반환하므로 더 이상 표시 할 노드가 없으면 null이됩니다. 그리고 이미 display 메소드 내부의 값을 인쇄하고 있으므로 WordTree 객체를 인쇄 할 필요가 없습니다.

+0

정말 고마워요 !! 그것은 일했다! 내가 15 미만의 평판을 가지고 당신의 의견을 upvote 수 없습니다 – Strawberry