Skip to content

Commit b1d3ce0

Browse files
Merge pull request amjadparacha#1 from eugenp/master
syncing
2 parents a0cbfa3 + b871797 commit b1d3ce0

File tree

1,346 files changed

+9747
-5274
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,346 files changed

+9747
-5274
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ out/
3030
.DS_Store
3131

3232
# Maven
33-
log/
33+
log/*
3434
target/
3535

3636
# Gradle
@@ -83,4 +83,5 @@ jta/transaction-logs/
8383
software-security/sql-injection-samples/derby.log
8484
spring-soap/src/main/java/com/baeldung/springsoap/gen/
8585
/report-*.json
86-
transaction.log
86+
transaction.log
87+
*-shell.log

akka-http/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
## Relevant articles:
1+
## Akka HTTP
2+
3+
This module contains articles about Akka HTTP.
4+
5+
### Relevant articles:
26

37
- [Introduction to Akka HTTP](https://www.baeldung.com/akka-http)

akka-streams/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Akka Streams
2+
3+
This module contains articles about Akka Streams.
4+
15
### Relevant articles
26

37
- [Guide to Akka Streams](https://www.baeldung.com/akka-streams)

algorithms-genetic/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## Relevant articles:
1+
## Genetic Algorithms
2+
3+
This module contains articles about genetic algorithms.
4+
5+
### Relevant articles:
26

37
- [Introduction to Jenetics Library](https://www.baeldung.com/jenetics)
48
- [Ant Colony Optimization](https://www.baeldung.com/java-ant-colony-optimization)

algorithms-miscellaneous-1/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
## Relevant articles:
1+
## Algorithms - Miscellaneous
2+
3+
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/../algorithms-sorting) and
4+
[genetic algorithms](/../algorithms-genetic), have their own dedicated modules.
5+
6+
### Relevant articles:
27

38
- [Validating Input With Finite Automata in Java](https://www.baeldung.com/java-finite-automata)
49
- [Example of Hill Climbing Algorithm](https://www.baeldung.com/java-hill-climbing-algorithm)
510
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](https://www.baeldung.com/java-monte-carlo-tree-search)
611
- [Binary Search Algorithm in Java](https://www.baeldung.com/java-binary-search)
712
- [Introduction to Minimax Algorithm](https://www.baeldung.com/java-minimax-algorithm)
813
- [How to Calculate Levenshtein Distance in Java?](https://www.baeldung.com/java-levenshtein-distance)
9-
- [How to Find the Kth Largest Element in Java](https://www.baeldung.com/java-kth-largest-element)
14+
- [How to Find the Kth Largest Element in Java](https://www.baeldung.com/java-kth-largest-element)
15+
- More articles: [[next -->]](/../algorithms-miscellaneous-2)

algorithms-miscellaneous-2/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
## Relevant articles:
1+
## Algorithms - Miscellaneous
2+
3+
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/../algorithms-sorting) and
4+
[genetic algorithms](/../algorithms-genetic), have their own dedicated modules.
5+
6+
### Relevant articles:
27

38
- [Dijkstra Shortest Path Algorithm in Java](https://www.baeldung.com/java-dijkstra)
49
- [Introduction to Cobertura](https://www.baeldung.com/cobertura)
@@ -8,3 +13,4 @@
813
- [Create a Sudoku Solver in Java](https://www.baeldung.com/java-sudoku)
914
- [Displaying Money Amounts in Words](https://www.baeldung.com/java-money-into-words)
1015
- [A Collaborative Filtering Recommendation System in Java](https://www.baeldung.com/java-collaborative-filtering-recommendations)
16+
- More articles: [[<-- prev]](/../algorithms-miscellaneous-1) [[next -->]](/../algorithms-miscellaneous-3)

algorithms-miscellaneous-3/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Algorithms - Miscellaneous
2+
3+
This module contains articles about algorithms. Some classes of algorithms, e.g., [sorting](/algorithms-sorting) and
4+
[genetic algorithms](/algorithms-genetic), have their own dedicated modules.
5+
16
## Relevant Articles:
27

38
- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique)
@@ -9,3 +14,6 @@
914
- [A Guide to the Folding Technique in Java](https://www.baeldung.com/folding-hashing-technique)
1015
- [Creating a Triangle with for Loops in Java](https://www.baeldung.com/java-print-triangle)
1116
- [Efficient Word Frequency Calculator in Java](https://www.baeldung.com/java-word-frequency)
17+
- [Interpolation Search in Java](https://www.baeldung.com/java-interpolation-search)
18+
- [The K-Means Clustering Algorithm in Java](https://www.baeldung.com/java-k-means-clustering-algorithm)
19+
- More articles: [[<-- prev]](/algorithms-miscellaneous-2) [[next -->]](/algorithms-miscellaneous-4)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.algorithms.breadthfirstsearch;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.*;
7+
8+
public class BreadthFirstSearchAlgorithm {
9+
10+
private static final Logger LOGGER = LoggerFactory.getLogger(BreadthFirstSearchAlgorithm.class);
11+
12+
public static <T> Optional<Tree<T>> search(T value, Tree<T> root) {
13+
Queue<Tree<T>> queue = new ArrayDeque<>();
14+
queue.add(root);
15+
16+
Tree<T> currentNode;
17+
while (!queue.isEmpty()) {
18+
currentNode = queue.remove();
19+
LOGGER.info("Visited node with value: {}", currentNode.getValue());
20+
21+
if (currentNode.getValue().equals(value)) {
22+
return Optional.of(currentNode);
23+
} else {
24+
queue.addAll(currentNode.getChildren());
25+
}
26+
}
27+
28+
return Optional.empty();
29+
}
30+
31+
public static <T> Optional<Node<T>> search(T value, Node<T> start) {
32+
Queue<Node<T>> queue = new ArrayDeque<>();
33+
queue.add(start);
34+
35+
Node<T> currentNode;
36+
Set<Node<T>> alreadyVisited = new HashSet<>();
37+
38+
while (!queue.isEmpty()) {
39+
currentNode = queue.remove();
40+
LOGGER.info("Visited node with value: {}", currentNode.getValue());
41+
42+
if (currentNode.getValue().equals(value)) {
43+
return Optional.of(currentNode);
44+
} else {
45+
alreadyVisited.add(currentNode);
46+
queue.addAll(currentNode.getNeighbors());
47+
queue.removeAll(alreadyVisited);
48+
}
49+
}
50+
51+
return Optional.empty();
52+
}
53+
54+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.algorithms.breadthfirstsearch;
2+
3+
import java.util.Collections;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class Node<T> {
8+
9+
private T value;
10+
private Set<Node<T>> neighbors;
11+
12+
public Node(T value) {
13+
this.value = value;
14+
this.neighbors = new HashSet<>();
15+
}
16+
17+
public T getValue() {
18+
return value;
19+
}
20+
21+
public Set<Node<T>> getNeighbors() {
22+
return Collections.unmodifiableSet(neighbors);
23+
}
24+
25+
public void connect(Node<T> node) {
26+
if (this == node) throw new IllegalArgumentException("Can't connect node to itself");
27+
this.neighbors.add(node);
28+
node.neighbors.add(this);
29+
}
30+
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.algorithms.breadthfirstsearch;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class Tree<T> {
8+
9+
private T value;
10+
private List<Tree<T>> children;
11+
12+
private Tree(T value) {
13+
this.value = value;
14+
this.children = new ArrayList<>();
15+
}
16+
17+
public static <T> Tree<T> of(T value) {
18+
return new Tree<>(value);
19+
}
20+
21+
public T getValue() {
22+
return value;
23+
}
24+
25+
public List<Tree<T>> getChildren() {
26+
return Collections.unmodifiableList(children);
27+
}
28+
29+
public Tree<T> addChild(T value) {
30+
Tree<T> newChild = new Tree<>(value);
31+
children.add(newChild);
32+
return newChild;
33+
}
34+
}

0 commit comments

Comments
 (0)