|
3 | 3 | import com.thealgorithms.datastructures.Node;
|
4 | 4 | import java.util.ArrayDeque;
|
5 | 5 | import java.util.ArrayList;
|
| 6 | +import java.util.HashSet; |
6 | 7 | import java.util.List;
|
7 | 8 | import java.util.Optional;
|
8 | 9 | import java.util.Queue;
|
| 10 | +import java.util.Set; |
9 | 11 |
|
10 | 12 | /**
|
11 |
| - * @author: caos321 |
12 |
| - * @date: 31 October 2021 (Sunday) |
13 |
| - * @wiki: https://en.wikipedia.org/wiki/Breadth-first_search |
| 13 | + * Breadth-First Search implementation for tree/graph traversal. |
| 14 | + * @author caos321 |
| 15 | + * @co-author manishraj27 |
| 16 | + * @see <a href="https://en.wikipedia.org/wiki/Breadth-first_search">Breadth-first search</a> |
14 | 17 | */
|
15 | 18 | public class BreadthFirstSearch<T> {
|
16 | 19 |
|
17 | 20 | private final List<T> visited = new ArrayList<>();
|
| 21 | + private final Set<T> visitedSet = new HashSet<>(); |
18 | 22 |
|
19 |
| - public Optional<Node<T>> search(final Node<T> node, final T value) { |
20 |
| - if (node == null) { |
| 23 | + /** |
| 24 | + * Performs a breadth-first search to find a node with the given value. |
| 25 | + * |
| 26 | + * @param root The root node to start the search from |
| 27 | + * @param value The value to search for |
| 28 | + * @return Optional containing the found node, or empty if not found |
| 29 | + */ |
| 30 | + public Optional<Node<T>> search(final Node<T> root, final T value) { |
| 31 | + if (value == null || root == null) { |
21 | 32 | return Optional.empty();
|
22 | 33 | }
|
23 |
| - if (node.getValue().equals(value)) { |
24 |
| - // add root node to visited |
25 |
| - visited.add(value); |
26 |
| - return Optional.of(node); |
27 |
| - } |
28 |
| - visited.add(node.getValue()); |
29 | 34 |
|
30 |
| - Queue<Node<T>> queue = new ArrayDeque<>(node.getChildren()); |
| 35 | + visited.clear(); |
| 36 | + visitedSet.clear(); |
| 37 | + |
| 38 | + Queue<Node<T>> queue = new ArrayDeque<>(); |
| 39 | + queue.offer(root); |
| 40 | + visitedSet.add(root.getValue()); |
| 41 | + visited.add(root.getValue()); |
31 | 42 |
|
32 | 43 | while (!queue.isEmpty()) {
|
33 | 44 | final Node<T> current = queue.poll();
|
34 |
| - visited.add(current.getValue()); |
35 | 45 |
|
36 |
| - if (current.getValue().equals(value)) { |
| 46 | + if (value.equals(current.getValue())) { |
37 | 47 | return Optional.of(current);
|
38 | 48 | }
|
39 | 49 |
|
40 |
| - queue.addAll(current.getChildren()); |
| 50 | + for (Node<T> child : current.getChildren()) { |
| 51 | + if (child != null && !visitedSet.contains(child.getValue())) { |
| 52 | + queue.offer(child); |
| 53 | + visitedSet.add(child.getValue()); |
| 54 | + visited.add(child.getValue()); |
| 55 | + } |
| 56 | + } |
41 | 57 | }
|
42 |
| - |
43 | 58 | return Optional.empty();
|
44 | 59 | }
|
45 | 60 |
|
| 61 | + /** |
| 62 | + * Returns the list of nodes in the order they were visited. |
| 63 | + * |
| 64 | + * @return A new list containing the visited nodes |
| 65 | + */ |
46 | 66 | public List<T> getVisited() {
|
47 |
| - return visited; |
| 67 | + return new ArrayList<>(visited); |
48 | 68 | }
|
49 | 69 | }
|
0 commit comments