Skip to content

Commit 01b3142

Browse files
authored
Merge branch 'master' into patch-2
2 parents 7cca3aa + b31bc86 commit 01b3142

File tree

8 files changed

+436
-64
lines changed

8 files changed

+436
-64
lines changed

DIRECTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@
466466
* [SumWithoutArithmeticOperators](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/SumWithoutArithmeticOperators.java)
467467
* [TrinomialTriangle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/TrinomialTriangle.java)
468468
* [TwinPrime](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/TwinPrime.java)
469+
* [UniformNumbers](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/UniformNumbers.java)
469470
* [VampireNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/VampireNumber.java)
470471
* [VectorCrossProduct](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/VectorCrossProduct.java)
471472
* [Volume](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Volume.java)
@@ -559,6 +560,7 @@
559560
* [NonPreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java)
560561
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
561562
* [ProportionalFairScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/ProportionalFairScheduling.java)
563+
* [RandomScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RandomScheduling.java)
562564
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
563565
* [SelfAdjustingScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.java)
564566
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
@@ -597,6 +599,7 @@
597599
* [UnionFind](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UnionFind.java)
598600
* [UpperBound](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UpperBound.java)
599601
* slidingwindow
602+
* [LongestSubarrayWithSumLessOrEqualToK](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java)
600603
* [LongestSubstringWithoutRepeatingCharacters](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/LongestSubstringWithoutRepeatingCharacters.java)
601604
* [MaxSumKSizeSubarray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarray.java)
602605
* [MinSumKSizeSubarray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java)
@@ -909,6 +912,7 @@
909912
* [StackArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayTest.java)
910913
* [StackOfLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java)
911914
* trees
915+
* [AVLTreeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/AVLTreeTest.java)
912916
* [BinaryTreeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java)
913917
* [BoundaryTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/BoundaryTraversalTest.java)
914918
* [BSTFromSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/BSTFromSortedArrayTest.java)
@@ -1119,6 +1123,7 @@
11191123
* [SumWithoutArithmeticOperatorsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SumWithoutArithmeticOperatorsTest.java)
11201124
* [TestArmstrong](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/TestArmstrong.java)
11211125
* [TwinPrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/TwinPrimeTest.java)
1126+
* [UniformNumbersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/UniformNumbersTest.java)
11221127
* [VolumeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/VolumeTest.java)
11231128
* misc
11241129
* [ColorContrastRatioTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java)
@@ -1189,6 +1194,7 @@
11891194
* [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java)
11901195
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
11911196
* [ProportionalFairSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/ProportionalFairSchedulingTest.java)
1197+
* [RandomSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RandomSchedulingTest.java)
11921198
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
11931199
* [SelfAdjustingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SelfAdjustingSchedulingTest.java)
11941200
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
@@ -1228,6 +1234,7 @@
12281234
* [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java)
12291235
* [UpperBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UpperBoundTest.java)
12301236
* slidingwindow
1237+
* [LongestSubarrayWithSumLessOrEqualToKTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToKTest.java)
12311238
* [LongestSubstringWithoutRepeatingCharactersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/LongestSubstringWithoutRepeatingCharactersTest.java)
12321239
* [MaxSumKSizeSubarrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarrayTest.java)
12331240
* [MinSumKSizeSubarrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarrayTest.java)

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
<dependency>
126126
<groupId>com.puppycrawl.tools</groupId>
127127
<artifactId>checkstyle</artifactId>
128-
<version>10.18.2</version>
128+
<version>10.19.0</version>
129129
</dependency>
130130
</dependencies>
131131
</plugin>
@@ -153,7 +153,7 @@
153153
<plugin>
154154
<groupId>org.apache.maven.plugins</groupId>
155155
<artifactId>maven-pmd-plugin</artifactId>
156-
<version>3.25.0</version>
156+
<version>3.26.0</version>
157157
<configuration>
158158
<printFailingErrors>true</printFailingErrors>
159159
<includeTests>true</includeTests>

src/main/java/com/thealgorithms/datastructures/trees/AVLTree.java

