commit
6e4e729156
@ -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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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());
|
||||||
}
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user