mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 22:43:30 +08:00
@ -1,16 +1,15 @@
|
||||
package com.thealgorithms.datastructures.buffers;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.RepeatedTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.RepeatedTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CircularBufferTest {
|
||||
private static final int BUFFER_SIZE = 10;
|
||||
@ -34,25 +33,21 @@ class CircularBufferTest {
|
||||
buffer.put(generateInt());
|
||||
assertFalse(buffer.isFull());
|
||||
|
||||
for (int i = 1; i < BUFFER_SIZE; i++)
|
||||
buffer.put(generateInt());
|
||||
for (int i = 1; i < BUFFER_SIZE; i++) buffer.put(generateInt());
|
||||
assertTrue(buffer.isFull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void get() {
|
||||
assertNull(buffer.get());
|
||||
for (int i = 0; i < 100; i++)
|
||||
buffer.put(i);
|
||||
for (int i = 0; i < BUFFER_SIZE; i++)
|
||||
assertEquals(i, buffer.get());
|
||||
for (int i = 0; i < 100; i++) buffer.put(i);
|
||||
for (int i = 0; i < BUFFER_SIZE; i++) assertEquals(i, buffer.get());
|
||||
assertNull(buffer.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void put() {
|
||||
for (int i = 0; i < BUFFER_SIZE; i++)
|
||||
assertTrue(buffer.put(generateInt()));
|
||||
for (int i = 0; i < BUFFER_SIZE; i++) assertTrue(buffer.put(generateInt()));
|
||||
assertFalse(buffer.put(generateInt()));
|
||||
}
|
||||
|
||||
@ -119,8 +114,7 @@ class CircularBufferTest {
|
||||
public List<Integer> getSortedListFrom(AtomicIntegerArray atomicArray) {
|
||||
int length = atomicArray.length();
|
||||
ArrayList<Integer> result = new ArrayList<>(length);
|
||||
for (int i = 0; i < length; i++)
|
||||
result.add(atomicArray.get(i));
|
||||
for (int i = 0; i < length; i++) result.add(atomicArray.get(i));
|
||||
result.sort(Comparator.comparingInt(o -> o));
|
||||
return result;
|
||||
}
|
||||
|
@ -15,19 +15,19 @@ class LFUCacheTest {
|
||||
lfuCache.put(4, 40);
|
||||
lfuCache.put(5, 50);
|
||||
|
||||
//get method call will increase frequency of key 1 by 1
|
||||
// get method call will increase frequency of key 1 by 1
|
||||
assertEquals(10, lfuCache.get(1));
|
||||
|
||||
//this operation will remove value with key as 2
|
||||
// this operation will remove value with key as 2
|
||||
lfuCache.put(6, 60);
|
||||
|
||||
//will return null as value with key 2 is now evicted
|
||||
// will return null as value with key 2 is now evicted
|
||||
assertEquals(null, lfuCache.get(2));
|
||||
|
||||
//should return 60
|
||||
// should return 60
|
||||
assertEquals(60, lfuCache.get(6));
|
||||
|
||||
//this operation will remove value with key as 3
|
||||
// this operation will remove value with key as 3
|
||||
lfuCache.put(7, 70);
|
||||
|
||||
assertEquals(null, lfuCache.get(2));
|
||||
@ -43,19 +43,19 @@ class LFUCacheTest {
|
||||
lfuCache.put(4, "Delta");
|
||||
lfuCache.put(5, "Eplison");
|
||||
|
||||
//get method call will increase frequency of key 1 by 1
|
||||
// get method call will increase frequency of key 1 by 1
|
||||
assertEquals("Alpha", lfuCache.get(1));
|
||||
|
||||
//this operation will remove value with key as 2
|
||||
// this operation will remove value with key as 2
|
||||
lfuCache.put(6, "Digamma");
|
||||
|
||||
//will return null as value with key 2 is now evicted
|
||||
// will return null as value with key 2 is now evicted
|
||||
assertEquals(null, lfuCache.get(2));
|
||||
|
||||
//should return string Digamma
|
||||
// should return string Digamma
|
||||
assertEquals("Digamma", lfuCache.get(6));
|
||||
|
||||
//this operation will remove value with key as 3
|
||||
// this operation will remove value with key as 3
|
||||
lfuCache.put(7, "Zeta");
|
||||
|
||||
assertEquals(null, lfuCache.get(2));
|
||||
|
@ -10,36 +10,30 @@ class HamiltonianCycleTest {
|
||||
|
||||
@Test
|
||||
void testFindHamiltonianCycleShouldReturnHamiltonianCycle() {
|
||||
int[] expectedArray = { 0, 1, 2, 4, 3, 0 };
|
||||
int[] expectedArray = {0, 1, 2, 4, 3, 0};
|
||||
int[][] inputArray = {
|
||||
{ 0, 1, 0, 1, 0 },
|
||||
{ 1, 0, 1, 1, 1 },
|
||||
{ 0, 1, 0, 0, 1 },
|
||||
{ 1, 1, 0, 0, 1 },
|
||||
{ 0, 1, 1, 1, 0 },
|
||||
{0, 1, 0, 1, 0},
|
||||
{1, 0, 1, 1, 1},
|
||||
{0, 1, 0, 0, 1},
|
||||
{1, 1, 0, 0, 1},
|
||||
{0, 1, 1, 1, 0},
|
||||
};
|
||||
|
||||
assertArrayEquals(
|
||||
expectedArray,
|
||||
hamiltonianCycle.findHamiltonianCycle(inputArray)
|
||||
);
|
||||
assertArrayEquals(expectedArray, hamiltonianCycle.findHamiltonianCycle(inputArray));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindHamiltonianCycleShouldReturnInfinityArray() {
|
||||
int[] expectedArray = { -1, -1, -1, -1, -1, -1 };
|
||||
int[] expectedArray = {-1, -1, -1, -1, -1, -1};
|
||||
|
||||
int[][] inputArray = {
|
||||
{ 0, 1, 0, 1, 0 },
|
||||
{ 1, 0, 1, 1, 1 },
|
||||
{ 0, 1, 0, 0, 1 },
|
||||
{ 1, 1, 0, 0, 0 },
|
||||
{ 0, 1, 1, 0, 0 },
|
||||
{0, 1, 0, 1, 0},
|
||||
{1, 0, 1, 1, 1},
|
||||
{0, 1, 0, 0, 1},
|
||||
{1, 1, 0, 0, 0},
|
||||
{0, 1, 1, 0, 0},
|
||||
};
|
||||
|
||||
assertArrayEquals(
|
||||
expectedArray,
|
||||
hamiltonianCycle.findHamiltonianCycle(inputArray)
|
||||
);
|
||||
assertArrayEquals(expectedArray, hamiltonianCycle.findHamiltonianCycle(inputArray));
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class KosarajuTest {
|
||||
@ -14,7 +13,7 @@ public class KosarajuTest {
|
||||
|
||||
@Test
|
||||
public void findStronglyConnectedComps() {
|
||||
//Create a adjacency list of graph
|
||||
// Create a adjacency list of graph
|
||||
var n = 8;
|
||||
var adjList = new ArrayList<List<Integer>>(n);
|
||||
|
||||
@ -36,10 +35,10 @@ public class KosarajuTest {
|
||||
List<List<Integer>> actualResult = kosaraju.kosaraju(n, adjList);
|
||||
List<List<Integer>> expectedResult = new ArrayList<>();
|
||||
/*
|
||||
Expected result:
|
||||
Expected result:
|
||||
0, 1, 2
|
||||
3
|
||||
5, 4, 6
|
||||
5, 4, 6
|
||||
7
|
||||
*/
|
||||
expectedResult.add(Arrays.asList(1, 2, 0));
|
||||
@ -51,7 +50,7 @@ public class KosarajuTest {
|
||||
|
||||
@Test
|
||||
public void findStronglyConnectedCompsShouldGetSingleNodes() {
|
||||
//Create a adjacency list of graph
|
||||
// Create a adjacency list of graph
|
||||
var n = 8;
|
||||
var adjList = new ArrayList<List<Integer>>(n);
|
||||
|
||||
@ -71,11 +70,10 @@ public class KosarajuTest {
|
||||
List<List<Integer>> actualResult = kosaraju.kosaraju(n, adjList);
|
||||
List<List<Integer>> expectedResult = new ArrayList<>();
|
||||
/*
|
||||
Expected result:
|
||||
Expected result:
|
||||
0, 1, 2, 3, 4, 5, 6, 7
|
||||
*/
|
||||
expectedResult.add(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 0));
|
||||
assertTrue(expectedResult.equals(actualResult));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,15 +5,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TarjansAlgorithmTest {
|
||||
|
||||
|
||||
TarjansAlgorithm tarjansAlgo = new TarjansAlgorithm();
|
||||
|
||||
@Test
|
||||
public void findStronglyConnectedComps(){
|
||||
public void findStronglyConnectedComps() {
|
||||
var v = 5;
|
||||
var graph = new ArrayList<List<Integer>>();
|
||||
for (int i = 0; i < v; i++) {
|
||||
@ -27,10 +26,10 @@ public class TarjansAlgorithmTest {
|
||||
|
||||
var actualResult = tarjansAlgo.stronglyConnectedComponents(v, graph);
|
||||
/*
|
||||
Expected result:
|
||||
Expected result:
|
||||
0, 1, 2
|
||||
3
|
||||
4
|
||||
4
|
||||
*/
|
||||
List<List<Integer>> expectedResult = new ArrayList<>();
|
||||
|
||||
@ -42,7 +41,7 @@ public class TarjansAlgorithmTest {
|
||||
|
||||
@Test
|
||||
public void findStronglyConnectedCompsShouldGetSingleNodes() {
|
||||
//Create a adjacency list of graph
|
||||
// Create a adjacency list of graph
|
||||
var n = 8;
|
||||
var adjList = new ArrayList<List<Integer>>(n);
|
||||
|
||||
@ -62,11 +61,10 @@ public class TarjansAlgorithmTest {
|
||||
List<List<Integer>> actualResult = tarjansAlgo.stronglyConnectedComponents(n, adjList);
|
||||
List<List<Integer>> expectedResult = new ArrayList<>();
|
||||
/*
|
||||
Expected result:
|
||||
Expected result:
|
||||
7, 6, 5, 4, 3, 2, 1, 0
|
||||
*/
|
||||
expectedResult.add(Arrays.asList(7, 6, 5, 4, 3, 2, 1, 0));
|
||||
assertTrue(expectedResult.equals(actualResult));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ class HashMapCuckooHashingTest {
|
||||
|
||||
private HashMapCuckooHashing createHashMapCuckooHashing() {
|
||||
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
|
||||
int[] values = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222 };
|
||||
int[] values = {11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222};
|
||||
Arrays.stream(values).forEach(hashTable::insertKey2HashTable);
|
||||
return hashTable;
|
||||
}
|
||||
|
@ -1,17 +1,14 @@
|
||||
package com.thealgorithms.datastructures.hashmap.hashing;
|
||||
|
||||
import com.thealgorithms.datastructures.hashmap.hashing.MajorityElement;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.thealgorithms.datastructures.hashmap.hashing.MajorityElement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MajorityElementTest{
|
||||
public class MajorityElementTest {
|
||||
@Test
|
||||
void testMajorityWithSingleMajorityElement() {
|
||||
int[] nums = {1, 2, 3, 9, 9, 6, 7, 8, 9, 9, 9, 9};
|
||||
@ -42,7 +39,7 @@ public class MajorityElementTest{
|
||||
@Test
|
||||
void testMajorityWithEmptyArray() {
|
||||
int[] nums = {};
|
||||
List<Integer> expected = Collections.emptyList();
|
||||
List<Integer> expected = Collections.emptyList();
|
||||
List<Integer> actual = MajorityElement.majority(nums);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
package com.thealgorithms.datastructures.hashmap.hashing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
abstract class MapTest {
|
||||
abstract <Key extends Comparable<Key>, Value> Map<Key, Value> getMap();
|
||||
|
@ -4,25 +4,25 @@ import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class LeftistHeapTest {
|
||||
|
||||
@Test
|
||||
void testLeftistHeap() {
|
||||
LeftistHeap heap = new LeftistHeap();
|
||||
Assertions.assertTrue(heap.isEmpty());
|
||||
heap.insert(6);
|
||||
Assertions.assertTrue(!heap.isEmpty());
|
||||
heap.insert(2);
|
||||
heap.insert(3);
|
||||
heap.insert(1);
|
||||
heap.in_order();
|
||||
Assertions.assertTrue(heap.in_order().toString().equals("[6, 2, 3, 1]"));
|
||||
Assertions.assertTrue(heap.extract_min() == 1);
|
||||
Assertions.assertTrue(heap.in_order().toString().equals("[6, 2, 3]"));
|
||||
heap.insert(8);
|
||||
heap.insert(12);
|
||||
heap.insert(4);
|
||||
Assertions.assertTrue(heap.in_order().toString().equals("[8, 3, 12, 2, 6, 4]"));
|
||||
heap.clear();
|
||||
Assertions.assertTrue(heap.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLeftistHeap() {
|
||||
LeftistHeap heap = new LeftistHeap();
|
||||
Assertions.assertTrue(heap.isEmpty());
|
||||
heap.insert(6);
|
||||
Assertions.assertTrue(!heap.isEmpty());
|
||||
heap.insert(2);
|
||||
heap.insert(3);
|
||||
heap.insert(1);
|
||||
heap.in_order();
|
||||
Assertions.assertTrue(heap.in_order().toString().equals("[6, 2, 3, 1]"));
|
||||
Assertions.assertTrue(heap.extract_min() == 1);
|
||||
Assertions.assertTrue(heap.in_order().toString().equals("[6, 2, 3]"));
|
||||
heap.insert(8);
|
||||
heap.insert(12);
|
||||
heap.insert(4);
|
||||
Assertions.assertTrue(heap.in_order().toString().equals("[8, 3, 12, 2, 6, 4]"));
|
||||
heap.clear();
|
||||
Assertions.assertTrue(heap.isEmpty());
|
||||
}
|
||||
}
|
@ -1,12 +1,10 @@
|
||||
package com.thealgorithms.datastructures.lists;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SinglyLinkedListTest {
|
||||
|
||||
@ -23,7 +21,7 @@ public class SinglyLinkedListTest {
|
||||
}
|
||||
|
||||
for (int i = 0; i < length - 1; i++) {
|
||||
nodeList.get(i).next = nodeList.get(i+1);
|
||||
nodeList.get(i).next = nodeList.get(i + 1);
|
||||
}
|
||||
|
||||
return new SinglyLinkedList(nodeList.get(0), length);
|
||||
@ -31,7 +29,7 @@ public class SinglyLinkedListTest {
|
||||
|
||||
@Test
|
||||
void detectLoop() {
|
||||
//List has cycle
|
||||
// List has cycle
|
||||
Node firstNode = new Node(1);
|
||||
Node secondNode = new Node(2);
|
||||
Node thirdNode = new Node(3);
|
||||
@ -53,16 +51,16 @@ public class SinglyLinkedListTest {
|
||||
void middle() {
|
||||
int oddNumberOfNode = 7;
|
||||
SinglyLinkedList list = createSampleList(oddNumberOfNode);
|
||||
assertEquals(oddNumberOfNode/2 + 1, list.middle().value);
|
||||
assertEquals(oddNumberOfNode / 2 + 1, list.middle().value);
|
||||
int evenNumberOfNode = 8;
|
||||
list = createSampleList(evenNumberOfNode);
|
||||
assertEquals(evenNumberOfNode/2, list.middle().value);
|
||||
assertEquals(evenNumberOfNode / 2, list.middle().value);
|
||||
|
||||
//return null if empty
|
||||
// return null if empty
|
||||
list = new SinglyLinkedList();
|
||||
assertNull(list.middle());
|
||||
|
||||
//return head if there is only one node
|
||||
// return head if there is only one node
|
||||
list = createSampleList(1);
|
||||
assertEquals(list.getHead(), list.middle());
|
||||
}
|
||||
@ -72,7 +70,7 @@ public class SinglyLinkedListTest {
|
||||
SinglyLinkedList list = createSampleList(5);
|
||||
assertEquals(1, list.getHead().value);
|
||||
assertEquals(5, list.getNth(4));
|
||||
list.swapNodes(1,5);
|
||||
list.swapNodes(1, 5);
|
||||
assertEquals(5, list.getHead().value);
|
||||
assertEquals(1, list.getNth(4));
|
||||
}
|
||||
@ -97,67 +95,69 @@ public class SinglyLinkedListTest {
|
||||
void deleteNth() {
|
||||
SinglyLinkedList list = createSampleList(10);
|
||||
assertTrue(list.search(7));
|
||||
list.deleteNth(6); //Index 6 has value 7
|
||||
list.deleteNth(6); // Index 6 has value 7
|
||||
assertFalse(list.search(7));
|
||||
}
|
||||
//Test to check whether the method reverseList() works fine
|
||||
// Test to check whether the method reverseList() works fine
|
||||
@Test
|
||||
void reverseList(){
|
||||
void reverseList() {
|
||||
|
||||
//Creating a new LinkedList of size:4
|
||||
//The linkedlist will be 1->2->3->4->null
|
||||
// Creating a new LinkedList of size:4
|
||||
// The linkedlist will be 1->2->3->4->null
|
||||
SinglyLinkedList list = createSampleList(4);
|
||||
|
||||
//Reversing the LinkedList using reverseList() method and storing the head of the reversed linkedlist in a head node
|
||||
//The reversed linkedlist will be 4->3->2->1->null
|
||||
Node head=list.reverseList(list.getHead());
|
||||
|
||||
//Recording the Nodes after reversing the LinkedList
|
||||
Node firstNode = head; //4
|
||||
Node secondNode = firstNode.next; //3
|
||||
Node thirdNode = secondNode.next; //2
|
||||
Node fourthNode = thirdNode.next; //1
|
||||
|
||||
//Checking whether the LinkedList is reversed or not by comparing the original list and reversed list nodes
|
||||
assertEquals(1,fourthNode.value);
|
||||
assertEquals(2,thirdNode.value);
|
||||
assertEquals(3,secondNode.value);
|
||||
assertEquals(4,firstNode.value);
|
||||
}
|
||||
|
||||
//Test to check whether implemented reverseList() method handles NullPointer Exception for TestCase where head==null
|
||||
@Test
|
||||
void reverseListNullPointer(){
|
||||
//Creating a linkedlist with first node assigned to null
|
||||
SinglyLinkedList list=new SinglyLinkedList();
|
||||
Node first=list.getHead();
|
||||
|
||||
//Reversing the linkedlist
|
||||
Node head=list.reverseList(first);
|
||||
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
|
||||
// linkedlist in a head node The reversed linkedlist will be 4->3->2->1->null
|
||||
Node head = list.reverseList(list.getHead());
|
||||
|
||||
//checking whether the method works fine if the input is null
|
||||
assertEquals(head,first);
|
||||
// Recording the Nodes after reversing the LinkedList
|
||||
Node firstNode = head; // 4
|
||||
Node secondNode = firstNode.next; // 3
|
||||
Node thirdNode = secondNode.next; // 2
|
||||
Node fourthNode = thirdNode.next; // 1
|
||||
|
||||
// Checking whether the LinkedList is reversed or not by comparing the original list and
|
||||
// reversed list nodes
|
||||
assertEquals(1, fourthNode.value);
|
||||
assertEquals(2, thirdNode.value);
|
||||
assertEquals(3, secondNode.value);
|
||||
assertEquals(4, firstNode.value);
|
||||
}
|
||||
|
||||
//Testing reverseList() method for a linkedlist of size: 20
|
||||
// Test to check whether implemented reverseList() method handles NullPointer Exception for
|
||||
// TestCase where head==null
|
||||
@Test
|
||||
void reverseListTest(){
|
||||
//Creating a new linkedlist
|
||||
void reverseListNullPointer() {
|
||||
// Creating a linkedlist with first node assigned to null
|
||||
SinglyLinkedList list = new SinglyLinkedList();
|
||||
Node first = list.getHead();
|
||||
|
||||
// Reversing the linkedlist
|
||||
Node head = list.reverseList(first);
|
||||
|
||||
// checking whether the method works fine if the input is null
|
||||
assertEquals(head, first);
|
||||
}
|
||||
|
||||
// Testing reverseList() method for a linkedlist of size: 20
|
||||
@Test
|
||||
void reverseListTest() {
|
||||
// Creating a new linkedlist
|
||||
SinglyLinkedList list = createSampleList(20);
|
||||
|
||||
//Reversing the LinkedList using reverseList() method and storing the head of the reversed linkedlist in a head node
|
||||
Node head=list.reverseList(list.getHead());
|
||||
|
||||
//Storing the head in a temp variable, so that we cannot loose the track of head
|
||||
Node temp=head;
|
||||
// Reversing the LinkedList using reverseList() method and storing the head of the reversed
|
||||
// linkedlist in a head node
|
||||
Node head = list.reverseList(list.getHead());
|
||||
|
||||
int i=20; //This is for the comparison of values of nodes of the reversed linkedlist
|
||||
//Checking whether the reverseList() method performed its task
|
||||
while(temp!=null && i>0){
|
||||
assertEquals(i,temp.value);
|
||||
temp=temp.next;
|
||||
i--;
|
||||
// Storing the head in a temp variable, so that we cannot loose the track of head
|
||||
Node temp = head;
|
||||
|
||||
int i = 20; // This is for the comparison of values of nodes of the reversed linkedlist
|
||||
// Checking whether the reverseList() method performed its task
|
||||
while (temp != null && i > 0) {
|
||||
assertEquals(i, temp.value);
|
||||
temp = temp.next;
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -69,16 +69,14 @@ class SkipListTest {
|
||||
@Test
|
||||
void checkSortedOnLowestLayer() {
|
||||
SkipList<String> skipList = new SkipList<>();
|
||||
String[] values = { "d", "b", "a", "c" };
|
||||
String[] values = {"d", "b", "a", "c"};
|
||||
Arrays.stream(values).forEach(skipList::add);
|
||||
print(skipList);
|
||||
|
||||
String[] actualOrder = IntStream
|
||||
.range(0, values.length)
|
||||
.mapToObj(skipList::get)
|
||||
.toArray(String[]::new);
|
||||
String[] actualOrder
|
||||
= IntStream.range(0, values.length).mapToObj(skipList::get).toArray(String[] ::new);
|
||||
|
||||
assertArrayEquals(new String[] { "a", "b", "c", "d" }, actualOrder);
|
||||
assertArrayEquals(new String[] {"a", "b", "c", "d"}, actualOrder);
|
||||
}
|
||||
|
||||
private SkipList<String> createSkipList() {
|
||||
|
@ -1,29 +1,26 @@
|
||||
package com.thealgorithms.datastructures.queues;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class LinkedQueueTest {
|
||||
@Test
|
||||
public void testQue() {
|
||||
LinkedQueue<Integer> queue = new LinkedQueue<>();
|
||||
for (int i = 1; i < 5; i++)
|
||||
queue.enqueue(i);
|
||||
@Test
|
||||
public void testQue() {
|
||||
LinkedQueue<Integer> queue = new LinkedQueue<>();
|
||||
for (int i = 1; i < 5; i++) queue.enqueue(i);
|
||||
|
||||
assertEquals(queue.peekRear(), 4);
|
||||
assertEquals(queue.peek(2), 2);
|
||||
assertEquals(queue.peekRear(), 4);
|
||||
assertEquals(queue.peek(2), 2);
|
||||
|
||||
assertEquals(queue.peek(4), 4);
|
||||
assertEquals(queue.peek(4), 4);
|
||||
|
||||
final int[] element = { 1 };
|
||||
final int[] element = {1};
|
||||
|
||||
// iterates over all the elements present
|
||||
// as in the form of nodes
|
||||
queue.forEach(integer -> {
|
||||
if (element[0]++ != integer)
|
||||
throw new AssertionError();
|
||||
});
|
||||
}
|
||||
// iterates over all the elements present
|
||||
// as in the form of nodes
|
||||
queue.forEach(integer -> {
|
||||
if (element[0]++ != integer) throw new AssertionError();
|
||||
});
|
||||
}
|
||||
}
|
@ -26,7 +26,7 @@ class PriorityQueuesTest {
|
||||
myQueue.insert(5);
|
||||
myQueue.insert(3);
|
||||
myQueue.insert(10);
|
||||
|
||||
|
||||
myQueue.remove();
|
||||
Assertions.assertEquals(myQueue.peek(), 5);
|
||||
myQueue.remove();
|
||||
@ -46,7 +46,7 @@ class PriorityQueuesTest {
|
||||
myQueue.insert(10);
|
||||
Assertions.assertEquals(myQueue.isEmpty(), false);
|
||||
Assertions.assertEquals(myQueue.isFull(), true);
|
||||
|
||||
|
||||
myQueue.remove();
|
||||
Assertions.assertEquals(myQueue.getSize(), 3);
|
||||
Assertions.assertEquals(myQueue.peek(), 5);
|
||||
|
@ -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