Add inorder binary tree traversal (#3898)
This commit is contained in:
parent
6d13d95e41
commit
45923d6872
@ -0,0 +1,60 @@
|
|||||||
|
package com.thealgorithms.datastructures.trees;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given tree is traversed in an 'inorder' way: LEFT -> ROOT -> RIGHT.
|
||||||
|
* Below are given the recursive and iterative implementations.
|
||||||
|
*
|
||||||
|
* Complexities:
|
||||||
|
* Recursive: O(n) - time, O(n) - space, where 'n' is the number of nodes in a tree.
|
||||||
|
*
|
||||||
|
* Iterative: O(n) - time, O(h) - space, where 'n' is the number of nodes in a tree
|
||||||
|
* and 'h' is the height of a binary tree.
|
||||||
|
* In the worst case 'h' can be O(n) if tree is completely unbalanced, for instance:
|
||||||
|
* 5
|
||||||
|
* \
|
||||||
|
* 6
|
||||||
|
* \
|
||||||
|
* 7
|
||||||
|
* \
|
||||||
|
* 8
|
||||||
|
*
|
||||||
|
* @author Albina Gimaletdinova on 21/02/2023
|
||||||
|
*/
|
||||||
|
public class InorderTraversal {
|
||||||
|
public static List<Integer> recursiveInorder(BinaryTree.Node root) {
|
||||||
|
List<Integer> result = new ArrayList<>();
|
||||||
|
recursiveInorder(root, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Integer> iterativeInorder(BinaryTree.Node root) {
|
||||||
|
List<Integer> result = new ArrayList<>();
|
||||||
|
if (root == null) return result;
|
||||||
|
|
||||||
|
Deque<BinaryTree.Node> stack = new ArrayDeque<>();
|
||||||
|
while (!stack.isEmpty() || root != null) {
|
||||||
|
while (root != null) {
|
||||||
|
stack.push(root);
|
||||||
|
root = root.left;
|
||||||
|
}
|
||||||
|
root = stack.pop();
|
||||||
|
result.add(root.data);
|
||||||
|
root = root.right;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void recursiveInorder(BinaryTree.Node root, List<Integer> result) {
|
||||||
|
if (root == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
recursiveInorder(root.left, result);
|
||||||
|
result.add(root.data);
|
||||||
|
recursiveInorder(root.right, result);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.thealgorithms.datastructures.trees;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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));
|
||||||
|
}
|
||||||
|
}
|
@ -27,8 +27,10 @@ public class PreOrderTraversalTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testRecursivePreOrder() {
|
public void testRecursivePreOrder() {
|
||||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
|
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
|
||||||
assertEquals(List.of(1, 2, 4, 5, 3, 6, 7), PreOrderTraversal.recursivePreOrder(root));
|
List<Integer> expected = List.of(1, 2, 4, 5, 3, 6, 7);
|
||||||
assertEquals(List.of(1, 2, 4, 5, 3, 6, 7), PreOrderTraversal.iterativePreOrder(root));
|
|
||||||
|
assertEquals(expected, PreOrderTraversal.recursivePreOrder(root));
|
||||||
|
assertEquals(expected, PreOrderTraversal.iterativePreOrder(root));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -43,7 +45,9 @@ public class PreOrderTraversalTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testRecursivePreOrderNonBalanced() {
|
public void testRecursivePreOrderNonBalanced() {
|
||||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{5, null, 6, null, 7, null, 8});
|
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{5, null, 6, null, 7, null, 8});
|
||||||
assertEquals(List.of(5, 6, 7, 8), PreOrderTraversal.recursivePreOrder(root));
|
List<Integer> expected = List.of(5, 6, 7, 8);
|
||||||
assertEquals(List.of(5, 6, 7, 8), PreOrderTraversal.iterativePreOrder(root));
|
|
||||||
|
assertEquals(expected, PreOrderTraversal.recursivePreOrder(root));
|
||||||
|
assertEquals(expected, PreOrderTraversal.iterativePreOrder(root));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user