Skip to content

Commit 223a767

Browse files
author
dupirefr
committed
[BAERL-3128] Fixes from editor review
1 parent f6e7473 commit 223a767

File tree

3 files changed

+45
-51
lines changed

3 files changed

+45
-51
lines changed

algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/breadthfirstsearch/BreadthFirstSearchAlgorithm.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ public static <T> Optional<Tree<T>> search(T value, Tree<T> root) {
1313
Queue<Tree<T>> queue = new ArrayDeque<>();
1414
queue.add(root);
1515

16-
return searchTreeQueue(value, queue);
17-
}
18-
19-
private static <T> Optional<Tree<T>> searchTreeQueue(T value, Queue<Tree<T>> queue) {
2016
Tree<T> currentNode;
2117
while (!queue.isEmpty()) {
2218
currentNode = queue.remove();
@@ -36,10 +32,6 @@ public static <T> Optional<Node<T>> search(T value, Node<T> start) {
3632
Queue<Node<T>> queue = new ArrayDeque<>();
3733
queue.add(start);
3834

39-
return searchNodeQueue(value, queue);
40-
}
41-
42-
private static <T> Optional<Node<T>> searchNodeQueue(T value, Queue<Node<T>> queue) {
4335
Node<T> currentNode;
4436
Set<Node<T>> alreadyVisited = new HashSet<>();
4537

@@ -51,11 +43,12 @@ private static <T> Optional<Node<T>> searchNodeQueue(T value, Queue<Node<T>> que
5143
return Optional.of(currentNode);
5244
} else {
5345
alreadyVisited.add(currentNode);
54-
queue.addAll(currentNode.getNeighbours());
46+
queue.addAll(currentNode.getNeighbors());
5547
queue.removeAll(alreadyVisited);
5648
}
5749
}
5850

5951
return Optional.empty();
6052
}
53+
6154
}

algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/breadthfirstsearch/Node.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@
77
public class Node<T> {
88

99
private T value;
10-
private Set<Node<T>> neighbours;
10+
private Set<Node<T>> neighbors;
1111

1212
public Node(T value) {
1313
this.value = value;
14-
this.neighbours = new HashSet<>();
14+
this.neighbors = new HashSet<>();
1515
}
1616

1717
public T getValue() {
1818
return value;
1919
}
2020

21-
public Set<Node<T>> getNeighbours() {
22-
return Collections.unmodifiableSet(neighbours);
21+
public Set<Node<T>> getNeighbors() {
22+
return Collections.unmodifiableSet(neighbors);
2323
}
2424

2525
public void connect(Node<T> node) {
2626
if (this == node) throw new IllegalArgumentException("Can't connect node to itself");
27-
this.neighbours.add(node);
28-
node.neighbours.add(this);
27+
this.neighbors.add(node);
28+
node.neighbors.add(this);
2929
}
3030

3131
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baeldung.algorithms.breadthfirstsearch;
22

3-
import org.junit.jupiter.api.BeforeEach;
43
import org.junit.jupiter.api.Test;
54

65
import static org.assertj.core.api.Assertions.assertThat;
@@ -13,73 +12,75 @@ class BreadthFirstSearchAlgorithmUnitTest {
1312
private Tree<Integer> rootSecondChild;
1413

1514
private Node<Integer> start;
16-
private Node<Integer> firstNeighbour;
17-
private Node<Integer> firstNeighbourNeighbour;
18-
private Node<Integer> secondNeighbour;
19-
20-
@BeforeEach
21-
void beforeEach() {
22-
initTree();
23-
initNode();
24-
}
25-
26-
private void initTree() {
27-
root = Tree.of(10);
28-
rootFirstChild = root.addChild(2);
29-
depthMostChild = rootFirstChild.addChild(3);
30-
rootSecondChild = root.addChild(4);
31-
}
32-
33-
private void initNode() {
34-
start = new Node<>(10);
35-
firstNeighbour = new Node<>(2);
36-
start.connect(firstNeighbour);
37-
38-
firstNeighbourNeighbour = new Node<>(3);
39-
firstNeighbour.connect(firstNeighbourNeighbour);
40-
firstNeighbourNeighbour.connect(start);
41-
42-
secondNeighbour = new Node<>(4);
43-
start.connect(secondNeighbour);
44-
}
15+
private Node<Integer> firstNeighbor;
16+
private Node<Integer> firstNeighborNeighbor;
17+
private Node<Integer> secondNeighbor;
4518

4619
@Test
4720
void givenTree_whenSearchTen_thenRoot() {
21+
initTree();
4822
assertThat(BreadthFirstSearchAlgorithm.search(10, root)).isPresent().contains(root);
4923
}
5024

5125
@Test
5226
void givenTree_whenSearchThree_thenDepthMostValue() {
27+
initTree();
5328
assertThat(BreadthFirstSearchAlgorithm.search(3, root)).isPresent().contains(depthMostChild);
5429
}
5530

5631
@Test
5732
void givenTree_whenSearchFour_thenRootSecondChild() {
33+
initTree();
5834
assertThat(BreadthFirstSearchAlgorithm.search(4, root)).isPresent().contains(rootSecondChild);
5935
}
6036

6137
@Test
6238
void givenTree_whenSearchFive_thenNotFound() {
39+
initTree();
6340
assertThat(BreadthFirstSearchAlgorithm.search(5, root)).isEmpty();
6441
}
6542

43+
private void initTree() {
44+
root = Tree.of(10);
45+
rootFirstChild = root.addChild(2);
46+
depthMostChild = rootFirstChild.addChild(3);
47+
rootSecondChild = root.addChild(4);
48+
}
49+
6650
@Test
6751
void givenNode_whenSearchTen_thenStart() {
68-
assertThat(BreadthFirstSearchAlgorithm.search(10, firstNeighbourNeighbour)).isPresent().contains(start);
52+
initNode();
53+
assertThat(BreadthFirstSearchAlgorithm.search(10, firstNeighborNeighbor)).isPresent().contains(start);
6954
}
7055

7156
@Test
72-
void givenNode_whenSearchThree_thenNeighbourNeighbour() {
73-
assertThat(BreadthFirstSearchAlgorithm.search(3, firstNeighbourNeighbour)).isPresent().contains(firstNeighbourNeighbour);
57+
void givenNode_whenSearchThree_thenNeighborNeighbor() {
58+
initNode();
59+
assertThat(BreadthFirstSearchAlgorithm.search(3, firstNeighborNeighbor)).isPresent().contains(firstNeighborNeighbor);
7460
}
7561

7662
@Test
77-
void givenNode_whenSearchFour_thenSecondNeighbour() {
78-
assertThat(BreadthFirstSearchAlgorithm.search(4, firstNeighbourNeighbour)).isPresent().contains(secondNeighbour);
63+
void givenNode_whenSearchFour_thenSecondNeighbor() {
64+
initNode();
65+
assertThat(BreadthFirstSearchAlgorithm.search(4, firstNeighborNeighbor)).isPresent().contains(secondNeighbor);
7966
}
8067

8168
@Test
8269
void givenNode_whenSearchFive_thenNotFound() {
83-
assertThat(BreadthFirstSearchAlgorithm.search(5, firstNeighbourNeighbour)).isEmpty();
70+
initNode();
71+
assertThat(BreadthFirstSearchAlgorithm.search(5, firstNeighborNeighbor)).isEmpty();
72+
}
73+
74+
private void initNode() {
75+
start = new Node<>(10);
76+
firstNeighbor = new Node<>(2);
77+
start.connect(firstNeighbor);
78+
79+
firstNeighborNeighbor = new Node<>(3);
80+
firstNeighbor.connect(firstNeighborNeighbor);
81+
firstNeighborNeighbor.connect(start);
82+
83+
secondNeighbor = new Node<>(4);
84+
start.connect(secondNeighbor);
8485
}
8586
}

0 commit comments

Comments
 (0)