Vertical order traversal refactoring, added unit test (#3844)

Vertical order traversal refactoring, added test
This commit is contained in:
Albina Gimaletdinova
2023-01-13 16:56:15 +03:00
committed by GitHub
parent 5aa417b6ae
commit 3b6e3edbfb
2 changed files with 64 additions and 23 deletions

View File

@ -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));
}
}