Search in sources :

Example 11 with BinaryNode

use of com.github.pedrovgs.binarytree.BinaryNode in project Algorithms by pedrovgs.

the class SortedArrayToBSTTest method shouldReturnOneBinarySearchTree.

/**
   * If you get an in order traversal of a BST you get a sorted collection of elements. We are
   * going to use this property to assert the result.
   */
@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);
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode) BinaryTreeInOrder(com.github.pedrovgs.problem15.BinaryTreeInOrder) Test(org.junit.Test)

Aggregations

BinaryNode (com.github.pedrovgs.binarytree.BinaryNode)11 LinkedList (java.util.LinkedList)5 Stack (java.util.Stack)4 Test (org.junit.Test)3 BinaryTreeInOrder (com.github.pedrovgs.problem15.BinaryTreeInOrder)1