title: Testing a method using recursion in binary search tree
body: <p>This <code>toRoot</code> method is supposed to return a list containing all the keys in the tree between the parameter <code>key</code> and the root. I have used a <code>System.out.println</code> statement in both the class and the junit test method. Why are the outputs of the two print statements different? The output printed in the <code>toRoot</code> method is what I want, but I am printing only the root in the junit test method. How do I fix this?</p>
  
  <pre><code>public List&lt;K&gt; toRoot(K key) {
          List&lt;K&gt; list=new LinkedList&lt;K&gt;();
          int i = key.compareTo(this.key);
          if(i == 0){
              list.add(this.key);
          }
          else if(i &lt; 0){
              left.toRoot(key);
              list.add(this.key);
          }
          else if(i &gt; 0){
              right.toRoot(key);
              list.add(this.key);
          }   
          System.out.print(list); //prints "i o n p s z i"...these are the desired 
          //outputs of the two assertEquals combined together
          return list;
      }
  </code></pre>
  
  <hr>
  
  <p>JUnit Testing</p>
  
  <pre><code>@Test public void testToRoot() {
      Tree&lt;Character, Integer&gt; tree = tree1();
      System.out.print(tree.toRoot('o')); //prints 'i'
      assertEquals("i", tree.toRoot('i'));
      assertEquals("o n p s z i", tree.toRoot('o'));
  }
  private static Tree&lt;Character, Integer&gt; tree1() {
      Tree&lt;Character, Integer&gt; tree = EmptyTree.getInstance();
  
      tree= tree.add('i', 1);
      tree= tree.add('z', 2);
      tree= tree.add('e', 3);
      tree= tree.add('s', 4);
      tree= tree.add('p', 5);
      tree= tree.add('n', 6);
      tree= tree.add('b', 7);
      tree= tree.add('h', 8);
      tree= tree.add('o', 9);
      tree= tree.add('f', 10);
  
      return tree;
  }
  </code></pre>
  
  <hr>
  
  <p>additional details:
  The tree class is an interface that the <code>NotEmptyTree</code> class implements and includes an overwritten <code>toRoot</code>. </p>
  
  <pre><code>public NonEmptyTree&lt;K, V&gt; add(K key, V value);
  </code></pre>
  
  <p>Here is the overwritten add method in the <code>NotEmptyTree</code> class</p>
  
  <pre><code>public NotEmptyTree&lt;K, V&gt; add(K key, V value) {
          if(key.compareTo(this.key) == 0){
              this.value = value;
          }else if(key.compareTo(this.key) &lt; 0){
              left = left.add(key, value);
          }else{
              right = right.add(key, value);
          }
          return this;    
      }
  </code></pre>
  
  <p>Letters near the front of the alphabet are smaller than those towards the end. In this case, <code>'i'</code> is the parent node of <code>'e'</code> and <code>'z'</code>. <code>'z'</code> has a left child of <code>'s'</code>, which has left child of <code>'p'</code>, which has left child of <code>'n'</code>, which has right child of <code>'o'</code>. <code>'e'</code> has left child of <code>'b'</code>, which has right child of <code>'h'</code>.</p>