Skip to content

Optimize BreadthFirstSearch implementation with bug fixes and improvements #5882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,54 @@
import com.thealgorithms.datastructures.Node;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;

/**
* @author: caos321
* @date: 31 October 2021 (Sunday)
* @wiki: https://en.wikipedia.org/wiki/Breadth-first_search
* Breadth-First Search implementation for tree/graph traversal.
* @author caos321
* @co-author @manishraj27
* @see <a href="https://en.wikipedia.org/wiki/Breadth-first_search">Breadth-first search</a>
*/
public class BreadthFirstSearch<T> {

private final List<T> visited = new ArrayList<>();
private final Set<T> visitedSet = new HashSet<>();

public Optional<Node<T>> search(final Node<T> node, final T value) {
if (node == null) {
/**
* Performs a breadth-first search to find a node with the given value.
*
* @param root The root node to start the search from
* @param value The value to search for
* @return Optional containing the found node, or empty if not found
*/
public Optional<Node<T>> search(final Node<T> root, final T value) {
if (root == null) {
return Optional.empty();
}
if (node.getValue().equals(value)) {
// add root node to visited
visited.add(value);
return Optional.of(node);
}
visited.add(node.getValue());

Queue<Node<T>> queue = new ArrayDeque<>(node.getChildren());
visited.add(root.getValue());
visitedSet.add(root.getValue());

if (root.getValue() == value) {
return Optional.of(root);
}

Queue<Node<T>> queue = new ArrayDeque<>(root.getChildren());
while (!queue.isEmpty()) {
final Node<T> current = queue.poll();
visited.add(current.getValue());
T currentValue = current.getValue();

if (visitedSet.contains(currentValue)) {
continue;
}

visited.add(currentValue);
visitedSet.add(currentValue);

if (current.getValue().equals(value)) {
if (currentValue == value || (value != null && value.equals(currentValue))) {
return Optional.of(current);
}

Expand All @@ -43,6 +60,11 @@ public Optional<Node<T>> search(final Node<T> node, final T value) {
return Optional.empty();
}

/**
* Returns the list of nodes in the order they were visited.
*
* @return List containing the visited nodes
*/
public List<T> getVisited() {
return visited;
}
Expand Down