[BAERL-3128] Fixes from editor review
This commit is contained in:
@@ -13,10 +13,6 @@ public class BreadthFirstSearchAlgorithm {
|
|||||||
Queue<Tree<T>> queue = new ArrayDeque<>();
|
Queue<Tree<T>> queue = new ArrayDeque<>();
|
||||||
queue.add(root);
|
queue.add(root);
|
||||||
|
|
||||||
return searchTreeQueue(value, queue);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static <T> Optional<Tree<T>> searchTreeQueue(T value, Queue<Tree<T>> queue) {
|
|
||||||
Tree<T> currentNode;
|
Tree<T> currentNode;
|
||||||
while (!queue.isEmpty()) {
|
while (!queue.isEmpty()) {
|
||||||
currentNode = queue.remove();
|
currentNode = queue.remove();
|
||||||
@@ -36,10 +32,6 @@ public class BreadthFirstSearchAlgorithm {
|
|||||||
Queue<Node<T>> queue = new ArrayDeque<>();
|
Queue<Node<T>> queue = new ArrayDeque<>();
|
||||||
queue.add(start);
|
queue.add(start);
|
||||||
|
|
||||||
return searchNodeQueue(value, queue);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static <T> Optional<Node<T>> searchNodeQueue(T value, Queue<Node<T>> queue) {
|
|
||||||
Node<T> currentNode;
|
Node<T> currentNode;
|
||||||
Set<Node<T>> alreadyVisited = new HashSet<>();
|
Set<Node<T>> alreadyVisited = new HashSet<>();
|
||||||
|
|
||||||
@@ -51,11 +43,12 @@ public class BreadthFirstSearchAlgorithm {
|
|||||||
return Optional.of(currentNode);
|
return Optional.of(currentNode);
|
||||||
} else {
|
} else {
|
||||||
alreadyVisited.add(currentNode);
|
alreadyVisited.add(currentNode);
|
||||||
queue.addAll(currentNode.getNeighbours());
|
queue.addAll(currentNode.getNeighbors());
|
||||||
queue.removeAll(alreadyVisited);
|
queue.removeAll(alreadyVisited);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,25 +7,25 @@ import java.util.Set;
|
|||||||
public class Node<T> {
|
public class Node<T> {
|
||||||
|
|
||||||
private T value;
|
private T value;
|
||||||
private Set<Node<T>> neighbours;
|
private Set<Node<T>> neighbors;
|
||||||
|
|
||||||
public Node(T value) {
|
public Node(T value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.neighbours = new HashSet<>();
|
this.neighbors = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public T getValue() {
|
public T getValue() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Node<T>> getNeighbours() {
|
public Set<Node<T>> getNeighbors() {
|
||||||
return Collections.unmodifiableSet(neighbours);
|
return Collections.unmodifiableSet(neighbors);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect(Node<T> node) {
|
public void connect(Node<T> node) {
|
||||||
if (this == node) throw new IllegalArgumentException("Can't connect node to itself");
|
if (this == node) throw new IllegalArgumentException("Can't connect node to itself");
|
||||||
this.neighbours.add(node);
|
this.neighbors.add(node);
|
||||||
node.neighbours.add(this);
|
node.neighbors.add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.algorithms.breadthfirstsearch;
|
package com.baeldung.algorithms.breadthfirstsearch;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@@ -13,14 +12,32 @@ class BreadthFirstSearchAlgorithmUnitTest {
|
|||||||
private Tree<Integer> rootSecondChild;
|
private Tree<Integer> rootSecondChild;
|
||||||
|
|
||||||
private Node<Integer> start;
|
private Node<Integer> start;
|
||||||
private Node<Integer> firstNeighbour;
|
private Node<Integer> firstNeighbor;
|
||||||
private Node<Integer> firstNeighbourNeighbour;
|
private Node<Integer> firstNeighborNeighbor;
|
||||||
private Node<Integer> secondNeighbour;
|
private Node<Integer> secondNeighbor;
|
||||||
|
|
||||||
@BeforeEach
|
@Test
|
||||||
void beforeEach() {
|
void givenTree_whenSearchTen_thenRoot() {
|
||||||
initTree();
|
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() {
|
private void initTree() {
|
||||||
@@ -30,56 +47,40 @@ class BreadthFirstSearchAlgorithmUnitTest {
|
|||||||
rootSecondChild = root.addChild(4);
|
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
|
@Test
|
||||||
void givenNode_whenSearchTen_thenStart() {
|
void givenNode_whenSearchTen_thenStart() {
|
||||||
assertThat(BreadthFirstSearchAlgorithm.search(10, firstNeighbourNeighbour)).isPresent().contains(start);
|
initNode();
|
||||||
|
assertThat(BreadthFirstSearchAlgorithm.search(10, firstNeighborNeighbor)).isPresent().contains(start);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenNode_whenSearchThree_thenNeighbourNeighbour() {
|
void givenNode_whenSearchThree_thenNeighborNeighbor() {
|
||||||
assertThat(BreadthFirstSearchAlgorithm.search(3, firstNeighbourNeighbour)).isPresent().contains(firstNeighbourNeighbour);
|
initNode();
|
||||||
|
assertThat(BreadthFirstSearchAlgorithm.search(3, firstNeighborNeighbor)).isPresent().contains(firstNeighborNeighbor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenNode_whenSearchFour_thenSecondNeighbour() {
|
void givenNode_whenSearchFour_thenSecondNeighbor() {
|
||||||
assertThat(BreadthFirstSearchAlgorithm.search(4, firstNeighbourNeighbour)).isPresent().contains(secondNeighbour);
|
initNode();
|
||||||
|
assertThat(BreadthFirstSearchAlgorithm.search(4, firstNeighborNeighbor)).isPresent().contains(secondNeighbor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenNode_whenSearchFive_thenNotFound() {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user