Replace List in BFS with Queue (#3231)
This commit is contained in:
parent
c500e8ae5a
commit
9e37775506
@ -2,10 +2,11 @@ package com.thealgorithms.searches;
|
||||
|
||||
import com.thealgorithms.searches.DepthFirstSearch.Node;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* @author: caos321
|
||||
@ -18,10 +19,10 @@ public class BreadthFirstSearch {
|
||||
return Optional.of(node);
|
||||
}
|
||||
|
||||
List<Node> queue = new ArrayList<>(node.getSubNodes());
|
||||
Queue<Node> queue = new ArrayDeque<>(node.getSubNodes());
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
final Node current = queue.get(0);
|
||||
final Node current = queue.poll();
|
||||
|
||||
if (current.getName().equals(name)) {
|
||||
return Optional.of(current);
|
||||
@ -29,7 +30,7 @@ public class BreadthFirstSearch {
|
||||
|
||||
queue.addAll(current.getSubNodes());
|
||||
|
||||
queue.remove(0);
|
||||
queue.remove();
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
|
Loading…
Reference in New Issue
Block a user