diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/breadthfirstsearch/BreadthFirstSearchAlgorithm.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/breadthfirstsearch/BreadthFirstSearchAlgorithm.java index 4331b2fc94..9d301f9578 100644 --- a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/breadthfirstsearch/BreadthFirstSearchAlgorithm.java +++ b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/breadthfirstsearch/BreadthFirstSearchAlgorithm.java @@ -13,10 +13,6 @@ public class BreadthFirstSearchAlgorithm { Queue> queue = new ArrayDeque<>(); queue.add(root); - return searchTreeQueue(value, queue); - } - - private static Optional> searchTreeQueue(T value, Queue> queue) { Tree currentNode; while (!queue.isEmpty()) { currentNode = queue.remove(); @@ -36,10 +32,6 @@ public class BreadthFirstSearchAlgorithm { Queue> queue = new ArrayDeque<>(); queue.add(start); - return searchNodeQueue(value, queue); - } - - private static Optional> searchNodeQueue(T value, Queue> queue) { Node currentNode; Set> alreadyVisited = new HashSet<>(); @@ -51,11 +43,12 @@ public class BreadthFirstSearchAlgorithm { return Optional.of(currentNode); } else { alreadyVisited.add(currentNode); - queue.addAll(currentNode.getNeighbours()); + queue.addAll(currentNode.getNeighbors()); queue.removeAll(alreadyVisited); } } return Optional.empty(); } + } diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/breadthfirstsearch/Node.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/breadthfirstsearch/Node.java index bfa879f0b7..54a589ae26 100644 --- a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/breadthfirstsearch/Node.java +++ b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/breadthfirstsearch/Node.java @@ -7,25 +7,25 @@ import java.util.Set; public class Node { private T value; - private Set> neighbours; + private Set> neighbors; public Node(T value) { this.value = value; - this.neighbours = new HashSet<>(); + this.neighbors = new HashSet<>(); } public T getValue() { return value; } - public Set> getNeighbours() { - return Collections.unmodifiableSet(neighbours); + public Set> getNeighbors() { + return Collections.unmodifiableSet(neighbors); } public void connect(Node node) { if (this == node) throw new IllegalArgumentException("Can't connect node to itself"); - this.neighbours.add(node); - node.neighbours.add(this); + this.neighbors.add(node); + node.neighbors.add(this); } } diff --git a/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/breadthfirstsearch/BreadthFirstSearchAlgorithmUnitTest.java b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/breadthfirstsearch/BreadthFirstSearchAlgorithmUnitTest.java index 188cc32c3e..aa22fc5353 100644 --- a/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/breadthfirstsearch/BreadthFirstSearchAlgorithmUnitTest.java +++ b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/breadthfirstsearch/BreadthFirstSearchAlgorithmUnitTest.java @@ -1,6 +1,5 @@ package com.baeldung.algorithms.breadthfirstsearch; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -13,14 +12,32 @@ class BreadthFirstSearchAlgorithmUnitTest { private Tree rootSecondChild; private Node start; - private Node firstNeighbour; - private Node firstNeighbourNeighbour; - private Node secondNeighbour; + private Node firstNeighbor; + private Node firstNeighborNeighbor; + private Node secondNeighbor; - @BeforeEach - void beforeEach() { + @Test + void givenTree_whenSearchTen_thenRoot() { initTree(); - initNode(); + assertThat(BreadthFirstSearchAlgorithm.search(10, root)).isPresent().contains(root); + } + + @Test + void givenTree_whenSearchThree_thenDepthMostValue() { + initTree(); + assertThat(BreadthFirstSearchAlgorithm.search(3, root)).isPresent().contains(depthMostChild); + } + + @Test + void givenTree_whenSearchFour_thenRootSecondChild() { + initTree(); + assertThat(BreadthFirstSearchAlgorithm.search(4, root)).isPresent().contains(rootSecondChild); + } + + @Test + void givenTree_whenSearchFive_thenNotFound() { + initTree(); + assertThat(BreadthFirstSearchAlgorithm.search(5, root)).isEmpty(); } private void initTree() { @@ -30,56 +47,40 @@ class BreadthFirstSearchAlgorithmUnitTest { rootSecondChild = root.addChild(4); } - private void initNode() { - start = new Node<>(10); - firstNeighbour = new Node<>(2); - start.connect(firstNeighbour); - - firstNeighbourNeighbour = new Node<>(3); - firstNeighbour.connect(firstNeighbourNeighbour); - firstNeighbourNeighbour.connect(start); - - secondNeighbour = new Node<>(4); - start.connect(secondNeighbour); - } - - @Test - void givenTree_whenSearchTen_thenRoot() { - assertThat(BreadthFirstSearchAlgorithm.search(10, root)).isPresent().contains(root); - } - - @Test - void givenTree_whenSearchThree_thenDepthMostValue() { - assertThat(BreadthFirstSearchAlgorithm.search(3, root)).isPresent().contains(depthMostChild); - } - - @Test - void givenTree_whenSearchFour_thenRootSecondChild() { - assertThat(BreadthFirstSearchAlgorithm.search(4, root)).isPresent().contains(rootSecondChild); - } - - @Test - void givenTree_whenSearchFive_thenNotFound() { - assertThat(BreadthFirstSearchAlgorithm.search(5, root)).isEmpty(); - } - @Test void givenNode_whenSearchTen_thenStart() { - assertThat(BreadthFirstSearchAlgorithm.search(10, firstNeighbourNeighbour)).isPresent().contains(start); + initNode(); + assertThat(BreadthFirstSearchAlgorithm.search(10, firstNeighborNeighbor)).isPresent().contains(start); } @Test - void givenNode_whenSearchThree_thenNeighbourNeighbour() { - assertThat(BreadthFirstSearchAlgorithm.search(3, firstNeighbourNeighbour)).isPresent().contains(firstNeighbourNeighbour); + void givenNode_whenSearchThree_thenNeighborNeighbor() { + initNode(); + assertThat(BreadthFirstSearchAlgorithm.search(3, firstNeighborNeighbor)).isPresent().contains(firstNeighborNeighbor); } @Test - void givenNode_whenSearchFour_thenSecondNeighbour() { - assertThat(BreadthFirstSearchAlgorithm.search(4, firstNeighbourNeighbour)).isPresent().contains(secondNeighbour); + void givenNode_whenSearchFour_thenSecondNeighbor() { + initNode(); + assertThat(BreadthFirstSearchAlgorithm.search(4, firstNeighborNeighbor)).isPresent().contains(secondNeighbor); } @Test void givenNode_whenSearchFive_thenNotFound() { - assertThat(BreadthFirstSearchAlgorithm.search(5, firstNeighbourNeighbour)).isEmpty(); + initNode(); + assertThat(BreadthFirstSearchAlgorithm.search(5, firstNeighborNeighbor)).isEmpty(); + } + + private void initNode() { + start = new Node<>(10); + firstNeighbor = new Node<>(2); + start.connect(firstNeighbor); + + firstNeighborNeighbor = new Node<>(3); + firstNeighbor.connect(firstNeighborNeighbor); + firstNeighborNeighbor.connect(start); + + secondNeighbor = new Node<>(4); + start.connect(secondNeighbor); } } \ No newline at end of file