package Examples; import org.junit.* ; import static org.junit.Assert.* ; public class TestBinarySearchTree { private BinarySearchTree empty() { return new BinarySearchTree() ; } private BinarySearchTree singleton() { BinarySearchTree t = new BinarySearchTree() ; t.insert(0); return t ; } private BinarySearchTree t1() { BinarySearchTree t = new BinarySearchTree() ; t.insert(0); t.insert(-10); t.insert(10); return t ; } @Test public void find() { assertNull(empty().find(0)) ; assertTrue(singleton().find(0).equals(0)) ; assertNull(singleton().find(10)) ; assertTrue(t1().find(10).equals(10)) ; assertTrue(t1().find(-10).equals(-10)) ; assertNull(t1().find(99)) ; } @Test public void findMax() { assertNull(empty().findMax()) ; assertTrue(singleton().findMax().equals(0)) ; assertTrue(t1().findMax().equals(10)) ; } @Test public void findMin() { assertNull(empty().findMin()) ; assertTrue(singleton().findMin().equals(0)) ; assertTrue(t1().findMin() .equals(-10)) ; } @Test public void isEmpty() { assertTrue(empty().isEmpty()) ; assertFalse(singleton().isEmpty()) ; assertFalse(t1().isEmpty()) ; } @Test public void makeEmpty() { BinarySearchTree t = empty() ; t.makeEmpty() ; assertTrue(t.isEmpty()) ; t = singleton() ; t.makeEmpty() ; assertTrue(t.isEmpty()) ; t = t1() ; t.makeEmpty() ; assertTrue(t.isEmpty()) ; } @Test public void insert() { BinarySearchTree t ; t = empty(); t.insert(0) ; assertTrue(t.findMax().equals(0)) ; t = singleton() ; t.insert(0) ; t.remove(0) ; assertTrue(t.isEmpty()); t = singleton() ; t.insert(10) ; assertTrue(t.findMax().equals(10)) ; t = t1() ; t.insert(10) ; assertTrue(t.findMax().equals(10)) ; t = t1() ; t.insert(-10) ; assertTrue(t.findMin().equals(-10)) ; t = t1() ; t.insert(-5) ; assertTrue(t.findMin().equals(-10)) ; assertTrue(t.findMax().equals(10)) ; assertTrue(t.find(-5).equals(-5)) ; } @Test public void remove() { BinarySearchTree t ; t = empty() ; t.remove(0) ; assertTrue(t.isEmpty()) ; t = singleton() ; t.remove(0) ; assertTrue(t.isEmpty()) ; t = singleton() ; t.remove(10) ; assertFalse(t.isEmpty()) ; t = t1() ; t.remove(10) ; assertNull(t.find(10)) ; t = t1() ; t.remove(-10) ; assertNull(t.find(-10)) ; t = t1() ; t.remove(-5) ; assertNull(t.find(-5)) ; } }