mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 16:27:33 +08:00
Enhance docs, remove main
, add tests in `SearchSinglyLink… (#6012)
This commit is contained in:
@ -520,6 +520,7 @@
|
||||
* [LowestBasePalindrome](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/LowestBasePalindrome.java)
|
||||
* [Luhn](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/Luhn.java)
|
||||
* [Mandelbrot](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/Mandelbrot.java)
|
||||
* [MaximumSlidingWindow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java)
|
||||
* [MaximumSumOfDistinctSubarraysWithLengthK](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/MaximumSumOfDistinctSubarraysWithLengthK.java)
|
||||
* [MemoryManagementAlgorithms](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/MemoryManagementAlgorithms.java)
|
||||
* [MiniMaxAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/MiniMaxAlgorithm.java)
|
||||
@ -887,6 +888,7 @@
|
||||
* [QuickSortLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/QuickSortLinkedListTest.java)
|
||||
* [ReverseKGroupTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java)
|
||||
* [RotateSinglyLinkedListsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java)
|
||||
* [SearchSinglyLinkedListRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/SearchSinglyLinkedListRecursionTest.java)
|
||||
* [SinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java)
|
||||
* [SkipListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/SkipListTest.java)
|
||||
* [SortedLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java)
|
||||
@ -1151,6 +1153,7 @@
|
||||
* [LineSweepTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/LineSweepTest.java)
|
||||
* [LinkListSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/LinkListSortTest.java)
|
||||
* [LowestBasePalindromeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/LowestBasePalindromeTest.java)
|
||||
* [MaximumSlidingWindowTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java)
|
||||
* [MaximumSumOfDistinctSubarraysWithLengthKTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/MaximumSumOfDistinctSubarraysWithLengthKTest.java)
|
||||
* [NewManShanksPrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java)
|
||||
* [NextFitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/NextFitTest.java)
|
||||
|
@ -1,31 +1,45 @@
|
||||
package com.thealgorithms.datastructures.lists;
|
||||
|
||||
/**
|
||||
* The SearchSinglyLinkedListRecursion class extends SinglyLinkedList and provides
|
||||
* a method to search for a value in a singly linked list using recursion.
|
||||
* <p>
|
||||
* This class demonstrates a recursive approach to check if a given integer value is
|
||||
* present in the linked list. The search method calls a private recursive helper method
|
||||
* `searchRecursion`, which checks each node's value and moves to the next node if necessary.
|
||||
* </p>
|
||||
* <p>
|
||||
* Example:
|
||||
* Given a list containing the values 1 -> 2 -> 3 -> 4, calling search(3) will return `true`,
|
||||
* while calling search(5) will return `false`.
|
||||
* </p>
|
||||
* <p>
|
||||
* Complexity:
|
||||
* <ul>
|
||||
* <li>Time Complexity: O(n), where n is the number of nodes in the linked list.</li>
|
||||
* <li>Space Complexity: O(n), due to the recursive call stack in the worst case.</li>
|
||||
* </ul>
|
||||
* </p>
|
||||
*/
|
||||
public class SearchSinglyLinkedListRecursion extends SinglyLinkedList {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SearchSinglyLinkedListRecursion list = new SearchSinglyLinkedListRecursion();
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
list.insert(i);
|
||||
}
|
||||
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
assert list.search(i);
|
||||
}
|
||||
assert !list.search(-1) && !list.search(100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the value key is present in the list using recursion.
|
||||
* Recursively searches for a given value in the linked list.
|
||||
*
|
||||
* @param node the head node.
|
||||
* @param key the value to be searched.
|
||||
* @return {@code true} if key is present in the list, otherwise
|
||||
* {@code false}.
|
||||
* @param node the head node to start the search.
|
||||
* @param key the integer value to be searched for.
|
||||
* @return {@code true} if the value `key` is present in the list; otherwise, {@code false}.
|
||||
*/
|
||||
private boolean searchRecursion(Node node, int key) {
|
||||
return (node != null && (node.value == key || searchRecursion(node.next, key)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Public search method to determine if a key is present in the linked list.
|
||||
*
|
||||
* @param key the integer value to be searched for.
|
||||
* @return {@code true} if the value `key` is present in the list; otherwise, {@code false}.
|
||||
*/
|
||||
@Override
|
||||
public boolean search(int key) {
|
||||
return searchRecursion(getHead(), key);
|
||||
|
@ -0,0 +1,89 @@
|
||||
package com.thealgorithms.datastructures.lists;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SearchSinglyLinkedListRecursionTest {
|
||||
|
||||
private SearchSinglyLinkedListRecursion list;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
list = new SearchSinglyLinkedListRecursion();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchInEmptyList() {
|
||||
// Test searching for a value in an empty list (should return false)
|
||||
assertFalse(list.search(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchSingleElementListFound() {
|
||||
// Insert a single element and search for it
|
||||
list.insert(5);
|
||||
assertTrue(list.search(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchSingleElementListNotFound() {
|
||||
// Insert a single element and search for a non-existent value
|
||||
list.insert(5);
|
||||
assertFalse(list.search(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchMultipleElementsListFound() {
|
||||
// Insert multiple elements and search for a middle value
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
list.insert(i);
|
||||
}
|
||||
assertTrue(list.search(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchMultipleElementsListFirstElement() {
|
||||
// Insert multiple elements and search for the first element
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
list.insert(i);
|
||||
}
|
||||
assertTrue(list.search(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchMultipleElementsListLastElement() {
|
||||
// Insert multiple elements and search for the last element
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
list.insert(i);
|
||||
}
|
||||
assertTrue(list.search(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchMultipleElementsListNotFound() {
|
||||
// Insert multiple elements and search for a non-existent element
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
list.insert(i);
|
||||
}
|
||||
assertFalse(list.search(15));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchNegativeValues() {
|
||||
// Insert positive and negative values and search for a negative value
|
||||
list.insert(-5);
|
||||
list.insert(-10);
|
||||
list.insert(5);
|
||||
assertTrue(list.search(-10));
|
||||
assertFalse(list.search(-3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearchZeroValue() {
|
||||
list.insert(0);
|
||||
assertTrue(list.search(0));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user