@Test public void reverseOrderTraversal_SimpleTree() { BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>(); bst.setComp(getIntComparator()); bst.insert(2); bst.insert(1); bst.insert(3); List<Integer> lst = TreeUtil.reverseLevelOrderTraversal(bst.getRoot()); assertTrue("Expected a non empty list for tree with single node", !lst.isEmpty()); assertTrue("Expecting size of the list to be 3", lst.size() == 3); assertEquals(1, (int) lst.get(0)); assertEquals(3, (int) lst.get(1)); assertEquals(2, (int) lst.get(2)); }
@Test public void reverseOrderTraversal_SimpleTestCase() { BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>(); bst.setComp(getIntComparator()); bst.insert(100); bst.insert(50); bst.insert(150); bst.insert(30); bst.insert(70); bst.insert(60); bst.insert(120); bst.insert(130); bst.insert(170); bst.insert(160); List<Integer> lst = TreeUtil.reverseLevelOrderTraversal(bst.getRoot()); assertTrue("Expected a non empty list for tree with single node", !lst.isEmpty()); int[] expectedVals = new int[] {60, 130, 160, 30, 70, 120, 170, 50, 150, 100}; assertTrue("Expecting size of the list to be 3", lst.size() == expectedVals.length); for (int index = 0; index < expectedVals.length; index++) { assertEquals(expectedVals[index], (int) lst.get(index)); } }
private static BinarySearchTree<Integer> generateRandomIntTree( int size, List<Integer> lstVals, boolean isUnique) { if (lstVals == null) { throw new IllegalArgumentException( "This methods expects an empty set to output all the added values in this set"); } else { // do nothing here. go ahead } lstVals.clear(); BinarySearchTree<Integer> retBst = new BinarySearchTree<Integer>(); retBst.setComp(getIntComparator()); Random random = new Random(); int maxValue = size * 10; int val = -1; for (int iter = 0; iter < size; iter++) { do { val = random.nextInt(maxValue); } while (isUnique && lstVals.contains(val)); lstVals.add(val); retBst.insert(val); } return retBst; }
@Test public void testLevelOrderTraversal_HandCraftedTree() { BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>(); bst.setComp(getIntComparator()); List<List<Integer>> expectValList = new ArrayList<List<Integer>>(); bst.insert(100); int level = 0; expectValList.add(new ArrayList<Integer>()); expectValList.get(level).add(100); level++; expectValList.add(new ArrayList<Integer>()); bst.insert(50); expectValList.get(level).add(50); bst.insert(150); expectValList.get(level).add(150); level++; expectValList.add(new ArrayList<Integer>()); bst.insert(30); expectValList.get(level).add(30); bst.insert(130); expectValList.get(level).add(130); bst.insert(180); expectValList.get(level).add(180); List<List<Integer>> lst = TreeUtil.levelOrderTraversal(bst.getRoot()); String msg = "Input Tree : [ 100, 50, 150, 30, #, 130, 180 ]"; assertNotNull(msg, lst); assertEquals(msg, expectValList.size(), lst.size()); for (int index = 0; index < expectValList.size(); index++) { List<Integer> eLst = expectValList.get(index); List<Integer> aLst = lst.get(index); assertEquals(msg, eLst.size(), aLst.size()); for (int index1 = 0; index1 < eLst.size(); index1++) { assertEquals(msg, eLst.get(index1), aLst.get(index1)); } } }