title: Why do I need a helper method to recursively search a binary search tree?
body: <p>Here is the code for the implementation of the Binary Search Tree:</p>
  
  <pre><code>public class BST&lt;T extends Comparable&lt;T&gt;&gt; {
  
  BSTNode&lt;T&gt; root;
  
  
  
  
  public T search(T target)
  {
      //loop to go through nodes and determine which routes to make
      BSTNode&lt;T&gt; tmp = root;
      while(tmp != null)
      {
          //c can have 3 values
          //0 = target found
          //(negative) = go left, target is smaller
          //(positive) = go left, target is greater than current position
          int c = target.compareTo(tmp.data);
  
          if(c==0)
          {
              return tmp.data;
          }
          else if(c&lt;0)
          {
              tmp = tmp.left;
          }
          else
          {
              tmp = tmp.right;
          }
  
      }
      return null;
  }
  
  /*
   * Need a helper method
   */
  public T recSearch(T target)
  {
      return recSearch(target, root);
  
  }
  
  //helper method for recSearch()
  private T recSearch(T target, BSTNode&lt;T&gt; root)
  {
      //Base case
      if(root == null)
          return null;
  
      int c = target.compareTo(root.data);
  
      if(c == 0)
          return root.data;
      else if(c&lt;0)
          return recSearch(target, root.left);
      else
          return recSearch(target, root.right);
  }
  </code></pre>
  
  <p>Why do I need the recursive helper method? Why can't I I just use "this.root" to carry out the recursive process that is taking place? Furthermore, if screwing up the root property of the object this method is being called on is a problem, then how is does the helper method prevent this from happening? Does it just create a pointer that is separate from the this.root property, and therefore won't mess up the root property of the object that the method is being called on?</p>
  
  <p>Sorry if the question doesn't seem straight forward, but if anyone can enlighten me on what's exactly going on behind the scenes I would really appreciate it.</p>