Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup combination #5901

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/main/java/com/thealgorithms/backtracking/Combination.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.thealgorithms.backtracking;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;
Expand All @@ -13,8 +14,6 @@ public final class Combination {
private Combination() {
}

private static int length;

/**
* Find all combinations of given array using backtracking
* @param arr the array.
Expand All @@ -23,39 +22,45 @@ private Combination() {
* @return a list of all combinations of length n. If n == 0, return null.
*/
public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
if (n < 0) {
throw new IllegalArgumentException("The combination length cannot be negative.");
}

if (n == 0) {
return null;
return Collections.emptyList();
}
length = n;
T[] array = arr.clone();
Arrays.sort(array);

List<TreeSet<T>> result = new LinkedList<>();
backtracking(array, 0, new TreeSet<T>(), result);
backtracking(array, n, 0, new TreeSet<T>(), result);
return result;
}

/**
* Backtrack all possible combinations of a given array
* @param arr the array.
* @param n length of the combination
* @param index the starting index.
* @param currSet set that tracks current combination
* @param result the list contains all combination.
* @param <T> the type of elements in the array.
*/
private static <T> void backtracking(T[] arr, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
if (index + length - currSet.size() > arr.length) {
private static <T> void backtracking(T[] arr, int n, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
if (index + n - currSet.size() > arr.length) {
return;
}
if (length - 1 == currSet.size()) {
if (currSet.size() == n - 1) {
for (int i = index; i < arr.length; i++) {
currSet.add(arr[i]);
result.add((TreeSet<T>) currSet.clone());
result.add(new TreeSet<>(currSet));
currSet.remove(arr[i]);
}
return;
}
for (int i = index; i < arr.length; i++) {
currSet.add(arr[i]);
backtracking(arr, i + 1, currSet, result);
backtracking(arr, n, i + 1, currSet, result);
currSet.remove(arr[i]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public class GenericHeap<T extends Comparable<T>> {
HashMap<T, Integer> map = new HashMap<>();

public void add(T item) {
if (item == null) {
throw new IllegalArgumentException("Cannot insert null into the heap.");
}

this.data.add(item);
map.put(item, this.data.size() - 1); //
upHeapify(this.data.size() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public static void main(String[] args) {
* @param listC the result list after merging
*/
public static void merge(List<Integer> listA, List<Integer> listB, List<Integer> listC) {
if (listA == null || listB == null || listC == null) {
throw new NullPointerException("Lists cannot be null.");
}

int pa = 0;
/* the index of listA */
int pb = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package com.thealgorithms.backtracking;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.junit.jupiter.api.Test;

public class CombinationTest {

@Test
void testNegativeElement() {
Integer[] array = {1, 2};
assertThrows(IllegalArgumentException.class, () -> { Combination.combination(array, -1); });
}

@Test
void testNoElement() {
List<TreeSet<Integer>> result = Combination.combination(new Integer[] {1, 2}, 0);
assertTrue(result == null);
assertNotNull(result);
assertEquals(0, result.size());
}

@Test
Expand All @@ -28,4 +39,13 @@ void testLengthTwo() {
assertTrue(arr[0] == 1);
assertTrue(arr[1] == 2);
}

@Test
void testCombinationsWithStrings() {
List<TreeSet<String>> result = Combination.combination(new String[] {"a", "b", "c"}, 2);
assertEquals(3, result.size());
assertTrue(result.contains(new TreeSet<>(Set.of("a", "b"))));
assertTrue(result.contains(new TreeSet<>(Set.of("a", "c"))));
assertTrue(result.contains(new TreeSet<>(Set.of("b", "c"))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.thealgorithms.datastructures.heaps;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class GenericHeapTest {

private GenericHeap<Integer> heap;

@BeforeEach
public void setUp() {
heap = new GenericHeap<>();
}

@Test
public void testGenericHeapAddAndGet() {
heap.add(19);
heap.add(36);
heap.add(100);
heap.add(-17);
heap.add(3);

// Check that the largest element (100) is at the top of the heap
assertEquals(100, heap.get());
}

@Test
public void testGenericHeapRemove() {
heap.add(19);
heap.add(36);
heap.add(100);
heap.add(-17);
heap.add(3);

// Verify that the largest element is removed correctly
assertEquals(100, heap.remove());

// The new element at the top should be 36
assertEquals(36, heap.get());

// Check that the size is correct after removal
assertEquals(4, heap.size());
}

@Test
public void testGenericHeapSize() {
assertTrue(heap.isEmpty());

heap.add(10);
heap.add(20);

// Check that the size is correct
assertEquals(2, heap.size());

heap.remove();

// After removal, the size should be 1
assertEquals(1, heap.size());
}

@Test
public void testGenericHeapIsEmpty() {
// Verify that the heap is initially empty
assertTrue(heap.isEmpty());

heap.add(15);

// Now the heap should not be empty
assertFalse(heap.isEmpty());

heap.remove();

// After removing the one element, it should be empty again
assertTrue(heap.isEmpty());
}

@Test
public void testGenericHeapUpdatePriority() {
heap.add(19);
heap.add(36);
heap.add(100);
heap.add(-17);
heap.add(3);

// Verify that the largest element initially is 100
assertEquals(100, heap.get());

heap.remove();

// Simulates a change in priority by increasing the value of 100 to 44
heap.add(44);

// Now, the new high should be 25
assertEquals(44, heap.get());
}

@Test
public void testGenericHeapRemoveUntilEmpty() {
heap.add(5);
heap.add(3);
heap.add(4);
heap.add(1);
heap.add(2);

// Remove all items and check that they are removed in descending order
assertEquals(5, heap.remove());
assertEquals(4, heap.remove());
assertEquals(3, heap.remove());
assertEquals(2, heap.remove());
assertEquals(1, heap.remove());

// Empty heap
assertTrue(heap.isEmpty());
}

@Test
public void testGenericHeapAddNullItem() {
// Check null item
assertThrows(IllegalArgumentException.class, () -> { heap.add(null); });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.thealgorithms.datastructures.lists;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

public class MergeSortedArrayListTest {

@Test
public void testMergeSortedWithSameSizeLists() {
List<Integer> listA = Arrays.asList(1, 3, 5, 7, 9);
List<Integer> listB = Arrays.asList(2, 4, 6, 8, 10);
List<Integer> listC = new ArrayList<>();

MergeSortedArrayList.merge(listA, listB, listC);

// Expected merged result
List<Integer> expected = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

// Assert the merged list is as expected
assertEquals(expected, listC);
}

@Test
public void testMergeSortedWithEmptyListA() {
List<Integer> listA = new ArrayList<>(); // Empty list
List<Integer> listB = Arrays.asList(2, 4, 6, 8, 10);
List<Integer> listC = new ArrayList<>();

MergeSortedArrayList.merge(listA, listB, listC);

// Expected result is listB since listA is empty
List<Integer> expected = Arrays.asList(2, 4, 6, 8, 10);
assertEquals(expected, listC);
}

@Test
public void testMergeSortedWithEmptyListB() {
List<Integer> listA = Arrays.asList(1, 3, 5, 7, 9);
List<Integer> listB = new ArrayList<>(); // Empty list
List<Integer> listC = new ArrayList<>();

MergeSortedArrayList.merge(listA, listB, listC);

// Expected result is listA since listB is empty
List<Integer> expected = Arrays.asList(1, 3, 5, 7, 9);
assertEquals(expected, listC);
}

@Test
public void testMergeSortedWithBothEmptyLists() {
List<Integer> listA = new ArrayList<>(); // Empty list
List<Integer> listB = new ArrayList<>(); // Empty list
List<Integer> listC = new ArrayList<>();

MergeSortedArrayList.merge(listA, listB, listC);

// Expected result is an empty list since both inputs are empty
List<Integer> expected = new ArrayList<>();
assertEquals(expected, listC);
}

@Test
public void testMergeSortedWithDifferentSizeLists() {
List<Integer> listA = Arrays.asList(1, 3);
List<Integer> listB = Arrays.asList(2, 4, 6, 8, 10);
List<Integer> listC = new ArrayList<>();

MergeSortedArrayList.merge(listA, listB, listC);

// Expected merged result
List<Integer> expected = Arrays.asList(1, 2, 3, 4, 6, 8, 10);
assertEquals(expected, listC);
}

@Test
public void testMergeSortedWithDuplicateElements() {
List<Integer> listA = Arrays.asList(1, 3, 5);
List<Integer> listB = Arrays.asList(1, 3, 5);
List<Integer> listC = new ArrayList<>();

MergeSortedArrayList.merge(listA, listB, listC);

// Expected merged result with duplicates
List<Integer> expected = Arrays.asList(1, 1, 3, 3, 5, 5);
assertEquals(expected, listC);
}

@Test
public void testMergeSortedWithNullListA() {
List<Integer> listA = null; // Null list
List<Integer> listB = Arrays.asList(2, 4, 6, 8, 10);
List<Integer> listC = new ArrayList<>();

assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(listA, listB, listC));
}

@Test
public void testMergeSortedWithNullListB() {
List<Integer> listA = Arrays.asList(1, 3, 5, 7, 9);
List<Integer> listB = null; // Null list
List<Integer> listC = new ArrayList<>();

assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(listA, listB, listC));
}

@Test
public void testMergeSortedWithBothNullLists() {
List<Integer> listA = null; // Null list
List<Integer> listB = null; // Null list
List<Integer> listC = new ArrayList<>();

assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(listA, listB, listC));
}

@Test
public void testMergeSortedNullLists() {
List<Integer> listA = null; // Null list
List<Integer> listB = null; // Null list
List<Integer> listC = null; // Null list

assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(listA, listB, listC));
}
}