Lines changed: 82 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package com.thealgorithms.datastructures.trees;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Represents an AVL Tree, a self-balancing binary search tree.
8+
* In an AVL tree, the heights of the two child subtrees of any node
9+
* differ by at most one. If they differ by more than one at any time,
10+
* rebalancing is performed to restore this property.
11+
*/
312
public class AVLTree {
413

514
private Node root;
615

7-
private class Node {
8-
16+
private static class Node {
917
private int key;
1018
private int balance;
1119
private int height;
@@ -17,8 +25,18 @@ private class Node {
1725
key = k;
1826
parent = p;
1927
}
28+
29+
public Integer getBalance() {
30+
return balance;
31+
}
2032
}
2133

34+
/**
35+
* Inserts a new key into the AVL tree.
36+
*
37+
* @param key the key to be inserted
38+
* @return {@code true} if the key was inserted, {@code false} if the key already exists
39+
*/
2240
public boolean insert(int key) {
2341
if (root == null) {
2442
root = new Node(key, null);
@@ -31,7 +49,6 @@ public boolean insert(int key) {
3149
}
3250

3351
parent = n;
34-
3552
boolean goLeft = n.key > key;
3653
n = goLeft ? n.left : n.right;
3754

@@ -49,8 +66,32 @@ public boolean insert(int key) {
4966
return true;
5067
}
5168

69+
/**
70+
* Deletes a key from the AVL tree.
71+
*
72+
* @param delKey the key to be deleted
73+
*/
74+
public void delete(int delKey) {
75+
if (root == null) {
76+
return;
77+
}
78+
79+
// Find the node to be deleted
80+
Node node = root;
81+
Node child = root;
82+
while (child != null) {
83+
node = child;
84+
child = delKey >= node.key ? node.right : node.left;
85+
if (delKey == node.key) {
86+
delete(node);
87+
return;
88+
}
89+
}
90+
}
91+
5292
private void delete(Node node) {
5393
if (node.left == null && node.right == null) {
94+
// Leaf node
5495
if (node.parent == null) {
5596
root = null;
5697
} else {
@@ -64,6 +105,8 @@ private void delete(Node node) {
64105
}
65106
return;
66107
}
108+
109+
// Node has one or two children
67110
Node child;
68111
if (node.left != null) {
69112
child = node.left;
@@ -80,26 +123,49 @@ private void delete(Node node) {
80123
delete(child);
81124
}
82125

83-
public void delete(int delKey) {
84-
if (root == null) {
85-
return;
126+
/**
127+
* Returns a list of balance factors for each node in the tree.
128+
*
129+
* @return a list of integers representing the balance factors of the nodes
130+
*/
131+
public List<Integer> returnBalance() {
132+
List<Integer> balances = new ArrayList<>();
133+
returnBalance(root, balances);
134+
return balances;
135+
}
136+
137+
private void returnBalance(Node n, List<Integer> balances) {
138+
if (n != null) {
139+
returnBalance(n.left, balances);
140+
balances.add(n.getBalance());
141+
returnBalance(n.right, balances);
86142
}
87-
Node node = root;
88-
Node child = root;
143+
}
89144

90-
while (child != null) {
91-
node = child;
92-
child = delKey >= node.key ? node.right : node.left;
93-
if (delKey == node.key) {
94-
delete(node);
95-
return;
96-
}
145+
/**
146+
* Searches for a key in the AVL tree.
147+
*
148+
* @param key the key to be searched
149+
* @return true if the key is found, false otherwise
150+
*/
151+
public boolean search(int key) {
152+
Node result = searchHelper(this.root, key);
153+
return result != null;
154+
}
155+
156+
private Node searchHelper(Node root, int key) {
157+
if (root == null || root.key == key) {
158+
return root;
97159
}
160+
161+
if (root.key > key) {
162+
return searchHelper(root.left, key);
163+
}
164+
return searchHelper(root.right, key);
98165
}
99166

100167
private void rebalance(Node n) {
101168
setBalance(n);
102-
103169
if (n.balance == -2) {
104170
if (height(n.left.left) >= height(n.left.right)) {
105171
n = rotateRight(n);
@@ -143,7 +209,6 @@ private Node rotateLeft(Node a) {
143209
}
144210

145211
setBalance(a, b);
146-
147212
return b;
148213
}
149214

@@ -169,7 +234,6 @@ private Node rotateRight(Node a) {
169234
}
170235

171236
setBalance(a, b);
172-
173237
return b;
174238
}
175239

@@ -197,53 +261,9 @@ private void setBalance(Node... nodes) {
197261
}
198262
}
199263

200-
public void printBalance() {
201-
printBalance(root);
202-
}
203-
204-
private void printBalance(Node n) {
205-
if (n != null) {
206-
printBalance(n.left);
207-
System.out.printf("%s ", n.balance);
208-
printBalance(n.right);
209-
}
210-
}
211-
212264
private void reheight(Node node) {
213265
if (node != null) {
214266
node.height = 1 + Math.max(height(node.left), height(node.right));
215267
}
216268
}
217-
218-
public boolean search(int key) {
219-
Node result = searchHelper(this.root, key);
220-
return result != null;
221-
}
222-
223-
private Node searchHelper(Node root, int key) {
224-
// root is null or key is present at root
225-
if (root == null || root.key == key) {
226-
return root;
227-
}
228-
229-
// key is greater than root's key
230-
if (root.key > key) {
231-
return searchHelper(root.left, key); // call the function on the node's left child
232-
}
233-
// key is less than root's key then
234-
// call the function on the node's right child as it is greater
235-
return searchHelper(root.right, key);
236-
}
237-
238-
public static void main(String[] args) {
239-
AVLTree tree = new AVLTree();
240-
241-
System.out.println("Inserting values 1 to 10");
242-
for (int i = 1; i < 10; i++) {
243-
tree.insert(i);
244-
}
245-
246-
System.out.print("Printing balance: ");
247-
tree.printBalance();
248-
}
249269
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* A positive integer is considered uniform if all
5+
* of its digits are equal. For example, 222 is uniform,
6+
* while 223 is not.
7+
* Given two positive integers a and b, determine the
8+
* number of uniform integers between a and b.
9+
*/
10+
public final class UniformNumbers {
11+
// Private constructor to prevent instantiation of the utility class
12+
private UniformNumbers() {
13+
// Prevent instantiation
14+
}
15+
/**
16+
* This function will find the number of uniform numbers
17+
* from 1 to num
18+
* @param num upper limit to find the uniform numbers
19+
* @return the count of uniform numbers between 1 and num
20+
*/
21+
public static int uniformNumbers(int num) {
22+
String numStr = Integer.toString(num);
23+
int uniformCount = (numStr.length() - 1) * 9;
24+
int finalUniform = Integer.parseInt(String.valueOf(numStr.charAt(0)).repeat(numStr.length()));
25+
26+
if (finalUniform <= num) {
27+
uniformCount += Integer.parseInt(String.valueOf(numStr.charAt(0)));
28+
} else {
29+
uniformCount += Integer.parseInt(String.valueOf(numStr.charAt(0))) - 1;
30+
}
31+
32+
return uniformCount;
33+
}
34+
/**
35+
* This function will calculate the number of uniform numbers
36+
* between a and b
37+
* @param a lower bound of range
38+
* @param b upper bound of range
39+
* @return the count of uniform numbers between a and b
40+
*/
41+
public static int countUniformIntegers(int a, int b) {
42+
if (b > a && b > 0 && a > 0) {
43+
return uniformNumbers(b) - uniformNumbers(a - 1);
44+
} else if (b == a) {
45+
return 1;
46+
} else {
47+
return 0;
48+
}
49+
}
50+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Random;
8+
9+
/**
10+
* RandomScheduling is an algorithm that assigns tasks in a random order.
11+
* It doesn't consider priority, deadlines, or burst times, making it
12+
* inefficient but useful in scenarios where fairness or unpredictability
13+
* is required (e.g., load balancing in distributed systems).
14+
*
15+
* Use Case: Distributed systems where randomness helps avoid task starvation.
16+
*
17+
* @author Hardvan
18+
*/
19+
public final class RandomScheduling {
20+
21+
private final List<String> tasks;
22+
private final Random random;
23+
24+
/**
25+
* Constructs a new RandomScheduling instance.
26+
*
27+
* @param tasks A collection of task names to be scheduled.
28+
* @param random A Random instance for generating random numbers.
29+
*/
30+
public RandomScheduling(Collection<String> tasks, Random random) {
31+
this.tasks = new ArrayList<>(tasks);
32+
this.random = random;
33+
}
34+
35+
/**
36+
* Schedules the tasks randomly and returns the randomized order.
37+
*
38+
* @return A list representing the tasks in their randomized execution order.
39+
*/
40+
public List<String> schedule() {
41+
List<String> shuffledTasks = new ArrayList<>(tasks);
42+
Collections.shuffle(shuffledTasks, random);
43+
return shuffledTasks;
44+
}
45+
}

0 commit comments

Comments
 (0)