Skip to content

Added recursive&iterative postorder binary tree traversal #3899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}