title: Heap Data-Structure Re-Heap Method
body: <p>I'm working on my assignment, which is to read from a text file, store the first 10 words in a heap. Then continue to read from the text file and if the word is less than the root of the heap, to replace it and re-heap the entire heap. My code seems to be working for the most part however I am running into a few problems.</p>
  
  <ul>
  <li>Some words even though they are less than the root are not being swapped</li>
  <li>Duplicate words</li>
  </ul>
  
  <p>I am supposed to end up with a heap containing the words <code>abandoning abandons
  abased
  abash
  abashed
  abashes
  abasing
  abate
  abatement
  abbe</code></p>
  
  <p>However I get the words, <code>abashes
                  abashed                              abash
          abased              abandons              abandoning              bewilderedly
      abandoning      armful      abandoning</code></p>
  
  <hr>
  
  <p>Here is my code so far:</p>
  
  <pre><code>public static void readFile() {
      BufferedReader reader;
      String inputLine;
      int counter = 0;
  
      try {
          reader = new BufferedReader(new FileReader(".\\src\\dictionary.txt"));
          while((inputLine = reader.readLine()) != null) {
              if(counter &lt; 10) {
                  heap.insert(inputLine);
                  counter++;
              }
  
              if(inputLine.compareTo(heap.find(0)) &lt; 0) {
                  heap.change(0, inputLine);
              }
          }
      } catch (IOException e) {
          System.out.println("Error: " + e);
      }
  }
  
  public boolean insert(String value) {
      if(currentSize == maxSize) { return false; }
  
      Node newNode = new Node(value);
      heap[currentSize] = newNode;
      trickleUp(currentSize++);
      return true;
  }
  
  public void trickleUp(int index) {
      int parent = (index - 1) / 2;
      Node bottom = heap[index];
  
      while(index &gt; 0 &amp;&amp; heap[parent].getData().compareTo(bottom.getData()) &lt; 0) {
          heap[index] = heap[parent];
          index = parent;
          parent = (parent - 1) / 2;
      }
      heap[index] = bottom;
  }
  
  public void trickleDown(int index) {
      int largerChild;
      Node top = heap[index];
  
      while(index &lt; currentSize / 2) {
          int leftChild = 2 * index + 1;
          int rightChild = index + 1;
  
          if(rightChild &lt; currentSize &amp;&amp; heap[leftChild].getData().compareTo(heap[rightChild].getData()) &lt; 0) {
              largerChild = rightChild;
          } else {
              largerChild = leftChild;
          }
  
          if(top.getData().compareTo(heap[largerChild].getData()) &gt; 0) {
              break;
          }
  
          heap[index] = heap[largerChild];
          index = largerChild;
      }
      heap[index] = top;
  }
  
  public boolean change(int index, String newValue) {
      if(index &lt; 0 || index &gt;= currentSize) { return false; }
  
      String oldValue = heap[index].getData();
      heap[index].setData(newValue);
  
      if(oldValue.compareTo(newValue) &lt; 0) {
          trickleUp(index);
      } else {
          trickleDown(index);
      }
      return true;
  }
  </code></pre>