title: Binary search tree: return occurrences of a word from a text file
body: <p>This is my first post so apologies if I haven't quite got the framing of the question quite right yet. I'll put my hands up straight away and say this is part of my 'homework', I've got about 99% of it done but I've got this niggling bug that has been driving me crazy all week and I simply can't see the wood through the trees here.</p>
  
  <p>The basic concept is that we have a text file, a poem of sorts, that our program has to look at and then place into a binary tree. We should record each word, the occurrence of each word and which line it appears in. I have managed to do that, save for that for in the example <code>"And I went to StackOverFlow and I asked the question, and the answer was..."</code> my program <em>should</em> return that the word <code>"And"</code> appears in line 1 (which it currently does), but it is currently recording every occurrence of that word and it should only record that the word "and" occurs in line 1 and not every instance of it.</p>
  
  <p>So in the above example <code>"And I went to StackOverFlow and I asked the question, and the answer was.."</code></p>
  
  <p>So just picking out the word <code>"And"</code> here since it occurs multiple times, my program is currently returning:</p>
  
  <p><code>"And [1, 1, 1]"</code></p>
  
  <p>But it <em>should</em> be returning:</p>
  
  <p><code>"And [1]"</code></p>
  
  <p>I have supplied the code for the entire program, but the method I think to focus on is the <code>recordWord</code> method, at the final <code>else if</code>, something isn't quite right there clearly! Apologies if this explanation is hugely convoluted but I couldn't think of another way to explain it! Any help would be hugely appreciated</p>
  
  <pre><code>import java.util.*;
  
  /**
   * A class representing a binary tree containing words.
   * 
   */
  public class WordTree {
  
      public String word;
      public ArrayList&lt;Integer&gt; lineNumbers;
      public WordTree left, 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 ArrayList&lt;Integer&gt;();
          lineNumbers.add(lineNo);
          left = null;
          right = null;
      }
  
      /**
       * Records a new occurrence of the given word, in the tree.
       * 
       * @param w
       *            the word
       * @param lineNo
       *            the line number
       * @pre this is a well formed binary search tree
       * @post if word was not in this tree, then the word and its line number
       *       line have been inserted into ordered word tree, else line has been
       *       appended to line-number list for word (if we haven't already
       *       recorded that line number for this word)
       */
      public void recordWord(String word2, int lineNo) {
          if (word.compareToIgnoreCase(word2) &lt; 0) {
              if (right != null) {
                  right.recordWord(word2, lineNo);
              } else {
                  right = new WordTree(word2, lineNo);
              }
          } else if (word.compareToIgnoreCase(word2) &gt; 0) {
              if (left != null) {
                  left.recordWord(word2, lineNo);
              } else {
                  left = new WordTree(word2, lineNo);
              }
  
          } else if (word.compareToIgnoreCase(word2) == 0) {
  
              lineNumbers.add(lineNo);
  
          }
      }
  
      // System.out.println("method recordWord not implemented yet");
  
      /**
       * Displays all the words in this tree.
       * 
       * @pre this is a well formed binary search tree
       * @post words have been written out in alphabetical order, each followed by
       *       ascending list of line numbers on which the word occurs
       */
      public void display() {
  
          if (left != null) {
              left.display();
          }
          System.out.println(word + lineNumbers);
  
          if (right != null) {
              right.display();
          }
  
      }
  
      /**
       * Counts how many different words there are in this tree.
       * 
       * @pre this is a well formed binary search tree
       * @return the number of different words in tree
       */
      public int numberOfEntries() {
          int count = 1;
          if (left != null) {
              count += left.numberOfEntries();
          }
          if (right != null) {
              count += right.numberOfEntries();
          }
  
      }
  }
  </code></pre>