From 14be9f4ced2d614421673dd477e59cb8e1289e3c Mon Sep 17 00:00:00 2001 From: Albina Gimaletdinova Date: Fri, 13 Jan 2023 12:54:37 +0300 Subject: [PATCH] Vertical order traversal refactoring, added test --- .../trees/VerticalOrderTraversal.java | 32 ++++------- .../trees/VerticalOrderTraversalTest.java | 53 +++++++++++++++++++ 2 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 src/test/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversalTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversal.java b/src/main/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversal.java index f90fab52b3d1..15fd348ea1eb 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversal.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversal.java @@ -22,23 +22,13 @@ */ public class VerticalOrderTraversal { - public static void main(String[] args) { - BinaryTree tree = new BinaryTree(); - tree.put(5); - tree.put(6); - tree.put(3); - tree.put(1); - tree.put(4); - BinaryTree.Node root = tree.getRoot(); - ArrayList ans = verticalTraversal(root); - for (int i : ans) { - System.out.print(i + " "); + /*Function that receives a root Node and prints the tree + in Vertical Order.*/ + public static ArrayList verticalTraversal(BinaryTree.Node root) { + if (root == null) { + return new ArrayList<>(); } - } - /*Function that receives a root Node and prints the tree - in Vertical Order.*/ - private static ArrayList verticalTraversal(BinaryTree.Node root) { /*Queue to store the Nodes.*/ Queue queue = new LinkedList<>(); @@ -84,21 +74,19 @@ private static ArrayList verticalTraversal(BinaryTree.Node root) { to the respective ArrayList present at that index. */ map.get(index.peek()).add(queue.peek().data); - max = (int) Math.max(max, index.peek()); - min = (int) Math.min(min, index.peek()); - /*The Node and its index are removed + max = Math.max(max, index.peek()); + min = Math.min(min, index.peek()); + /*The Node and its index are removed from their respective queues.*/ index.poll(); queue.poll(); } /*Finally map data is printed here which has keys - from min to max. Each ArrayList represents a + from min to max. Each ArrayList represents a vertical column that is added in ans ArrayList.*/ ArrayList ans = new ArrayList<>(); for (int i = min; i <= max; i++) { - for (int j = 0; j < map.get(i).size(); j++) { - ans.add(map.get(i).get(j)); - } + ans.addAll(map.get(i)); } return ans; } diff --git a/src/test/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversalTest.java b/src/test/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversalTest.java new file mode 100644 index 000000000000..42875e4ffa70 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversalTest.java @@ -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 13/01/2023 + */ +public class VerticalOrderTraversalTest { + @Test + public void testRootNull() { + assertEquals(Collections.emptyList(), VerticalOrderTraversal.verticalTraversal(null)); + } + + @Test + public void testSingleNodeTree() { + final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{50}); + assertEquals(List.of(50), VerticalOrderTraversal.verticalTraversal(root)); + } + + /* + 1 + / \ + 2 3 + /\ /\ + 4 5 6 7 + */ + @Test + public void testVerticalTraversalCompleteTree() { + final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7}); + assertEquals(List.of(4, 2, 1, 5, 6, 3, 7), VerticalOrderTraversal.verticalTraversal(root)); + } + + /* + 1 + / \ + 2 3 + /\ /\ + 4 56 7 + / \ + 8 9 + */ + @Test + public void testVerticalTraversalDifferentHeight() { + final BinaryTree.Node root = TreeTestUtils.createTree( + new Integer[]{1, 2, 3, 4, 5, 6, 7, null, null, 8, null, null, 9}); + assertEquals(List.of(4, 2, 8, 1, 5, 6, 3, 9, 7), VerticalOrderTraversal.verticalTraversal(root)); + } +}