Add postorder binary tree traversal (#3899)

This commit is contained in:
Albina Gimaletdinova 2023-02-27 15:06:39 +03:00 committed by GitHub
parent b98dc2c5b5
commit 3499c1bee6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,62 @@
package com.thealgorithms.datastructures.trees;
import java.util.*;
/**
* Given tree is traversed in a 'post-order' way: LEFT -> RIGHT -> ROOT.
* Below are given the recursive and iterative implementations.
* <p>
* Complexities:
* Recursive: O(n) - time, O(n) - space, where 'n' is the number of nodes in a tree.
* <p>
* 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 PostOrderTraversal {
public static List<Integer> recursivePostOrder(BinaryTree.Node root) {
List<Integer> result = new ArrayList<>();
recursivePostOrder(root, result);
return result;
}
public static List<Integer> iterativePostOrder(BinaryTree.Node root) {
LinkedList<Integer> result = new LinkedList<>();
if (root == null) {
return result;
}
Deque<BinaryTree.Node> stack = new ArrayDeque<>();
stack.push(root);
while (!stack.isEmpty()) {
BinaryTree.Node node = stack.pop();
result.addFirst(node.data);
if (node.left != null) {
stack.push(node.left);
}
if (node.right != null) {
stack.push(node.right);
}
}
return result;
}
private static void recursivePostOrder(BinaryTree.Node root, List<Integer> result) {
if (root == null) {
return;
}
recursivePostOrder(root.left, result);
recursivePostOrder(root.right, result);
result.add(root.data);
}
}

View File

@ -0,0 +1,55 @@
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;
/**
* Given tree is traversed in a 'post-order' way: LEFT -> RIGHT -> ROOT.
*
* @author Albina Gimaletdinova on 21/02/2023
*/
public class PostOrderTraversalTest {
@Test
public void testNullRoot() {
assertEquals(Collections.emptyList(), PostOrderTraversal.recursivePostOrder(null));
assertEquals(Collections.emptyList(), PostOrderTraversal.iterativePostOrder(null));
}
/*
1
/ \
2 3
/\ /\
4 5 6 7
*/
@Test
public void testPostOrder() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
List<Integer> expected = List.of(4, 5, 2, 6, 7, 3, 1);
assertEquals(expected, PostOrderTraversal.recursivePostOrder(root));
assertEquals(expected, PostOrderTraversal.iterativePostOrder(root));
}
/*
5
\
6
\
7
\
8
*/
@Test
public void testPostOrderNonBalanced() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{5, null, 6, null, 7, null, 8});
List<Integer> expected = List.of(8, 7, 6, 5);
assertEquals(expected, PostOrderTraversal.recursivePostOrder(root));
assertEquals(expected, PostOrderTraversal.iterativePostOrder(root));
}
}