diff --git a/src/algorithms/graphs/depthFirstSearch.java b/src/algorithms/graphs/depthFirstSearch.java index e8bb2fed..808e5288 100644 --- a/src/algorithms/graphs/depthFirstSearch.java +++ b/src/algorithms/graphs/depthFirstSearch.java @@ -32,10 +32,9 @@ * If an adjacency matrix were used, it would cost O(V) to find neighbours for a single vertex, making our * average case time complexity O(V^2) for a connected graph * - * The implementation demonstrates the use of DFS in finding the pre-order (Root, Left, Right) traversal of a binary tree + * The implementation demonstrates the use of DFS in finding the order traversals of a binary tree * The tree is represented using a custom BinaryTreeNode class * - * TODO: Add new examples, and algo for all orderings */ public class depthFirstSearch { @@ -56,6 +55,36 @@ public static List preOrder(BinaryTreeNode root) { return traversal; } + public static List inOrder(BinaryTreeNode root) { + if (root == null) { return new ArrayList<>(); } + List traversal = new ArrayList<>(); + traversal.add(root.getVal()); + if (root.getLeft() == null && root.getRight() == null) { + return traversal; + } else { + // we combine the traversal of the left subtree with the root and the traversal of the right subtree + traversal.addAll(inOrder(root.getRight())); + List left = inOrder(root.getLeft()); + left.addAll(traversal); + return left; + } + } + + public static List postOrder(BinaryTreeNode root) { + if (root == null) { return new ArrayList<>(); } + List traversal = new ArrayList<>(); + traversal.add(root.getVal()); + if (root.getLeft() == null && root.getRight() == null) { + return traversal; + } else { + // we combine the traversal of the left and right subtrees with the root + List leftAndRight = postOrder(root.getLeft()); + leftAndRight.addAll(postOrder(root.getRight())); + leftAndRight.addAll(traversal); + return leftAndRight; + } + } + // call this for visualization of process public static List preOrderVisualize(BinaryTreeNode root) { if (root == null) { return new ArrayList<>(); } diff --git a/test/algorithms/graphs/depthFirstSearchTest.java b/test/algorithms/graphs/depthFirstSearchTest.java index 84cef22a..d9f12bbf 100644 --- a/test/algorithms/graphs/depthFirstSearchTest.java +++ b/test/algorithms/graphs/depthFirstSearchTest.java @@ -46,4 +46,76 @@ public void dfs_preOrderTraversal_shouldReturnAccurate() { assert secondResult.equals(secondList); assert thirdResult.equals(thirdList); } + + @Test + public void dfs_inOrderTraversal_shouldReturnAccurate() { + // empty tree + List firstList = new ArrayList<>(); + BinaryTreeNode root1 = null; + List firstResult = depthFirstSearch.inOrder(root1); + + //standard tree + // 1 + // / \ + // 2 3 + // / \ + // 4 5 + List secondList = new ArrayList<>(Arrays.asList(2, 1, 4, 3, 5)); + BinaryTreeNode rootRight2 = new BinaryTreeNode(3, new BinaryTreeNode(4), new BinaryTreeNode(5)); + BinaryTreeNode root2 = new BinaryTreeNode(1, new BinaryTreeNode(2), rootRight2); + List secondResult = depthFirstSearch.inOrder(root2); + + //standard tree 2 + // 1 + // / \ + // 2 7 + // / \ + // 3 5 + // / / + // 4 6 + List thirdList = new ArrayList<>(Arrays.asList(4, 3, 2, 6, 5, 1, 7)); + BinaryTreeNode rootLeft3 = new BinaryTreeNode(2, new BinaryTreeNode(3, new BinaryTreeNode(4), null), new BinaryTreeNode(5, new BinaryTreeNode(6), null)); + BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7)); + List thirdResult = depthFirstSearch.inOrder(root3); + + assert firstResult.equals(firstList); + assert secondResult.equals(secondList); + assert thirdResult.equals(thirdList); + } + + @Test + public void dfs_postOrderTraversal_shouldReturnAccurate() { + // empty tree + List firstList = new ArrayList<>(); + BinaryTreeNode root1 = null; + List firstResult = depthFirstSearch.inOrder(root1); + + //standard tree + // 1 + // / \ + // 2 3 + // / \ + // 4 5 + List secondList = new ArrayList<>(Arrays.asList(2, 4, 5, 3, 1)); + BinaryTreeNode rootRight2 = new BinaryTreeNode(3, new BinaryTreeNode(4), new BinaryTreeNode(5)); + BinaryTreeNode root2 = new BinaryTreeNode(1, new BinaryTreeNode(2), rootRight2); + List secondResult = depthFirstSearch.postOrder(root2); + + //standard tree 2 + // 1 + // / \ + // 2 7 + // / \ + // 3 5 + // / / + // 4 6 + List thirdList = new ArrayList<>(Arrays.asList(4, 3, 6, 5, 2, 7, 1)); + BinaryTreeNode rootLeft3 = new BinaryTreeNode(2, new BinaryTreeNode(3, new BinaryTreeNode(4), null), new BinaryTreeNode(5, new BinaryTreeNode(6), null)); + BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7)); + List thirdResult = depthFirstSearch.postOrder(root3); + + assert firstResult.equals(firstList); + assert secondResult.equals(secondList); + assert thirdResult.equals(thirdList); + } }