SortedArrayToBST { public BinaryNode<Integer> transform(Integer[] sortedArray) { if (sortedArray == null || sortedArray.length == 0) { throw new IllegalArgumentException("You can't use a null or empty array."); } return transformToBST(sortedArray, 0, sortedArray.length - 1); }  BinaryNode<Integer> transform(Integer[] sortedArray);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullArrays() { sortedArrayToBST.transform(null); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptAnEmptyArray() { Integer[] emptyArray = new Integer[0]; sortedArrayToBST.transform(emptyArray); }
@Test public void shouldReturnJustOneNodeIfTheArrayContainsJustOneElement() { Integer[] array = { 1 }; BinaryNode<Integer> result = sortedArrayToBST.transform(array); assertEquals(new Integer(1), result.getData()); }
@Test public void shouldReturnOneBinarySearchTree() { Integer[] array = { 1, 2, 3, 4, 5, 6, 7, 8 }; BinaryNode<Integer> result = sortedArrayToBST.transform(array); BinaryTreeInOrder inOrder = new BinaryTreeInOrder(); List<BinaryNode<Integer>> resultList = inOrder.getIterative(result); Integer[] resultArray = new Integer[resultList.size()]; for (int i = 0; i < resultList.size(); i++) { resultArray[i] = resultList.get(i).getData(); } assertArrayEquals(array, resultArray); }