Merge pull request #7026 from eugenp/graph-fix

fix remove vertex
This commit is contained in:
Loredana Crusoveanu 2019-05-29 21:56:53 +03:00 committed by GitHub
commit 6e4e729156
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 5 deletions

View File

@ -19,7 +19,7 @@ public class Graph {
void removeVertex(String label) { void removeVertex(String label) {
Vertex v = new Vertex(label); Vertex v = new Vertex(label);
adjVertices.values().stream().map(e -> e.remove(v)).collect(Collectors.toList()); adjVertices.values().stream().forEach(e -> e.remove(v));
adjVertices.remove(new Vertex(label)); adjVertices.remove(new Vertex(label));
} }

View File

@ -1,20 +1,31 @@
package com.baeldung.graph; package com.baeldung.graph;
import org.junit.Assert; import static org.junit.Assert.assertEquals;
import org.junit.Test; import org.junit.Test;
public class GraphTraversalUnitTest { public class GraphUnitTest {
@Test @Test
public void givenAGraph_whenTraversingDepthFirst_thenExpectedResult() { public void givenAGraph_whenTraversingDepthFirst_thenExpectedResult() {
Graph graph = createGraph(); Graph graph = createGraph();
Assert.assertEquals("[Bob, Rob, Maria, Alice, Mark]", assertEquals("[Bob, Rob, Maria, Alice, Mark]",
GraphTraversal.depthFirstTraversal(graph, "Bob").toString()); GraphTraversal.depthFirstTraversal(graph, "Bob").toString());
} }
@Test @Test
public void givenAGraph_whenTraversingBreadthFirst_thenExpectedResult() { public void givenAGraph_whenTraversingBreadthFirst_thenExpectedResult() {
Graph graph = createGraph(); Graph graph = createGraph();
Assert.assertEquals("[Bob, Alice, Rob, Mark, Maria]", assertEquals("[Bob, Alice, Rob, Mark, Maria]",
GraphTraversal.breadthFirstTraversal(graph, "Bob").toString());
}
@Test
public void givenAGraph_whenRemoveVertex_thenVertedNotFound() {
Graph graph = createGraph();
assertEquals("[Bob, Alice, Rob, Mark, Maria]",
GraphTraversal.breadthFirstTraversal(graph, "Bob").toString());
graph.removeVertex("Maria");
assertEquals("[Bob, Alice, Rob, Mark]",
GraphTraversal.breadthFirstTraversal(graph, "Bob").toString()); GraphTraversal.breadthFirstTraversal(graph, "Bob").toString());
} }