Skip to content

Commit fada815

Browse files
authored
Merge branch 'master' into min_pq_improve
2 parents 6f87d10 + 0f1dcbe commit fada815

29 files changed

+1471
-250
lines changed

DIRECTORY.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@
321321
* [BinaryAddition](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java)
322322
* [CoinChange](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/CoinChange.java)
323323
* [DigitSeparation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java)
324+
* [EgyptianFraction](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/EgyptianFraction.java)
324325
* [FractionalKnapsack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/FractionalKnapsack.java)
325326
* [GaleShapley](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/GaleShapley.java)
326327
* [JobSequencing](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/JobSequencing.java)
@@ -351,6 +352,7 @@
351352
* [BinaryPow](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/BinaryPow.java)
352353
* [BinomialCoefficient](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/BinomialCoefficient.java)
353354
* [Ceil](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Ceil.java)
355+
* [ChineseRemainderTheorem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/ChineseRemainderTheorem.java)
354356
* [CircularConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/CircularConvolutionFFT.java)
355357
* [CollatzConjecture](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/CollatzConjecture.java)
356358
* [Combinations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Combinations.java)
@@ -568,6 +570,8 @@
568570
* [TernarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/TernarySearch.java)
569571
* [UnionFind](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UnionFind.java)
570572
* [UpperBound](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UpperBound.java)
573+
* slidingwindow
574+
* [MaxSumKSizeSubarray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarray.java)
571575
* sorts
572576
* [AdaptiveMergeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/AdaptiveMergeSort.java)
573577
* [BeadSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/BeadSort.java)
@@ -826,11 +830,17 @@
826830
* [HashMapCuckooHashingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/hashmap/HashMapCuckooHashingTest.java)
827831
* heaps
828832
* [FibonacciHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/FibonacciHeapTest.java)
833+
* [GenericHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java)
829834
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
830835
* [MinPriorityQueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MinPriorityQueueTest.java)
831836
* lists
832837
* [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
838+
* [CountSinglyLinkedListRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CountSinglyLinkedListRecursionTest.java)
833839
* [CreateAndDetectLoopTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CreateAndDetectLoopTest.java)
840+
* [CursorLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CursorLinkedListTest.java)
841+
* [MergeKSortedLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/MergeKSortedLinkedListTest.java)
842+
* [MergeSortedArrayListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/MergeSortedArrayListTest.java)
843+
* [MergeSortedSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/MergeSortedSinglyLinkedListTest.java)
834844
* [QuickSortLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/QuickSortLinkedListTest.java)
835845
* [ReverseKGroupTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java)
836846
* [RotateSinglyLinkedListsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java)
@@ -938,6 +948,7 @@
938948
* [BinaryAdditionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/BinaryAdditionTest.java)
939949
* [CoinChangeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/CoinChangeTest.java)
940950
* [DigitSeparationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java)
951+
* [EgyptianFractionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/EgyptianFractionTest.java)
941952
* [FractionalKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/FractionalKnapsackTest.java)
942953
* [GaleShapleyTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/GaleShapleyTest.java)
943954
* [JobSequencingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/JobSequencingTest.java)
@@ -965,6 +976,7 @@
965976
* [BinaryPowTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/BinaryPowTest.java)
966977
* [BinomialCoefficientTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/BinomialCoefficientTest.java)
967978
* [CeilTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CeilTest.java)
979+
* [ChineseRemainderTheoremTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ChineseRemainderTheoremTest.java)
968980
* [CollatzConjectureTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CollatzConjectureTest.java)
969981
* [CombinationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CombinationsTest.java)
970982
* [ConvolutionFFTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ConvolutionFFTTest.java)
@@ -1151,6 +1163,8 @@
11511163
* [TestSearchInARowAndColWiseSortedMatrix](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/TestSearchInARowAndColWiseSortedMatrix.java)
11521164
* [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java)
11531165
* [UpperBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UpperBoundTest.java)
1166+
* slidingwindow
1167+
* [MaxSumKSizeSubarrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarrayTest.java)
11541168
* sorts
11551169
* [AdaptiveMergeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/AdaptiveMergeSortTest.java)
11561170
* [BeadSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/BeadSortTest.java)

src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ public class GenericHeap<T extends Comparable<T>> {
99
HashMap<T, Integer> map = new HashMap<>();
1010

1111
public void add(T item) {
12+
if (item == null) {
13+
throw new IllegalArgumentException("Cannot insert null into the heap.");
14+
}
15+
1216
this.data.add(item);
1317
map.put(item, this.data.size() - 1); //
1418
upHeapify(this.data.size() - 1);

src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
package com.thealgorithms.datastructures.lists;
22

3+
/**
4+
* This class is a circular singly linked list implementation. In a circular linked list,
5+
* the last node points back to the first node, creating a circular chain.
6+
*
7+
* <p>This implementation includes basic operations such as appending elements
8+
* to the end, removing elements from a specified position, and converting
9+
* the list to a string representation.
10+
*
11+
* @param <E> the type of elements held in this list
12+
*/
313
public class CircleLinkedList<E> {
414

5-
private static final class Node<E> {
15+
/**
16+
* A static nested class representing a node in the circular linked list.
17+
*
18+
* @param <E> the type of element stored in the node
19+
*/
20+
static final class Node<E> {
621

722
Node<E> next;
823
E value;
@@ -13,44 +28,56 @@ private Node(E value, Node<E> next) {
1328
}
1429
}
1530

16-
// For better O.O design this should be private allows for better black box design
1731
private int size;
18-
// this will point to dummy node;
19-
private Node<E> head = null;
20-
private Node<E> tail = null; // keeping a tail pointer to keep track of the end of list
32+
Node<E> head = null;
33+
private Node<E> tail;
2134

22-
// constructor for class.. here we will make a dummy node for circly linked list implementation
23-
// with reduced error catching as our list will never be empty;
35+
/**
36+
* Initializes a new circular linked list. A dummy head node is used for simplicity,
37+
* pointing initially to itself to ensure the list is never empty.
38+
*/
2439
public CircleLinkedList() {
25-
// creation of the dummy node
26-
head = new Node<E>(null, head);
40+
head = new Node<>(null, head);
2741
tail = head;
2842
size = 0;
2943
}
3044

31-
// getter for the size... needed because size is private.
45+
/**
46+
* Returns the current size of the list.
47+
*
48+
* @return the number of elements in the list
49+
*/
3250
public int getSize() {
3351
return size;
3452
}
3553

36-
// for the sake of simplistiy this class will only contain the append function or addLast other
37-
// add functions can be implemented however this is the basses of them all really.
54+
/**
55+
* Appends a new element to the end of the list. Throws a NullPointerException if
56+
* a null value is provided.
57+
*
58+
* @param value the value to append to the list
59+
* @throws NullPointerException if the value is null
60+
*/
3861
public void append(E value) {
3962
if (value == null) {
40-
// we do not want to add null elements to the list.
4163
throw new NullPointerException("Cannot add null element to the list");
4264
}
43-
// head.next points to the last element;
4465
if (tail == null) {
45-
tail = new Node<E>(value, head);
66+
tail = new Node<>(value, head);
4667
head.next = tail;
4768
} else {
48-
tail.next = new Node<E>(value, head);
69+
tail.next = new Node<>(value, head);
4970
tail = tail.next;
5071
}
5172
size++;
5273
}
5374

75+
/**
76+
* Returns a string representation of the list in the format "[ element1, element2, ... ]".
77+
* An empty list is represented as "[]".
78+
*
79+
* @return the string representation of the list
80+
*/
5481
public String toString() {
5582
if (size == 0) {
5683
return "[]";
@@ -68,23 +95,27 @@ public String toString() {
6895
return sb.toString();
6996
}
7097

98+
/**
99+
* Removes and returns the element at the specified position in the list.
100+
* Throws an IndexOutOfBoundsException if the position is invalid.
101+
*
102+
* @param pos the position of the element to remove
103+
* @return the value of the removed element
104+
* @throws IndexOutOfBoundsException if the position is out of range
105+
*/
71106
public E remove(int pos) {
72107
if (pos >= size || pos < 0) {
73-
// catching errors
74-
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
108+
throw new IndexOutOfBoundsException("Position out of bounds");
75109
}
76-
// we need to keep track of the element before the element we want to remove we can see why
77-
// bellow.
110+
78111
Node<E> before = head;
79112
for (int i = 1; i <= pos; i++) {
80113
before = before.next;
81114
}
82115
Node<E> destroy = before.next;
83116
E saved = destroy.value;
84-
// assigning the next reference to the element following the element we want to remove...
85-
// the last element will be assigned to the head.
86-
before.next = before.next.next;
87-
// scrubbing
117+
before.next = destroy.next;
118+
88119
if (destroy == tail) {
89120
tail = before;
90121
}

src/main/java/com/thealgorithms/datastructures/lists/CountSinglyLinkedListRecursion.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
package com.thealgorithms.datastructures.lists;
22

3+
/**
4+
* CountSinglyLinkedListRecursion extends a singly linked list to include a
5+
* recursive count method, which calculates the number of nodes in the list.
6+
*/
37
public class CountSinglyLinkedListRecursion extends SinglyLinkedList {
48

5-
public static void main(String[] args) {
6-
CountSinglyLinkedListRecursion list = new CountSinglyLinkedListRecursion();
7-
for (int i = 1; i <= 5; ++i) {
8-
list.insert(i);
9-
}
10-
assert list.count() == 5;
11-
}
12-
139
/**
14-
* Calculate the count of the list manually using recursion.
10+
* Recursively calculates the number of nodes in the list.
1511
*
16-
* @param head head of the list.
17-
* @return count of the list.
12+
* @param head the head node of the list segment being counted.
13+
* @return the count of nodes from the given head node onward.
1814
*/
1915
private int countRecursion(Node head) {
2016
return head == null ? 0 : 1 + countRecursion(head.next);
2117
}
2218

2319
/**
24-
* Returns the count of the list.
20+
* Returns the total number of nodes in the list by invoking the recursive
21+
* count helper method.
22+
*
23+
* @return the total node count in the list.
2524
*/
2625
@Override
2726
public int count() {

src/main/java/com/thealgorithms/datastructures/lists/CreateAndDetectLoop.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
package com.thealgorithms.datastructures.lists;
22

3+
/**
4+
* CreateAndDetectLoop provides utility methods for creating and detecting loops
5+
* (cycles) in a singly linked list. Loops in a linked list are created by
6+
* connecting the "next" pointer of one node to a previous node in the list,
7+
* forming a cycle.
8+
*/
39
public final class CreateAndDetectLoop {
410

5-
// Node class representing a single node in the linked list
11+
/**
12+
* Private constructor to prevent instantiation of this utility class.
13+
*/
614
private CreateAndDetectLoop() {
715
throw new UnsupportedOperationException("Utility class");
816
}
17+
18+
/**
19+
* Node represents an individual element in the linked list, containing
20+
* data and a reference to the next node.
21+
*/
922
static final class Node {
1023
int data;
1124
Node next;
@@ -16,19 +29,16 @@ static final class Node {
1629
}
1730
}
1831

19-
// Method to create a loop between two specific positions in the linked list
20-
/*
21-
* Test case that shows the Cycle(loop) in a LinkedList
22-
* Let's take this linked list:
23-
* 1->2->3->4->5->6
24-
* \______/
25-
* In this linked list we can see there is a cycle.
26-
* we can create loop by calling createLoop function in main after creating LL
27-
* createLoop(head,2,5);
28-
* to detect there is loop or not we can call detectloop function in main
29-
* detectloop(head);
32+
/**
33+
* Creates a loop in a linked list by connecting the next pointer of a node
34+
* at a specified starting position (position2) to another node at a specified
35+
* destination position (position1). If either position is invalid, no loop
36+
* will be created.
37+
*
38+
* @param head the head node of the linked list
39+
* @param position1 the position in the list where the loop should end
40+
* @param position2 the position in the list where the loop should start
3041
*/
31-
3242
static void createLoop(Node head, int position1, int position2) {
3343
if (position1 == 0 || position2 == 0) {
3444
return;
@@ -39,29 +49,32 @@ static void createLoop(Node head, int position1, int position2) {
3949

4050
int count1 = 1;
4151
int count2 = 1;
42-
// Traverse to find node at position1
52+
// Traverse to the node at position1
4353
while (count1 < position1 && node1 != null) {
4454
node1 = node1.next;
4555
count1++;
4656
}
4757

48-
// Traverse to find node at position2
58+
// Traverse to the node at position2
4959
while (count2 < position2 && node2 != null) {
5060
node2 = node2.next;
5161
count2++;
5262
}
5363

54-
// Create a loop by connecting node2's next to node1
64+
// If both nodes are valid, create the loop
5565
if (node1 != null && node2 != null) {
5666
node2.next = node1;
5767
}
5868
}
59-
// Method to detect a loop in the linked list
69+
6070
/**
61-
* Detects the presence of a loop in the linked list.
71+
* Detects the presence of a loop in the linked list using Floyd's cycle-finding
72+
* algorithm, also known as the "tortoise and hare" method.
6273
*
63-
* @see <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">Floyd's Cycle Detection Algorithm</a>
64-
* @return true if loop exists else false
74+
* @param head the head node of the linked list
75+
* @return true if a loop is detected, false otherwise
76+
* @see <a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare">
77+
* Floyd's Cycle Detection Algorithm</a>
6578
*/
6679
static boolean detectLoop(Node head) {
6780
Node sptr = head;

0 commit comments

Comments
 (0)