BAEL-1123 - Testing Linked List for Cycles - deep20jain@gmail.com (#2525)
* Adding node and cycle detection by hashing * Adding implementation for all algorithms for cycle detection and removal * Applying formatting rules
This commit is contained in:
committed by
Zeger Hendrikse
parent
4baaf9f984
commit
d8398caaa2
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CycleDetectionBruteForceTest extends CycleDetectionTestBase {
|
||||
|
||||
@Test
|
||||
public void givenNormalList_dontDetectLoop() {
|
||||
Node<Integer> root = createList();
|
||||
Assert.assertFalse(CycleDetectionBruteForce.detectCycle(root));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCyclicList_detectLoop() {
|
||||
Node<Integer> root = createList();
|
||||
createLoop(root);
|
||||
Assert.assertTrue(CycleDetectionBruteForce.detectCycle(root));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CycleDetectionByFastAndSlowIteratorsTest extends CycleDetectionTestBase {
|
||||
|
||||
@Test
|
||||
public void givenNormalList_dontDetectLoop() {
|
||||
Node<Integer> root = createList();
|
||||
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(root));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCyclicList_detectLoop() {
|
||||
Node<Integer> root = createList();
|
||||
createLoop(root);
|
||||
Assert.assertTrue(CycleDetectionByFastAndSlowIterators.detectCycle(root));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CycleDetectionByHashingTest extends CycleDetectionTestBase {
|
||||
|
||||
@Test
|
||||
public void givenNormalList_dontDetectLoop() {
|
||||
Node<Integer> root = createList();
|
||||
Assert.assertFalse(CycleDetectionByHashing.detectCycle(root));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCyclicList_detectLoop() {
|
||||
Node<Integer> root = createList();
|
||||
createLoop(root);
|
||||
Assert.assertTrue(CycleDetectionByHashing.detectCycle(root));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
public class CycleDetectionTestBase {
|
||||
|
||||
public static Node<Integer> createList() {
|
||||
Node<Integer> root = Node.createNewNode(10, null);
|
||||
|
||||
for (int i = 9; i >= 1; --i) {
|
||||
Node<Integer> current = Node.createNewNode(i, root);
|
||||
root = current;
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
public static void createLoop(Node<Integer> root) {
|
||||
Node<Integer> tail = Node.getTail(root);
|
||||
|
||||
Node<Integer> middle = root;
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
middle = middle.next;
|
||||
}
|
||||
|
||||
tail.next = middle;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CycleRemovalBruteForceTest extends CycleDetectionTestBase {
|
||||
|
||||
@Test
|
||||
public void givenNormalList_dontDetectLoop() {
|
||||
Node<Integer> root = createList();
|
||||
Assert.assertFalse(CycleRemovalBruteForce.detectAndRemoveCycle(root));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCyclicList_detectAndRemoveLoop() {
|
||||
Node<Integer> root = createList();
|
||||
createLoop(root);
|
||||
Assert.assertTrue(CycleRemovalBruteForce.detectAndRemoveCycle(root));
|
||||
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(root));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CycleRemovalByCountingLoopNodesTest extends CycleDetectionTestBase {
|
||||
|
||||
@Test
|
||||
public void givenNormalList_dontDetectLoop() {
|
||||
Node<Integer> root = createList();
|
||||
Assert.assertFalse(CycleRemovalByCountingLoopNodes.detectAndRemoveCycle(root));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCyclicList_detectAndRemoveLoop() {
|
||||
Node<Integer> root = createList();
|
||||
createLoop(root);
|
||||
Assert.assertTrue(CycleRemovalByCountingLoopNodes.detectAndRemoveCycle(root));
|
||||
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(root));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CycleRemovalWithoutCountingLoopNodesTest extends CycleDetectionTestBase {
|
||||
|
||||
@Test
|
||||
public void givenNormalList_dontDetectLoop() {
|
||||
Node<Integer> root = createList();
|
||||
Assert.assertFalse(CycleRemovalWithoutCountingLoopNodes.detectAndRemoveCycle(root));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCyclicList_detectAndRemoveLoop() {
|
||||
Node<Integer> root = createList();
|
||||
createLoop(root);
|
||||
Assert.assertTrue(CycleRemovalWithoutCountingLoopNodes.detectAndRemoveCycle(root));
|
||||
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(root));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user