forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInorderTraversalTest.java
More file actions
52 lines (45 loc) · 1.42 KB
/
InorderTraversalTest.java
File metadata and controls
52 lines (45 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.thealgorithms.datastructures.trees;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
/**
* @author Albina Gimaletdinova on 21/02/2023
*/
public class InorderTraversalTest {
@Test
public void testNullRoot() {
assertEquals(Collections.emptyList(), InorderTraversal.recursiveInorder(null));
assertEquals(Collections.emptyList(), InorderTraversal.iterativeInorder(null));
}
/*
1
/ \
2 3
/\ /\
4 5 6 7
*/
@Test
public void testRecursiveInorder() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, 3, 4, 5, 6, 7});
List<Integer> expected = List.of(4, 2, 5, 1, 6, 3, 7);
assertEquals(expected, InorderTraversal.recursiveInorder(root));
assertEquals(expected, InorderTraversal.iterativeInorder(root));
}
/*
5
\
6
\
7
\
8
*/
@Test
public void testRecursiveInorderNonBalanced() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {5, null, 6, null, 7, null, 8});
List<Integer> expected = List.of(5, 6, 7, 8);
assertEquals(expected, InorderTraversal.recursiveInorder(root));
assertEquals(expected, InorderTraversal.iterativeInorder(root));
}
}