Skip to content

Commit 56f1e97

Browse files
committed
Merge branch 'main' into branch-Gradle
2 parents b953aeb + 1752f9a commit 56f1e97

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

src/main/java/algorithms/graphs/depthFirstSearch.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@
3232
* If an adjacency matrix were used, it would cost O(V) to find neighbours for a single vertex, making our
3333
* average case time complexity O(V^2) for a connected graph
3434
*
35-
* The implementation demonstrates the use of DFS in finding the pre-order (Root, Left, Right) traversal of a binary tree
35+
* The implementation demonstrates the use of DFS in finding the order traversals of a binary tree
3636
* The tree is represented using a custom BinaryTreeNode class
3737
*
38-
* TODO: Add new examples, and algo for all orderings
3938
*/
4039

4140
public class depthFirstSearch {
@@ -56,6 +55,36 @@ public static List<Integer> preOrder(BinaryTreeNode root) {
5655
return traversal;
5756
}
5857

58+
public static List<Integer> inOrder(BinaryTreeNode root) {
59+
if (root == null) { return new ArrayList<>(); }
60+
List<Integer> traversal = new ArrayList<>();
61+
traversal.add(root.getVal());
62+
if (root.getLeft() == null && root.getRight() == null) {
63+
return traversal;
64+
} else {
65+
// we combine the traversal of the left subtree with the root and the traversal of the right subtree
66+
traversal.addAll(inOrder(root.getRight()));
67+
List<Integer> left = inOrder(root.getLeft());
68+
left.addAll(traversal);
69+
return left;
70+
}
71+
}
72+
73+
public static List<Integer> postOrder(BinaryTreeNode root) {
74+
if (root == null) { return new ArrayList<>(); }
75+
List<Integer> traversal = new ArrayList<>();
76+
traversal.add(root.getVal());
77+
if (root.getLeft() == null && root.getRight() == null) {
78+
return traversal;
79+
} else {
80+
// we combine the traversal of the left and right subtrees with the root
81+
List<Integer> leftAndRight = postOrder(root.getLeft());
82+
leftAndRight.addAll(postOrder(root.getRight()));
83+
leftAndRight.addAll(traversal);
84+
return leftAndRight;
85+
}
86+
}
87+
5988
// call this for visualization of process
6089
public static List<Integer> preOrderVisualize(BinaryTreeNode root) {
6190
if (root == null) { return new ArrayList<>(); }

src/test/java/algorithms/graphs/depthFirstSearchTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,76 @@ public void dfs_preOrderTraversal_shouldReturnAccurate() {
4646
assert secondResult.equals(secondList);
4747
assert thirdResult.equals(thirdList);
4848
}
49+
50+
@Test
51+
public void dfs_inOrderTraversal_shouldReturnAccurate() {
52+
// empty tree
53+
List<Integer> firstList = new ArrayList<>();
54+
BinaryTreeNode root1 = null;
55+
List<Integer> firstResult = depthFirstSearch.inOrder(root1);
56+
57+
//standard tree
58+
// 1
59+
// / \
60+
// 2 3
61+
// / \
62+
// 4 5
63+
List<Integer> secondList = new ArrayList<>(Arrays.asList(2, 1, 4, 3, 5));
64+
BinaryTreeNode rootRight2 = new BinaryTreeNode(3, new BinaryTreeNode(4), new BinaryTreeNode(5));
65+
BinaryTreeNode root2 = new BinaryTreeNode(1, new BinaryTreeNode(2), rootRight2);
66+
List<Integer> secondResult = depthFirstSearch.inOrder(root2);
67+
68+
//standard tree 2
69+
// 1
70+
// / \
71+
// 2 7
72+
// / \
73+
// 3 5
74+
// / /
75+
// 4 6
76+
List<Integer> thirdList = new ArrayList<>(Arrays.asList(4, 3, 2, 6, 5, 1, 7));
77+
BinaryTreeNode rootLeft3 = new BinaryTreeNode(2, new BinaryTreeNode(3, new BinaryTreeNode(4), null), new BinaryTreeNode(5, new BinaryTreeNode(6), null));
78+
BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7));
79+
List<Integer> thirdResult = depthFirstSearch.inOrder(root3);
80+
81+
assert firstResult.equals(firstList);
82+
assert secondResult.equals(secondList);
83+
assert thirdResult.equals(thirdList);
84+
}
85+
86+
@Test
87+
public void dfs_postOrderTraversal_shouldReturnAccurate() {
88+
// empty tree
89+
List<Integer> firstList = new ArrayList<>();
90+
BinaryTreeNode root1 = null;
91+
List<Integer> firstResult = depthFirstSearch.inOrder(root1);
92+
93+
//standard tree
94+
// 1
95+
// / \
96+
// 2 3
97+
// / \
98+
// 4 5
99+
List<Integer> secondList = new ArrayList<>(Arrays.asList(2, 4, 5, 3, 1));
100+
BinaryTreeNode rootRight2 = new BinaryTreeNode(3, new BinaryTreeNode(4), new BinaryTreeNode(5));
101+
BinaryTreeNode root2 = new BinaryTreeNode(1, new BinaryTreeNode(2), rootRight2);
102+
List<Integer> secondResult = depthFirstSearch.postOrder(root2);
103+
104+
//standard tree 2
105+
// 1
106+
// / \
107+
// 2 7
108+
// / \
109+
// 3 5
110+
// / /
111+
// 4 6
112+
List<Integer> thirdList = new ArrayList<>(Arrays.asList(4, 3, 6, 5, 2, 7, 1));
113+
BinaryTreeNode rootLeft3 = new BinaryTreeNode(2, new BinaryTreeNode(3, new BinaryTreeNode(4), null), new BinaryTreeNode(5, new BinaryTreeNode(6), null));
114+
BinaryTreeNode root3 = new BinaryTreeNode(1, rootLeft3, new BinaryTreeNode(7));
115+
List<Integer> thirdResult = depthFirstSearch.postOrder(root3);
116+
117+
assert firstResult.equals(firstList);
118+
assert secondResult.equals(secondList);
119+
assert thirdResult.equals(thirdList);
120+
}
49121
}

0 commit comments

Comments
 (0)