Skip to content

Commit 45923d6

Browse files
authored
Add inorder binary tree traversal (TheAlgorithms#3898)
1 parent 6d13d95 commit 45923d6

File tree

3 files changed

+121
-4
lines changed

3 files changed

+121
-4
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Deque;
6+
import java.util.List;
7+
8+
/**
9+
* Given tree is traversed in an 'inorder' way: LEFT -> ROOT -> RIGHT.
10+
* Below are given the recursive and iterative implementations.
11+
*
12+
* Complexities:
13+
* Recursive: O(n) - time, O(n) - space, where 'n' is the number of nodes in a tree.
14+
*
15+
* Iterative: O(n) - time, O(h) - space, where 'n' is the number of nodes in a tree
16+
* and 'h' is the height of a binary tree.
17+
* In the worst case 'h' can be O(n) if tree is completely unbalanced, for instance:
18+
* 5
19+
* \
20+
* 6
21+
* \
22+
* 7
23+
* \
24+
* 8
25+
*
26+
* @author Albina Gimaletdinova on 21/02/2023
27+
*/
28+
public class InorderTraversal {
29+
public static List<Integer> recursiveInorder(BinaryTree.Node root) {
30+
List<Integer> result = new ArrayList<>();
31+
recursiveInorder(root, result);
32+
return result;
33+
}
34+
35+
public static List<Integer> iterativeInorder(BinaryTree.Node root) {
36+
List<Integer> result = new ArrayList<>();
37+
if (root == null) return result;
38+
39+
Deque<BinaryTree.Node> stack = new ArrayDeque<>();
40+
while (!stack.isEmpty() || root != null) {
41+
while (root != null) {
42+
stack.push(root);
43+
root = root.left;
44+
}
45+
root = stack.pop();
46+
result.add(root.data);
47+
root = root.right;
48+
}
49+
return result;
50+
}
51+
52+
private static void recursiveInorder(BinaryTree.Node root, List<Integer> result) {
53+
if (root == null) {
54+
return;
55+
}
56+
recursiveInorder(root.left, result);
57+
result.add(root.data);
58+
recursiveInorder(root.right, result);
59+
}
60+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
/**
11+
* @author Albina Gimaletdinova on 21/02/2023
12+
*/
13+
public class InorderTraversalTest {
14+
@Test
15+
public void testNullRoot() {
16+
assertEquals(Collections.emptyList(), InorderTraversal.recursiveInorder(null));
17+
assertEquals(Collections.emptyList(), InorderTraversal.iterativeInorder(null));
18+
}
19+
20+
/*
21+
1
22+
/ \
23+
2 3
24+
/\ /\
25+
4 5 6 7
26+
*/
27+
@Test
28+
public void testRecursiveInorder() {
29+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
30+
List<Integer> expected = List.of(4, 2, 5, 1, 6, 3, 7);
31+
32+
assertEquals(expected, InorderTraversal.recursiveInorder(root));
33+
assertEquals(expected, InorderTraversal.iterativeInorder(root));
34+
}
35+
36+
/*
37+
5
38+
\
39+
6
40+
\
41+
7
42+
\
43+
8
44+
*/
45+
@Test
46+
public void testRecursiveInorderNonBalanced() {
47+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{5, null, 6, null, 7, null, 8});
48+
List<Integer> expected = List.of(5, 6, 7, 8);
49+
50+
assertEquals(expected, InorderTraversal.recursiveInorder(root));
51+
assertEquals(expected, InorderTraversal.iterativeInorder(root));
52+
}
53+
}

src/test/java/com/thealgorithms/datastructures/trees/PreOrderTraversalTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ public void testNullRoot() {
2727
@Test
2828
public void testRecursivePreOrder() {
2929
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
30-
assertEquals(List.of(1, 2, 4, 5, 3, 6, 7), PreOrderTraversal.recursivePreOrder(root));
31-
assertEquals(List.of(1, 2, 4, 5, 3, 6, 7), PreOrderTraversal.iterativePreOrder(root));
30+
List<Integer> expected = List.of(1, 2, 4, 5, 3, 6, 7);
31+
32+
assertEquals(expected, PreOrderTraversal.recursivePreOrder(root));
33+
assertEquals(expected, PreOrderTraversal.iterativePreOrder(root));
3234
}
3335

3436
/*
@@ -43,7 +45,9 @@ public void testRecursivePreOrder() {
4345
@Test
4446
public void testRecursivePreOrderNonBalanced() {
4547
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{5, null, 6, null, 7, null, 8});
46-
assertEquals(List.of(5, 6, 7, 8), PreOrderTraversal.recursivePreOrder(root));
47-
assertEquals(List.of(5, 6, 7, 8), PreOrderTraversal.iterativePreOrder(root));
48+
List<Integer> expected = List.of(5, 6, 7, 8);
49+
50+
assertEquals(expected, PreOrderTraversal.recursivePreOrder(root));
51+
assertEquals(expected, PreOrderTraversal.iterativePreOrder(root));
4852
}
4953
}

0 commit comments

Comments
 (0)