title: How to fill an array with node elements from a binary search tree, in ascending order?
body: <p>In a school assignment I'm supposed to complete a method that should return an array of
  node elements in ascendig order. The nodes are assembled in a binary search tree, so to sort them correct, I got a tip to create a recursive method to do the job.</p>
  
  <p>The problem is that this doesn't even yield all the elements in the collection according to the test output (java.lang.AssertionError: toArray() does not return all the elements in the collection.)</p>
  
  <p>I couldn't come up with any other way to deal with the array, and I'm not quite sure if the recursion even works. Any help is much appreciated.
  Below is my code:</p>
  
  <pre><code>public class BinarySearchTree&lt;E extends Comparable&lt;E&gt;&gt; implements
      IfiCollection&lt;E&gt; {
  
      Node root;
      Node current;
      int size = 0;
      int i = 0;
  
      public class Node {
      E obj;
      Node left, right;
  
      public Node(E e) {
          obj = e;
      }
  
      } // END class Node
  
      [...]
  
      public E[] toArray(E[] a) {
  
      Node n = root;
  
      a = sort(n, a);
      return a;
  
      }
  
      public E[] sort(Node n, E[] a) { //, int idx, E[] a) {
  
      if (n.left != null) {
          current = n.left;
          sort(current, a);
      }
  
  
      a[i] = current.obj;
      i++;
  
      if (n.right != null) {
          current = n.right;
          sort(current, a);
          }
  
      return a;
  
      } // END public Node sort
  
      [...]
  
  } // END class BinarySearchTree
  </code></pre>
  
  <p>Test output:</p>
  
  <p>java.lang.AssertionError: toArray() does not return all the elements in the collection.: TestPerson("Bender").compareTo(TestPerson("Fry")) == 0 expected:true but was:false
      at inf1010.assignment.IfiCollectionTest.assertCompareToEquals(IfiCollectionTest.java:74)
      at inf1010.assignment.IfiCollectionTest.assertCompareToEquals(IfiCollectionTest.java:83)
      at inf1010.assignment.IfiCollectionTest.assertCompareToEqualsNoOrder(IfiCollectionTest.java:100)
      at inf1010.assignment.IfiCollectionTest.toArray(IfiCollectionTest.java:202)</p>
  
  <pre><code>protected void assertCompareToEquals(TestPerson actual,
          TestPerson expected, String msg) {
              assertTrue(actual.compareTo(expected) == 0, String.format( // l:74
              "%s: %s.compareTo(%s) == 0", msg, actual, expected));
  }
  
      [...]
  
  protected void assertCompareToEquals(TestPerson[] actual,
          TestPerson[] expected, String msg) {
      for (int i = 0; i &lt; actual.length; i++) {
          TestPerson a = actual[i];
          TestPerson e = expected[i];
          assertCompareToEquals(a, e, msg); // l:83
      }
  }
  
      [...]
  
  protected void assertCompareToEqualsNoOrder(TestPerson[] actual,
          TestPerson[] expected, String msg) {
      assertEquals(actual.length, expected.length, msg);
  
      TestPerson[] actualElements = new TestPerson[actual.length];
      System.arraycopy(actual, 0, actualElements, 0, actual.length);
  
      TestPerson[] expectedElements = new TestPerson[expected.length];
      System.arraycopy(expected, 0, expectedElements, 0, expected.length);
  
      Arrays.sort(expectedElements);
      Arrays.sort(actualElements);
  
      assertCompareToEquals(actualElements, expectedElements, msg); // l:100
  }
  
      [...]
  
  @Test(dependsOnGroups = { "collection-core" },
      description="Tests if method toArray yields all the elements inserted in the collection in sorted order with smallest item first.")
  public void toArray() {
      TestPerson[] actualElements = c.toArray(new TestPerson[c.size()]);
  
      for (int i = 0; i &lt; actualElements.length; i++) {
          assertNotNull(actualElements[i],
                  "toArray() - array element at index " + i + " is null");
      }
  
      TestPerson[] expectedElements = allElementsAsArray();
      assertCompareToEqualsNoOrder(actualElements, expectedElements, // l:202
              "toArray() does not return all the elements in the collection.");
  
      Arrays.sort(expectedElements);
      assertCompareToEquals(actualElements, expectedElements,
              "toArray() does not return the elements in sorted order with "
                      + "the smallest elements first.");
  
  
      TestPerson[] inArr = new TestPerson[NAMES.length + 1];
      inArr[NAMES.length] = new TestPerson("TEMP");
      actualElements = c.toArray(inArr);
      assertNull(actualElements[NAMES.length],
              "The the element in the array immediately following the "
              + "end of the list is not set to null");
  }
  </code></pre>
  
  <p>I don't know if I should post more of the test code, it's quite extensive, and it might be a little too much for one post?</p>