Co-authored-by: Amit Kumar <akumar@indeed.com>
This commit is contained in:
parent
f35aeb5311
commit
c9e7b219a7
@ -0,0 +1,51 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import com.thealgorithms.datastructures.trees.BinaryTree.Node;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class CeilInBinarySearchTreeTest {
|
||||
|
||||
@Test
|
||||
public void testRootNull() {
|
||||
assertNull(CeilInBinarySearchTree.getCeil(null, 9));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeyPresentRootIsCeil() {
|
||||
final Node root = TreeTestUtils.createTree(new Integer[]{100, 10, 200});
|
||||
assertEquals(100, CeilInBinarySearchTree.getCeil(root, 100).data);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeyPresentLeafIsCeil() {
|
||||
final Node root = TreeTestUtils.createTree(new Integer[]{100, 10, 200});
|
||||
assertEquals(10, CeilInBinarySearchTree.getCeil(root, 10).data);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeyAbsentRootIsCeil() {
|
||||
final Node root = TreeTestUtils.createTree(new Integer[]{100, 10, 200, 5, 50, 150, 300});
|
||||
assertEquals(100, CeilInBinarySearchTree.getCeil(root, 75).data);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeyAbsentLeafIsCeil() {
|
||||
final Node root = TreeTestUtils.createTree(new Integer[]{100, 10, 200, 5, 50, 150, 300});
|
||||
assertEquals(50, CeilInBinarySearchTree.getCeil(root, 40).data);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeyAbsentLeftMostNodeIsCeil() {
|
||||
final Node root = TreeTestUtils.createTree(new Integer[]{100, 10, 200, 5, 50, 150, 300});
|
||||
assertEquals(5, CeilInBinarySearchTree.getCeil(root, 1).data);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeyAbsentCeilIsNull() {
|
||||
final Node root = TreeTestUtils.createTree(new Integer[]{100, 10, 200, 5, 50, 150, 300});
|
||||
assertNull(CeilInBinarySearchTree.getCeil(root, 400));
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import com.thealgorithms.datastructures.trees.BinaryTree.Node;
|
||||
|
||||
public class TreeTestUtils {
|
||||
|
||||
/**
|
||||
* Creates a binary tree with given values
|
||||
*
|
||||
* @param values: Level order representation of tree
|
||||
* @return Root of a binary tree
|
||||
*/
|
||||
public static Node createTree(final Integer[] values) {
|
||||
if (values == null || values.length == 0 || values[0] == null) {
|
||||
throw new IllegalArgumentException("Values array should not be empty or null.");
|
||||
}
|
||||
final Node root = new Node(values[0]);
|
||||
final Queue<Node> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
int end = 1;
|
||||
while (end < values.length) {
|
||||
final Node node = queue.remove();
|
||||
if (values[end] == null) {
|
||||
node.left = null;
|
||||
} else {
|
||||
node.left = new Node(values[end]);
|
||||
queue.add(node.left);
|
||||
}
|
||||
end++;
|
||||
if (end < values.length) {
|
||||
if (values[end] == null) {
|
||||
node.right = null;
|
||||
} else {
|
||||
node.right = new Node(values[end]);
|
||||
queue.add(node.right);
|
||||
}
|
||||
}
|
||||
end++;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user