import java.util.Arrays; import java.util.Collections; import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import org.junit.Ignore; import org.junit.Test; public class BinarySearchTreeTest { @Test public void dataIsRetained() { BinarySearchTree binarySearchTree = new BinarySearchTree<>(); final int expected = 4; binarySearchTree.insert(expected); final BinarySearchTree.Node root = binarySearchTree.getRoot(); assertNotNull(root); final int actual = root.getData(); assertEquals(expected, actual); } @Ignore("Remove to run test") @Test public void insertsLess() { BinarySearchTree binarySearchTree = new BinarySearchTree<>(); final char expectedRoot = '4'; final char expectedLeft = '2'; binarySearchTree.insert(expectedRoot); binarySearchTree.insert(expectedLeft); final BinarySearchTree.Node root = binarySearchTree.getRoot(); assertNotNull(root); final BinarySearchTree.Node left = root.getLeft(); assertNotNull(left); final char actualRoot = root.getData(); final char actualLeft = left.getData(); assertEquals(expectedLeft, actualLeft); assertEquals(expectedRoot, actualRoot); } @Ignore("Remove to run test") @Test public void insertsSame() { BinarySearchTree binarySearchTree = new BinarySearchTree<>(); final String expectedRoot = "4"; final String expectedLeft = "4"; binarySearchTree.insert(expectedRoot); binarySearchTree.insert(expectedLeft); final BinarySearchTree.Node root = binarySearchTree.getRoot(); assertNotNull(root); final BinarySearchTree.Node left = root.getLeft(); assertNotNull(left); final String actualRoot = root.getData(); final String actualLeft = left.getData(); assertEquals(expectedLeft, actualLeft); assertEquals(expectedRoot, actualRoot); } @Ignore("Remove to run test") @Test public void insertsRight() { BinarySearchTree binarySearchTree = new BinarySearchTree<>(); final int expectedRoot = 4; final int expectedRight = 5; binarySearchTree.insert(expectedRoot); binarySearchTree.insert(expectedRight); final BinarySearchTree.Node root = binarySearchTree.getRoot(); assertNotNull(root); final BinarySearchTree.Node right = root.getRight(); assertNotNull(right); final int actualRoot = root.getData(); final int actualRight = right.getData(); assertEquals(expectedRight, actualRight); assertEquals(expectedRoot, actualRoot); } @Ignore("Remove to run test") @Test public void createsComplexTree() { BinarySearchTree binarySearchTree = new BinarySearchTree<>(); List expected = Collections.unmodifiableList( Arrays.asList('4', '2', '6', '1', '3', '5', '7') ); List treeData = Collections.unmodifiableList( Arrays.asList('4', '2', '6', '1', '3', '5', '7') ); treeData.forEach(binarySearchTree::insert); List actual = binarySearchTree.getAsLevelOrderList(); assertEquals(expected, actual); } @Ignore("Remove to run test") @Test public void sortsSingleElement() { BinarySearchTree binarySearchTree = new BinarySearchTree<>(); List expected = Collections.unmodifiableList( Collections.singletonList("2") ); binarySearchTree.insert("2"); List actual = binarySearchTree.getAsSortedList(); assertEquals(expected, actual); } @Ignore("Remove to run test") @Test public void sortsCollectionOfTwoIfSecondInsertedIsSmallerThanFirst() { BinarySearchTree binarySearchTree = new BinarySearchTree<>(); List expected = Collections.unmodifiableList( Arrays.asList(1, 2) ); binarySearchTree.insert(2); binarySearchTree.insert(1); List actual = binarySearchTree.getAsSortedList(); assertEquals(expected, actual); } @Ignore("Remove to run test") @Test public void sortsCollectionOfTwoIfSecondNumberisSameAsFirst() { BinarySearchTree binarySearchTree = new BinarySearchTree<>(); List expected = Collections.unmodifiableList( Arrays.asList('2', '2') ); binarySearchTree.insert('2'); binarySearchTree.insert('2'); List actual = binarySearchTree.getAsSortedList(); assertEquals(expected, actual); } @Ignore("Remove to run test") @Test public void sortsCollectionOfTwoIfSecondInsertedIsBiggerThanFirst() { BinarySearchTree binarySearchTree = new BinarySearchTree<>(); List expected = Collections.unmodifiableList( Arrays.asList('2', '3') ); binarySearchTree.insert('2'); binarySearchTree.insert('3'); List actual = binarySearchTree.getAsSortedList(); assertEquals(expected, actual); } @Ignore("Remove to run test") @Test public void iteratesOverComplexTree() { BinarySearchTree binarySearchTree = new BinarySearchTree<>(); List expected = Collections.unmodifiableList( Arrays.asList("1", "2", "3", "5", "6", "7") ); List treeData = Collections.unmodifiableList( Arrays.asList("2", "1", "3", "6", "7", "5") ); treeData.forEach(binarySearchTree::insert); List actual = binarySearchTree.getAsSortedList(); assertEquals(expected, actual); } }