Refactor BSTFromSortedArray (#4162)

This commit is contained in:
Albina Gimaletdinova
2023-04-22 10:53:12 +03:00
committed by GitHub
parent c01a382d94
commit 4c18e60671
5 changed files with 87 additions and 51 deletions

View File

@ -0,0 +1,47 @@
package com.thealgorithms.datastructures.trees;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* @author Albina Gimaletdinova on 20/04/2023
*/
public class BSTFromSortedArrayTest {
@Test
public void testNullArray() {
BinaryTree.Node actualBST = BSTFromSortedArray.createBST(null);
Assertions.assertNull(actualBST);
}
@Test
public void testEmptyArray() {
BinaryTree.Node actualBST = BSTFromSortedArray.createBST(new int[]{});
Assertions.assertNull(actualBST);
}
@Test
public void testSingleElementArray() {
BinaryTree.Node actualBST = BSTFromSortedArray.createBST(new int[]{Integer.MIN_VALUE});
Assertions.assertTrue(CheckBinaryTreeIsValidBST.isBST(actualBST));
}
@Test
public void testCreateBSTFromSmallArray() {
BinaryTree.Node actualBST = BSTFromSortedArray.createBST(new int[]{1, 2, 3});
Assertions.assertTrue(CheckBinaryTreeIsValidBST.isBST(actualBST));
}
@Test
public void testCreateBSTFromLongerArray() {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
BinaryTree.Node actualBST = BSTFromSortedArray.createBST(array);
Assertions.assertTrue(CheckBinaryTreeIsValidBST.isBST(actualBST));
}
@Test
public void testShouldNotCreateBSTFromNonSortedArray() {
int[] array = {10, 2, 3, 4, 5, 6, 7, 8, 9, 1};
BinaryTree.Node actualBST = BSTFromSortedArray.createBST(array);
Assertions.assertFalse(CheckBinaryTreeIsValidBST.isBST(actualBST));
}
}

View File

@ -8,16 +8,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Albina Gimaletdinova on 17/02/2023
*/
public class ValidBSTOrNotTest {
public class CheckBinaryTreeIsValidBSTTest {
@Test
public void testRootNull() {
assertTrue(ValidBSTOrNot.isBST(null));
assertTrue(CheckBinaryTreeIsValidBST.isBST(null));
}
@Test
public void testOneNode() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{Integer.MIN_VALUE});
assertTrue(ValidBSTOrNot.isBST(root));
assertTrue(CheckBinaryTreeIsValidBST.isBST(root));
}
/*
@ -30,7 +30,7 @@ public class ValidBSTOrNotTest {
@Test
public void testBinaryTreeIsBST() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{9, 7, 13, 3, 8, 10, 20});
assertTrue(ValidBSTOrNot.isBST(root));
assertTrue(CheckBinaryTreeIsValidBST.isBST(root));
}
/*
@ -43,7 +43,7 @@ public class ValidBSTOrNotTest {
@Test
public void testBinaryTreeWithDuplicatedNodesIsNotBST() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{9, 7, 13, 3, 8, 10, 13});
assertFalse(ValidBSTOrNot.isBST(root));
assertFalse(CheckBinaryTreeIsValidBST.isBST(root));
}
/*
@ -56,6 +56,6 @@ public class ValidBSTOrNotTest {
@Test
public void testBinaryTreeIsNotBST() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{9, 7, 13, 3, 8, 10, 12});
assertFalse(ValidBSTOrNot.isBST(root));
assertFalse(CheckBinaryTreeIsValidBST.isBST(root));
}
}