JavaAlgorithms
Elementary and no so elementary Java algorithms
|
00001 00009 package treeAlgorithms; 00010 00011 import static org.junit.Assert.*; 00012 00013 import java.util.ArrayList; 00014 00015 import org.junit.Test; 00016 00022 public class TestTree { 00023 00024 @Test 00025 public void testOrderedCheck() { 00026 TreeNode<Integer> four = new TreeNode<Integer>(4); 00027 TreeNode<Integer> six = new TreeNode<Integer>(6); 00028 TreeNode<Integer> eight = new TreeNode<Integer>(8); 00029 TreeNode<Integer> twelve = new TreeNode<Integer>(12); 00030 TreeNode<Integer> ten = new TreeNode<Integer>(10); 00031 TreeNode<Integer> thirteen = new TreeNode<Integer>(13); 00032 TreeNode<Integer> sixteen = new TreeNode<Integer>(16); 00033 TreeNode<Integer> thirtyTwo = new TreeNode<Integer>(32); 00034 TreeNode<Integer> sixtyFour = new TreeNode<Integer>(64); 00035 six.setLeft(four); 00036 twelve.setLeft(ten); 00037 twelve.setRight(thirteen); 00038 eight.setLeft(six); 00039 eight.setRight(twelve); 00040 thirtyTwo.setRight(sixtyFour); 00041 sixteen.setLeft(eight); 00042 sixteen.setRight(thirtyTwo); 00043 assertTrue("Tree is ordered by was not recognized", Algorithms.isOrdered(sixteen)); 00044 eight.setValue(96); 00045 assertFalse("Tree is not ordered but was not recognized", Algorithms.isOrdered(sixteen)); 00046 eight.setValue(8); 00047 thirteen.setValue(5); 00048 assertFalse("Tree is not ordered but was not recognized", Algorithms.isOrdered(sixteen)); 00049 } 00050 00054 @Test 00055 public void testStringTreeBuild() { 00056 final String[] words = new String[] { "t'was", "brillig", "and", "the", 00057 "slighty", "toves", "did", "gyre", "and", "gimble", "in", 00058 "the", "wabe", "all", "mimsy", "were", "the", "borogroves", 00059 "and", "moonraths", "outgrabe" }; 00060 TreeBuilder<String> builder = new TreeBuilder<String>(); 00061 for (String word : words) { 00062 builder.addNode(word); 00063 } 00064 TreeNode<String> root = builder.getTree(); 00065 assertTrue("String tree is not ordered", Algorithms.isOrdered(root)); 00066 } 00067 00068 @Test 00069 public void testInOrder() { 00070 class OrderedVisit<T extends Comparable<T>> implements 00071 Algorithms.NodeVisitor<T> { 00072 private ArrayList<T> mValues = new ArrayList<T>(); 00073 00074 @Override 00075 public void visit(TreeNode<T> node) { 00076 Comparable<T> val = node.getValue(); 00077 mValues.add((T) val); 00078 } 00079 00080 public ArrayList<T> getValues() { 00081 return mValues; 00082 } 00083 } // class OrderedVisit 00084 00085 Character vals[] = new Character[] { 'A', 'B', 'D', 'E', 'H', 'I', 'F', 00086 'C', 'G' }; 00087 Character ordered[] = new Character[] { 'A', 'B', 'C', 'D', 'E', 'F', 00088 'G', 'H', 'I' }; 00089 TreeBuilder<Character> builder = new TreeBuilder<Character>(vals); 00090 TreeNode<Character> root = builder.getTree(); 00091 OrderedVisit<Character> visitor = new OrderedVisit<Character>(); 00092 Algorithms.inOrder(root, visitor); 00093 ArrayList<Character> treeVals = visitor.getValues(); 00094 int ix = 0; 00095 boolean OK = true; 00096 for (Character ch : ordered) { 00097 if (!ch.equals(treeVals.get(ix))) { 00098 OK = false; 00099 } 00100 ix++; 00101 } 00102 assertTrue("In order traversal failed", OK); 00103 } 00104 00105 }