Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/algorithms/graphs/depthFirstSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -56,6 +55,36 @@ public static List<Integer> preOrder(BinaryTreeNode root) {
return traversal;
}

public static List<Integer> inOrder(BinaryTreeNode root) {
if (root == null) { return new ArrayList<>(); }
List<Integer> 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<Integer> left = inOrder(root.getLeft());
left.addAll(traversal);
return left;
}
}

public static List<Integer> postOrder(BinaryTreeNode root) {
if (root == null) { return new ArrayList<>(); }
List<Integer> 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<Integer> leftAndRight = postOrder(root.getLeft());
leftAndRight.addAll(postOrder(root.getRight()));
leftAndRight.addAll(traversal);
return leftAndRight;
}
}

// call this for visualization of process
public static List<Integer> preOrderVisualize(BinaryTreeNode root) {
if (root == null) { return new ArrayList<>(); }
Expand Down
72 changes: 72 additions & 0 deletions test/algorithms/graphs/depthFirstSearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> firstList = new ArrayList<>();
BinaryTreeNode root1 = null;
List<Integer> firstResult = depthFirstSearch.inOrder(root1);

//standard tree
// 1
// / \
// 2 3
// / \
// 4 5
List<Integer> 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<Integer> secondResult = depthFirstSearch.inOrder(root2);

//standard tree 2
// 1
// / \
// 2 7
// / \
// 3 5
// / /
// 4 6
List<Integer> 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<Integer> 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<Integer> firstList = new ArrayList<>();
BinaryTreeNode root1 = null;
List<Integer> firstResult = depthFirstSearch.inOrder(root1);

//standard tree
// 1
// / \
// 2 3
// / \
// 4 5
List<Integer> 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<Integer> secondResult = depthFirstSearch.postOrder(root2);

//standard tree 2
// 1
// / \
// 2 7
// / \
// 3 5
// / /
// 4 6
List<Integer> 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<Integer> thirdResult = depthFirstSearch.postOrder(root3);

assert firstResult.equals(firstList);
assert secondResult.equals(secondList);
assert thirdResult.equals(thirdList);
}
}