diff --git a/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java b/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java index 93a41871..0cb05f6f 100644 --- a/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java +++ b/src/main/java/com/thealgorithms/searches/BreadthFirstSearch.java @@ -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 queue = new ArrayList<>(node.getSubNodes()); + Queue 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();