mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-31 16:56:40 +08:00
@ -15,19 +15,19 @@ public class BSTFromSortedArrayTest {
|
||||
|
||||
@Test
|
||||
public void testEmptyArray() {
|
||||
BinaryTree.Node actualBST = BSTFromSortedArray.createBST(new int[]{});
|
||||
BinaryTree.Node actualBST = BSTFromSortedArray.createBST(new int[] {});
|
||||
Assertions.assertNull(actualBST);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleElementArray() {
|
||||
BinaryTree.Node actualBST = BSTFromSortedArray.createBST(new int[]{Integer.MIN_VALUE});
|
||||
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});
|
||||
BinaryTree.Node actualBST = BSTFromSortedArray.createBST(new int[] {1, 2, 3});
|
||||
Assertions.assertTrue(CheckBinaryTreeIsValidBST.isBST(actualBST));
|
||||
}
|
||||
|
||||
|
@ -6,73 +6,71 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
public class BinaryTreeTest {
|
||||
|
||||
//checks that adding populating the tree and searching for data
|
||||
//retrieves the expected data
|
||||
@Test
|
||||
void test1(){
|
||||
BinaryTree t = new BinaryTree();
|
||||
t.put(3);
|
||||
t.put(5);
|
||||
t.put(7);
|
||||
t.put(9);
|
||||
t.put(12);
|
||||
|
||||
// checks that adding populating the tree and searching for data
|
||||
// retrieves the expected data
|
||||
@Test
|
||||
void test1() {
|
||||
BinaryTree t = new BinaryTree();
|
||||
t.put(3);
|
||||
t.put(5);
|
||||
t.put(7);
|
||||
t.put(9);
|
||||
t.put(12);
|
||||
|
||||
assertEquals(t.find(5).data, 5);
|
||||
assertEquals(t.find(7).data, 7);
|
||||
}
|
||||
|
||||
//checks that removing data from the tree
|
||||
//properly removes and makes the new root the expected new root
|
||||
@Test
|
||||
void test2(){
|
||||
BinaryTree t = new BinaryTree();
|
||||
t.put(3);
|
||||
t.put(5);
|
||||
t.put(7);
|
||||
t.put(9);
|
||||
t.put(12);
|
||||
t.remove(3);
|
||||
t.remove(5);
|
||||
t.remove(7);
|
||||
|
||||
|
||||
assertEquals(t.getRoot().data, 9);
|
||||
}
|
||||
|
||||
//checks that removing an unexistend node returns false
|
||||
// as specified by the documentation of the function
|
||||
@Test
|
||||
void test3(){
|
||||
BinaryTree t = new BinaryTree();
|
||||
t.put(3);
|
||||
t.put(5);
|
||||
t.put(7);
|
||||
t.put(9);
|
||||
t.put(12);
|
||||
|
||||
assertEquals(t.remove(9), true);
|
||||
assertEquals(t.remove(398745987), false);
|
||||
}
|
||||
|
||||
//check if the bfs, inOrder, preOrder and postOrder functions
|
||||
//worg as expected, also increases the coverage measures in
|
||||
//JaCoCo
|
||||
@Test
|
||||
void test4(){
|
||||
BinaryTree t = new BinaryTree();
|
||||
t.put(3);
|
||||
t.put(5);
|
||||
t.put(7);
|
||||
t.put(9);
|
||||
t.put(12);
|
||||
|
||||
t.bfs(t.find(12));
|
||||
t.inOrder(t.getRoot());
|
||||
t.preOrder(t.getRoot());
|
||||
t.postOrder(t.getRoot());
|
||||
|
||||
assertEquals(t.remove(9), true);
|
||||
assertEquals(t.remove(398745987), false);
|
||||
}
|
||||
assertEquals(t.find(5).data, 5);
|
||||
assertEquals(t.find(7).data, 7);
|
||||
}
|
||||
|
||||
// checks that removing data from the tree
|
||||
// properly removes and makes the new root the expected new root
|
||||
@Test
|
||||
void test2() {
|
||||
BinaryTree t = new BinaryTree();
|
||||
t.put(3);
|
||||
t.put(5);
|
||||
t.put(7);
|
||||
t.put(9);
|
||||
t.put(12);
|
||||
t.remove(3);
|
||||
t.remove(5);
|
||||
t.remove(7);
|
||||
|
||||
assertEquals(t.getRoot().data, 9);
|
||||
}
|
||||
|
||||
// checks that removing an unexistend node returns false
|
||||
// as specified by the documentation of the function
|
||||
@Test
|
||||
void test3() {
|
||||
BinaryTree t = new BinaryTree();
|
||||
t.put(3);
|
||||
t.put(5);
|
||||
t.put(7);
|
||||
t.put(9);
|
||||
t.put(12);
|
||||
|
||||
assertEquals(t.remove(9), true);
|
||||
assertEquals(t.remove(398745987), false);
|
||||
}
|
||||
|
||||
// check if the bfs, inOrder, preOrder and postOrder functions
|
||||
// worg as expected, also increases the coverage measures in
|
||||
// JaCoCo
|
||||
@Test
|
||||
void test4() {
|
||||
BinaryTree t = new BinaryTree();
|
||||
t.put(3);
|
||||
t.put(5);
|
||||
t.put(7);
|
||||
t.put(9);
|
||||
t.put(12);
|
||||
|
||||
t.bfs(t.find(12));
|
||||
t.inOrder(t.getRoot());
|
||||
t.preOrder(t.getRoot());
|
||||
t.postOrder(t.getRoot());
|
||||
|
||||
assertEquals(t.remove(9), true);
|
||||
assertEquals(t.remove(398745987), false);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
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;
|
||||
|
||||
import com.thealgorithms.datastructures.trees.BinaryTree.Node;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CeilInBinarySearchTreeTest {
|
||||
|
||||
@Test
|
||||
@ -15,37 +15,37 @@ public class CeilInBinarySearchTreeTest {
|
||||
|
||||
@Test
|
||||
public void testKeyPresentRootIsCeil() {
|
||||
final Node root = TreeTestUtils.createTree(new Integer[]{100, 10, 200});
|
||||
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});
|
||||
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});
|
||||
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});
|
||||
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});
|
||||
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});
|
||||
final Node root = TreeTestUtils.createTree(new Integer[] {100, 10, 200, 5, 50, 150, 300});
|
||||
assertNull(CeilInBinarySearchTree.getCeil(root, 400));
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Albina Gimaletdinova on 17/02/2023
|
||||
*/
|
||||
@ -16,7 +16,7 @@ public class CheckBinaryTreeIsValidBSTTest {
|
||||
|
||||
@Test
|
||||
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(CheckBinaryTreeIsValidBST.isBST(root));
|
||||
}
|
||||
|
||||
@ -29,7 +29,8 @@ public class CheckBinaryTreeIsValidBSTTest {
|
||||
*/
|
||||
@Test
|
||||
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(CheckBinaryTreeIsValidBST.isBST(root));
|
||||
}
|
||||
|
||||
@ -42,7 +43,8 @@ public class CheckBinaryTreeIsValidBSTTest {
|
||||
*/
|
||||
@Test
|
||||
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(CheckBinaryTreeIsValidBST.isBST(root));
|
||||
}
|
||||
|
||||
@ -55,7 +57,8 @@ public class CheckBinaryTreeIsValidBSTTest {
|
||||
*/
|
||||
@Test
|
||||
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(CheckBinaryTreeIsValidBST.isBST(root));
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author kumanoit on 10/10/22 IST 1:02 AM
|
||||
*/
|
||||
@ -17,20 +17,19 @@ public class CheckTreeIsSymmetricTest {
|
||||
|
||||
@Test
|
||||
public void testSingleNodeTree() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{100});
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {100});
|
||||
assertTrue(CheckTreeIsSymmetric.isSymmetric(root));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSymmetricTree() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1,2,2,3,4,4,3});
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, 2, 3, 4, 4, 3});
|
||||
assertTrue(CheckTreeIsSymmetric.isSymmetric(root));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonSymmetricTree() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1,2,2,3,5,4,3});
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, 2, 3, 5, 4, 3});
|
||||
assertFalse(CheckTreeIsSymmetric.isSymmetric(root));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Albina Gimaletdinova on 14/05/2023
|
||||
*/
|
||||
@ -13,7 +12,8 @@ public class CreateBinaryTreeFromInorderPreorderTest {
|
||||
public void testOnNullArraysShouldReturnNullTree() {
|
||||
// when
|
||||
BinaryTree.Node root = CreateBinaryTreeFromInorderPreorder.createTree(null, null);
|
||||
BinaryTree.Node rootOpt = CreateBinaryTreeFromInorderPreorder.createTreeOptimized(null, null);
|
||||
BinaryTree.Node rootOpt
|
||||
= CreateBinaryTreeFromInorderPreorder.createTreeOptimized(null, null);
|
||||
|
||||
// then
|
||||
Assertions.assertNull(root);
|
||||
@ -28,7 +28,8 @@ public class CreateBinaryTreeFromInorderPreorderTest {
|
||||
|
||||
// when
|
||||
BinaryTree.Node root = CreateBinaryTreeFromInorderPreorder.createTree(preorder, inorder);
|
||||
BinaryTree.Node rootOpt = CreateBinaryTreeFromInorderPreorder.createTreeOptimized(preorder, inorder);
|
||||
BinaryTree.Node rootOpt
|
||||
= CreateBinaryTreeFromInorderPreorder.createTreeOptimized(preorder, inorder);
|
||||
|
||||
// then
|
||||
Assertions.assertNull(root);
|
||||
@ -43,7 +44,8 @@ public class CreateBinaryTreeFromInorderPreorderTest {
|
||||
|
||||
// when
|
||||
BinaryTree.Node root = CreateBinaryTreeFromInorderPreorder.createTree(preorder, inorder);
|
||||
BinaryTree.Node rootOpt = CreateBinaryTreeFromInorderPreorder.createTreeOptimized(preorder, inorder);
|
||||
BinaryTree.Node rootOpt
|
||||
= CreateBinaryTreeFromInorderPreorder.createTreeOptimized(preorder, inorder);
|
||||
|
||||
// then
|
||||
checkTree(preorder, inorder, root);
|
||||
@ -58,7 +60,8 @@ public class CreateBinaryTreeFromInorderPreorderTest {
|
||||
|
||||
// when
|
||||
BinaryTree.Node root = CreateBinaryTreeFromInorderPreorder.createTree(preorder, inorder);
|
||||
BinaryTree.Node rootOpt = CreateBinaryTreeFromInorderPreorder.createTreeOptimized(preorder, inorder);
|
||||
BinaryTree.Node rootOpt
|
||||
= CreateBinaryTreeFromInorderPreorder.createTreeOptimized(preorder, inorder);
|
||||
|
||||
// then
|
||||
checkTree(preorder, inorder, root);
|
||||
@ -73,7 +76,8 @@ public class CreateBinaryTreeFromInorderPreorderTest {
|
||||
|
||||
// when
|
||||
BinaryTree.Node root = CreateBinaryTreeFromInorderPreorder.createTree(preorder, inorder);
|
||||
BinaryTree.Node rootOpt = CreateBinaryTreeFromInorderPreorder.createTreeOptimized(preorder, inorder);
|
||||
BinaryTree.Node rootOpt
|
||||
= CreateBinaryTreeFromInorderPreorder.createTreeOptimized(preorder, inorder);
|
||||
|
||||
// then
|
||||
checkTree(preorder, inorder, root);
|
||||
@ -88,7 +92,8 @@ public class CreateBinaryTreeFromInorderPreorderTest {
|
||||
|
||||
// when
|
||||
BinaryTree.Node root = CreateBinaryTreeFromInorderPreorder.createTree(preorder, inorder);
|
||||
BinaryTree.Node rootOpt = CreateBinaryTreeFromInorderPreorder.createTreeOptimized(preorder, inorder);
|
||||
BinaryTree.Node rootOpt
|
||||
= CreateBinaryTreeFromInorderPreorder.createTreeOptimized(preorder, inorder);
|
||||
|
||||
// then
|
||||
checkTree(preorder, inorder, root);
|
||||
|
@ -1,11 +1,10 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Albina Gimaletdinova on 21/02/2023
|
||||
@ -26,7 +25,7 @@ public class InorderTraversalTest {
|
||||
*/
|
||||
@Test
|
||||
public void testRecursiveInorder() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, 3, 4, 5, 6, 7});
|
||||
List<Integer> expected = List.of(4, 2, 5, 1, 6, 3, 7);
|
||||
|
||||
assertEquals(expected, InorderTraversal.recursiveInorder(root));
|
||||
@ -44,7 +43,8 @@ public class InorderTraversalTest {
|
||||
*/
|
||||
@Test
|
||||
public void testRecursiveInorderNonBalanced() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{5, null, 6, null, 7, null, 8});
|
||||
final BinaryTree.Node root
|
||||
= TreeTestUtils.createTree(new Integer[] {5, null, 6, null, 7, null, 8});
|
||||
List<Integer> expected = List.of(5, 6, 7, 8);
|
||||
|
||||
assertEquals(expected, InorderTraversal.recursiveInorder(root));
|
||||
|
@ -7,18 +7,18 @@ import org.junit.jupiter.api.Test;
|
||||
public class KDTreeTest {
|
||||
|
||||
KDTree.Point pointOf(int x, int y) {
|
||||
return new KDTree.Point(new int[] { x, y });
|
||||
return new KDTree.Point(new int[] {x, y});
|
||||
}
|
||||
|
||||
@Test
|
||||
void findMin() {
|
||||
int[][] coordinates = {
|
||||
{ 30, 40 },
|
||||
{ 5, 25 },
|
||||
{ 70, 70 },
|
||||
{ 10, 12 },
|
||||
{ 50, 30 },
|
||||
{ 35, 45 },
|
||||
{30, 40},
|
||||
{5, 25},
|
||||
{70, 70},
|
||||
{10, 12},
|
||||
{50, 30},
|
||||
{35, 45},
|
||||
};
|
||||
KDTree kdTree = new KDTree(coordinates);
|
||||
|
||||
@ -29,12 +29,12 @@ public class KDTreeTest {
|
||||
@Test
|
||||
void delete() {
|
||||
int[][] coordinates = {
|
||||
{ 30, 40 },
|
||||
{ 5, 25 },
|
||||
{ 70, 70 },
|
||||
{ 10, 12 },
|
||||
{ 50, 30 },
|
||||
{ 35, 45 },
|
||||
{30, 40},
|
||||
{5, 25},
|
||||
{70, 70},
|
||||
{10, 12},
|
||||
{50, 30},
|
||||
{35, 45},
|
||||
};
|
||||
KDTree kdTree = new KDTree(coordinates);
|
||||
|
||||
@ -46,12 +46,12 @@ public class KDTreeTest {
|
||||
@Test
|
||||
void findNearest() {
|
||||
int[][] coordinates = {
|
||||
{ 2, 3 },
|
||||
{ 5, 4 },
|
||||
{ 9, 6 },
|
||||
{ 4, 7 },
|
||||
{ 8, 1 },
|
||||
{ 7, 2 },
|
||||
{2, 3},
|
||||
{5, 4},
|
||||
{9, 6},
|
||||
{4, 7},
|
||||
{8, 1},
|
||||
{7, 2},
|
||||
};
|
||||
KDTree kdTree = new KDTree(coordinates);
|
||||
|
||||
|
@ -8,7 +8,7 @@ public class LazySegmentTreeTest {
|
||||
|
||||
@Test
|
||||
void build() {
|
||||
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
LazySegmentTree lazySegmentTree = new LazySegmentTree(arr);
|
||||
assertEquals(55, lazySegmentTree.getRoot().getValue());
|
||||
assertEquals(15, lazySegmentTree.getRoot().getLeft().getValue());
|
||||
@ -17,7 +17,7 @@ public class LazySegmentTreeTest {
|
||||
|
||||
@Test
|
||||
void update() {
|
||||
int[] arr = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
|
||||
int[] arr = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
|
||||
LazySegmentTree lazySegmentTree = new LazySegmentTree(arr);
|
||||
assertEquals(10, lazySegmentTree.getRoot().getValue());
|
||||
|
||||
@ -36,7 +36,7 @@ public class LazySegmentTreeTest {
|
||||
|
||||
@Test
|
||||
void get() {
|
||||
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
LazySegmentTree lazySegmentTree = new LazySegmentTree(arr);
|
||||
assertEquals(55, lazySegmentTree.getRange(0, 10));
|
||||
assertEquals(3, lazySegmentTree.getRange(0, 2));
|
||||
@ -46,14 +46,15 @@ public class LazySegmentTreeTest {
|
||||
|
||||
@Test
|
||||
void updateAndGet() {
|
||||
int[] arr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
int[] arr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
LazySegmentTree lazySegmentTree = new LazySegmentTree(arr);
|
||||
|
||||
for (int i = 0; i < 10; i++) for (int j = i + 1; j < 10; j++) {
|
||||
lazySegmentTree.updateRange(i, j, 1);
|
||||
assertEquals(j - i, lazySegmentTree.getRange(i, j));
|
||||
lazySegmentTree.updateRange(i, j, -1);
|
||||
assertEquals(0, lazySegmentTree.getRange(i, j));
|
||||
}
|
||||
for (int i = 0; i < 10; i++)
|
||||
for (int j = i + 1; j < 10; j++) {
|
||||
lazySegmentTree.updateRange(i, j, 1);
|
||||
assertEquals(j - i, lazySegmentTree.getRange(i, j));
|
||||
lazySegmentTree.updateRange(i, j, -1);
|
||||
assertEquals(0, lazySegmentTree.getRange(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,10 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Albina Gimaletdinova on 08/02/2023
|
||||
@ -18,7 +17,7 @@ public class LevelOrderTraversalTest {
|
||||
|
||||
@Test
|
||||
public void testSingleNodeTree() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{50});
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {50});
|
||||
assertEquals(List.of(List.of(50)), LevelOrderTraversal.traverse(root));
|
||||
}
|
||||
|
||||
@ -31,8 +30,9 @@ public class LevelOrderTraversalTest {
|
||||
*/
|
||||
@Test
|
||||
public void testLevelOrderTraversalCompleteTree() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
|
||||
assertEquals(List.of(List.of(1), List.of(2, 3), List.of(4, 5, 6, 7)), LevelOrderTraversal.traverse(root));
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, 3, 4, 5, 6, 7});
|
||||
assertEquals(List.of(List.of(1), List.of(2, 3), List.of(4, 5, 6, 7)),
|
||||
LevelOrderTraversal.traverse(root));
|
||||
}
|
||||
|
||||
/*
|
||||
@ -47,8 +47,8 @@ public class LevelOrderTraversalTest {
|
||||
@Test
|
||||
public void testLevelOrderTraversalDifferentHeight() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(
|
||||
new Integer[]{1, 2, 3, 4, 5, 6, 7, null, null, 8, null, null, 9});
|
||||
new Integer[] {1, 2, 3, 4, 5, 6, 7, null, null, 8, null, null, 9});
|
||||
assertEquals(List.of(List.of(1), List.of(2, 3), List.of(4, 5, 6, 7), List.of(8, 9)),
|
||||
LevelOrderTraversal.traverse(root));
|
||||
LevelOrderTraversal.traverse(root));
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,10 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Given tree is traversed in a 'post-order' way: LEFT -> RIGHT -> ROOT.
|
||||
@ -28,7 +27,7 @@ public class PostOrderTraversalTest {
|
||||
*/
|
||||
@Test
|
||||
public void testPostOrder() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, 3, 4, 5, 6, 7});
|
||||
List<Integer> expected = List.of(4, 5, 2, 6, 7, 3, 1);
|
||||
|
||||
assertEquals(expected, PostOrderTraversal.recursivePostOrder(root));
|
||||
@ -46,7 +45,8 @@ public class PostOrderTraversalTest {
|
||||
*/
|
||||
@Test
|
||||
public void testPostOrderNonBalanced() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{5, null, 6, null, 7, null, 8});
|
||||
final BinaryTree.Node root
|
||||
= TreeTestUtils.createTree(new Integer[] {5, null, 6, null, 7, null, 8});
|
||||
List<Integer> expected = List.of(8, 7, 6, 5);
|
||||
|
||||
assertEquals(expected, PostOrderTraversal.recursivePostOrder(root));
|
||||
|
@ -1,11 +1,10 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Albina Gimaletdinova on 17/02/2023
|
||||
@ -26,7 +25,7 @@ public class PreOrderTraversalTest {
|
||||
*/
|
||||
@Test
|
||||
public void testRecursivePreOrder() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, 3, 4, 5, 6, 7});
|
||||
List<Integer> expected = List.of(1, 2, 4, 5, 3, 6, 7);
|
||||
|
||||
assertEquals(expected, PreOrderTraversal.recursivePreOrder(root));
|
||||
@ -44,7 +43,8 @@ public class PreOrderTraversalTest {
|
||||
*/
|
||||
@Test
|
||||
public void testRecursivePreOrderNonBalanced() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{5, null, 6, null, 7, null, 8});
|
||||
final BinaryTree.Node root
|
||||
= TreeTestUtils.createTree(new Integer[] {5, null, 6, null, 7, null, 8});
|
||||
List<Integer> expected = List.of(5, 6, 7, 8);
|
||||
|
||||
assertEquals(expected, PreOrderTraversal.recursivePreOrder(root));
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Albina Gimaletdinova on 12/01/2023
|
||||
*/
|
||||
@ -16,14 +16,14 @@ public class SameTreesCheckTest {
|
||||
|
||||
@Test
|
||||
public void testOneRootIsNull() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{100});
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {100});
|
||||
assertFalse(SameTreesCheck.check(root, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleNodeTreesAreSame() {
|
||||
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[]{100});
|
||||
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[]{100});
|
||||
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[] {100});
|
||||
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[] {100});
|
||||
assertTrue(SameTreesCheck.check(p, q));
|
||||
}
|
||||
|
||||
@ -36,12 +36,11 @@ public class SameTreesCheckTest {
|
||||
*/
|
||||
@Test
|
||||
public void testSameTreesIsSuccessful() {
|
||||
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
|
||||
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
|
||||
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[] {1, 2, 3, 4, 5, 6, 7});
|
||||
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[] {1, 2, 3, 4, 5, 6, 7});
|
||||
assertTrue(SameTreesCheck.check(p, q));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
1 1
|
||||
/ \ / \
|
||||
@ -51,8 +50,8 @@ public class SameTreesCheckTest {
|
||||
*/
|
||||
@Test
|
||||
public void testSameTreesFails() {
|
||||
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
|
||||
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6});
|
||||
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[] {1, 2, 3, 4, 5, 6, 7});
|
||||
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[] {1, 2, 3, 4, 5, 6});
|
||||
assertFalse(SameTreesCheck.check(p, q));
|
||||
}
|
||||
|
||||
@ -63,9 +62,8 @@ public class SameTreesCheckTest {
|
||||
*/
|
||||
@Test
|
||||
public void testTreesWithDifferentStructure() {
|
||||
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[]{1, 2});
|
||||
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[]{1, null, 2});
|
||||
final BinaryTree.Node p = TreeTestUtils.createTree(new Integer[] {1, 2});
|
||||
final BinaryTree.Node q = TreeTestUtils.createTree(new Integer[] {1, null, 2});
|
||||
assertFalse(SameTreesCheck.check(p, q));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import com.thealgorithms.datastructures.trees.BinaryTree.Node;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import com.thealgorithms.datastructures.trees.BinaryTree.Node;
|
||||
|
||||
public class TreeTestUtils {
|
||||
|
||||
|
@ -1,11 +1,10 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Albina Gimaletdinova on 13/01/2023
|
||||
@ -18,7 +17,7 @@ public class VerticalOrderTraversalTest {
|
||||
|
||||
@Test
|
||||
public void testSingleNodeTree() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{50});
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {50});
|
||||
assertEquals(List.of(50), VerticalOrderTraversal.verticalTraversal(root));
|
||||
}
|
||||
|
||||
@ -31,7 +30,7 @@ public class VerticalOrderTraversalTest {
|
||||
*/
|
||||
@Test
|
||||
public void testVerticalTraversalCompleteTree() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
|
||||
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));
|
||||
}
|
||||
|
||||
@ -47,7 +46,8 @@ public class VerticalOrderTraversalTest {
|
||||
@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));
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,10 @@
|
||||
package com.thealgorithms.datastructures.trees;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Albina Gimaletdinova on 11/01/2023
|
||||
@ -18,7 +17,7 @@ public class ZigzagTraversalTest {
|
||||
|
||||
@Test
|
||||
public void testSingleNodeTree() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{50});
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {50});
|
||||
assertEquals(List.of(List.of(50)), ZigzagTraversal.traverse(root));
|
||||
}
|
||||
|
||||
@ -31,8 +30,9 @@ public class ZigzagTraversalTest {
|
||||
*/
|
||||
@Test
|
||||
public void testZigzagTraversalCompleteTree() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
|
||||
assertEquals(List.of(List.of(1), List.of(3, 2), List.of(4, 5, 6, 7)), ZigzagTraversal.traverse(root));
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, 3, 4, 5, 6, 7});
|
||||
assertEquals(List.of(List.of(1), List.of(3, 2), List.of(4, 5, 6, 7)),
|
||||
ZigzagTraversal.traverse(root));
|
||||
}
|
||||
|
||||
/*
|
||||
@ -47,8 +47,8 @@ public class ZigzagTraversalTest {
|
||||
@Test
|
||||
public void testZigzagTraversalDifferentHeight() {
|
||||
final BinaryTree.Node root = TreeTestUtils.createTree(
|
||||
new Integer[]{1, 2, 3, 4, 5, 6, 7, null, null, 8, null, null, 9});
|
||||
new Integer[] {1, 2, 3, 4, 5, 6, 7, null, null, 8, null, null, 9});
|
||||
assertEquals(List.of(List.of(1), List.of(3, 2), List.of(4, 5, 6, 7), List.of(9, 8)),
|
||||
ZigzagTraversal.traverse(root));
|
||||
ZigzagTraversal.traverse(root));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user