Refactor BSTFromSortedArray (#4162)
This commit is contained in:
parent
c01a382d94
commit
4c18e60671
@ -0,0 +1,33 @@
|
|||||||
|
package com.thealgorithms.datastructures.trees;
|
||||||
|
|
||||||
|
import com.thealgorithms.datastructures.trees.BinaryTree.Node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a sorted array. Create a balanced binary search tree from it.
|
||||||
|
*
|
||||||
|
* Steps: 1. Find the middle element of array. This will act as root 2. Use the
|
||||||
|
* left half recursively to create left subtree 3. Use the right half
|
||||||
|
* recursively to create right subtree
|
||||||
|
*/
|
||||||
|
public class BSTFromSortedArray {
|
||||||
|
public static Node createBST(int[] array) {
|
||||||
|
if (array == null || array.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return createBST(array, 0, array.length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Node createBST(int[] array, int startIdx, int endIdx) {
|
||||||
|
// No element left.
|
||||||
|
if (startIdx > endIdx) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
int mid = startIdx + (endIdx - startIdx) / 2;
|
||||||
|
|
||||||
|
// middle element will be the root
|
||||||
|
Node root = new Node(array[mid]);
|
||||||
|
root.left = createBST(array, startIdx, mid - 1);
|
||||||
|
root.right = createBST(array, mid + 1, endIdx);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
@ -8,7 +8,7 @@ package com.thealgorithms.datastructures.trees;
|
|||||||
* where 'min' and 'max' values represent the child nodes (left, right).
|
* where 'min' and 'max' values represent the child nodes (left, right).
|
||||||
* 2. The smallest possible node value is Integer.MIN_VALUE, the biggest - Integer.MAX_VALUE.
|
* 2. The smallest possible node value is Integer.MIN_VALUE, the biggest - Integer.MAX_VALUE.
|
||||||
*/
|
*/
|
||||||
public class ValidBSTOrNot {
|
public class CheckBinaryTreeIsValidBST {
|
||||||
public static boolean isBST(BinaryTree.Node root) {
|
public static boolean isBST(BinaryTree.Node root) {
|
||||||
return isBSTUtil(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
return isBSTUtil(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||||
}
|
}
|
@ -1,44 +0,0 @@
|
|||||||
package com.thealgorithms.datastructures.trees;
|
|
||||||
|
|
||||||
import com.thealgorithms.datastructures.trees.BinaryTree.Node;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Given a sorted array. Create a balanced binary search tree from it.
|
|
||||||
*
|
|
||||||
* Steps: 1. Find the middle element of array. This will act as root 2. Use the
|
|
||||||
* left half recursively to create left subtree 3. Use the right half
|
|
||||||
* recursively to create right subtree
|
|
||||||
*/
|
|
||||||
public class CreateBSTFromSortedArray {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
test(new int[] {});
|
|
||||||
test(new int[] { 1, 2, 3 });
|
|
||||||
test(new int[] { 1, 2, 3, 4, 5 });
|
|
||||||
test(new int[] { 1, 2, 3, 4, 5, 6, 7 });
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void test(int[] array) {
|
|
||||||
BinaryTree root = new BinaryTree(createBst(array, 0, array.length - 1));
|
|
||||||
System.out.println("\n\nPreorder Traversal: ");
|
|
||||||
root.preOrder(root.getRoot());
|
|
||||||
System.out.println("\nInorder Traversal: ");
|
|
||||||
root.inOrder(root.getRoot());
|
|
||||||
System.out.println("\nPostOrder Traversal: ");
|
|
||||||
root.postOrder(root.getRoot());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Node createBst(int[] array, int start, int end) {
|
|
||||||
// No element left.
|
|
||||||
if (start > end) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
int mid = start + (end - start) / 2;
|
|
||||||
|
|
||||||
// middle element will be the root
|
|
||||||
Node root = new Node(array[mid]);
|
|
||||||
root.left = createBst(array, start, mid - 1);
|
|
||||||
root.right = createBst(array, mid + 1, end);
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
}
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
@ -8,16 +8,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||||||
/**
|
/**
|
||||||
* @author Albina Gimaletdinova on 17/02/2023
|
* @author Albina Gimaletdinova on 17/02/2023
|
||||||
*/
|
*/
|
||||||
public class ValidBSTOrNotTest {
|
public class CheckBinaryTreeIsValidBSTTest {
|
||||||
@Test
|
@Test
|
||||||
public void testRootNull() {
|
public void testRootNull() {
|
||||||
assertTrue(ValidBSTOrNot.isBST(null));
|
assertTrue(CheckBinaryTreeIsValidBST.isBST(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOneNode() {
|
public void testOneNode() {
|
||||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{Integer.MIN_VALUE});
|
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
|
@Test
|
||||||
public void testBinaryTreeIsBST() {
|
public void testBinaryTreeIsBST() {
|
||||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{9, 7, 13, 3, 8, 10, 20});
|
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
|
@Test
|
||||||
public void testBinaryTreeWithDuplicatedNodesIsNotBST() {
|
public void testBinaryTreeWithDuplicatedNodesIsNotBST() {
|
||||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{9, 7, 13, 3, 8, 10, 13});
|
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
|
@Test
|
||||||
public void testBinaryTreeIsNotBST() {
|
public void testBinaryTreeIsNotBST() {
|
||||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{9, 7, 13, 3, 8, 10, 12});
|
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{9, 7, 13, 3, 8, 10, 12});
|
||||||
assertFalse(ValidBSTOrNot.isBST(root));
|
assertFalse(CheckBinaryTreeIsValidBST.isBST(root));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user