Skip to content

Commit 3b6e3ed

Browse files
authored
Vertical order traversal refactoring, added unit test (#3844)
Vertical order traversal refactoring, added test
1 parent 5aa417b commit 3b6e3ed

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

src/main/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversal.java

+10-22
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,13 @@
2222
*/
2323
public class VerticalOrderTraversal {
2424

25-
public static void main(String[] args) {
26-
BinaryTree tree = new BinaryTree();
27-
tree.put(5);
28-
tree.put(6);
29-
tree.put(3);
30-
tree.put(1);
31-
tree.put(4);
32-
BinaryTree.Node root = tree.getRoot();
33-
ArrayList<Integer> ans = verticalTraversal(root);
34-
for (int i : ans) {
35-
System.out.print(i + " ");
25+
/*Function that receives a root Node and prints the tree
26+
in Vertical Order.*/
27+
public static ArrayList<Integer> verticalTraversal(BinaryTree.Node root) {
28+
if (root == null) {
29+
return new ArrayList<>();
3630
}
37-
}
3831

39-
/*Function that receives a root Node and prints the tree
40-
in Vertical Order.*/
41-
private static ArrayList<Integer> verticalTraversal(BinaryTree.Node root) {
4232
/*Queue to store the Nodes.*/
4333
Queue<BinaryTree.Node> queue = new LinkedList<>();
4434

@@ -84,21 +74,19 @@ private static ArrayList<Integer> verticalTraversal(BinaryTree.Node root) {
8474
to the respective ArrayList present at that
8575
index. */
8676
map.get(index.peek()).add(queue.peek().data);
87-
max = (int) Math.max(max, index.peek());
88-
min = (int) Math.min(min, index.peek());
89-
/*The Node and its index are removed
77+
max = Math.max(max, index.peek());
78+
min = Math.min(min, index.peek());
79+
/*The Node and its index are removed
9080
from their respective queues.*/
9181
index.poll();
9282
queue.poll();
9383
}
9484
/*Finally map data is printed here which has keys
95-
from min to max. Each ArrayList represents a
85+
from min to max. Each ArrayList represents a
9686
vertical column that is added in ans ArrayList.*/
9787
ArrayList<Integer> ans = new ArrayList<>();
9888
for (int i = min; i <= max; i++) {
99-
for (int j = 0; j < map.get(i).size(); j++) {
100-
ans.add(map.get(i).get(j));
101-
}
89+
ans.addAll(map.get(i));
10290
}
10391
return ans;
10492
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
/**
11+
* @author Albina Gimaletdinova on 13/01/2023
12+
*/
13+
public class VerticalOrderTraversalTest {
14+
@Test
15+
public void testRootNull() {
16+
assertEquals(Collections.emptyList(), VerticalOrderTraversal.verticalTraversal(null));
17+
}
18+
19+
@Test
20+
public void testSingleNodeTree() {
21+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{50});
22+
assertEquals(List.of(50), VerticalOrderTraversal.verticalTraversal(root));
23+
}
24+
25+
/*
26+
1
27+
/ \
28+
2 3
29+
/\ /\
30+
4 5 6 7
31+
*/
32+
@Test
33+
public void testVerticalTraversalCompleteTree() {
34+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
35+
assertEquals(List.of(4, 2, 1, 5, 6, 3, 7), VerticalOrderTraversal.verticalTraversal(root));
36+
}
37+
38+
/*
39+
1
40+
/ \
41+
2 3
42+
/\ /\
43+
4 56 7
44+
/ \
45+
8 9
46+
*/
47+
@Test
48+
public void testVerticalTraversalDifferentHeight() {
49+
final BinaryTree.Node root = TreeTestUtils.createTree(
50+
new Integer[]{1, 2, 3, 4, 5, 6, 7, null, null, 8, null, null, 9});
51+
assertEquals(List.of(4, 2, 8, 1, 5, 6, 3, 9, 7), VerticalOrderTraversal.verticalTraversal(root));
52+
}
53+
}

0 commit comments

Comments
 (0)