title: How do I understand this remove method in heap data structure?
body: <p>Given the following code:</p>
  
  <pre><code>public Node remove() {
      Node rootNode = heapArray[0]; // set the root node 
      heapArray[0] = heapArray[--currentSize]; // putting the value of the last index in the array at 
      // the 0th index 
  
      trickleDown(0); // call the trickle down method starting at the 0th index 
  
      return rootNode; // we return the root node because that will be replaced by another node 
  }
  
  private void trickleDown(int idx) { // idx is the index 
  
      int largerChildIdx; // larger child index 
      Node top = heapArray[idx]; // save last node into top variable ????
  
      // will run as long as idx is not on the bottom row (has at least 1 child)
      while(idx &lt; currentSize/2) {
          int leftChildIdx = 2*idx +1;
          int rightChildIdx = 2*idx +2;
  
          // figure out which child is larger
          if(rightChildIdx &lt; currentSize /* check to make sure we are not all the way at the end of the heap
             */ &amp;&amp; heapArray[leftChildIdx].getKey() &lt; heapArray[rightChildIdx].getKey()) {
              largerChildIdx = rightChildIdx;
          } else {
              largerChildIdx = leftChildIdx;
          }
          if(top.getKey() &gt;= heapArray[largerChildIdx].getKey()) {
              // successfully made root the largest
              break;
          }
          heapArray[idx] = heapArray[largerChildIdx];
          idx = largerChildIdx; 
      }
  
      heapArray[idx] = top;
  
  }
  </code></pre>
  
  <hr>
  
  <p>I have a question for this line:</p>
  
  <p><em>while(idx &lt; currentSize/2)</em></p>
  
  <p>Why is it idx &lt; currentSize/2? What does this accomplish when we are trying to remove from the heap?</p>