Graph implements Serializable { public boolean removeNode(T object) throws NodeNotExistsExeption { Node<T> node = findNode(object); if (node == null) throw new NodeNotExistsExeption(object == null ? "null" : object.toString()); List<Node<T>> ascendingNodes = getAscendingNodes(node); for (Node<T> ascendingNode : ascendingNodes) { ArrayList<Edge<T>> outputEdges = ascendingNode.getOutputEdges(); ArrayList<Node<T>> nodesToRemove = new ArrayList<Node<T>>( outputEdges.size()); for (Edge<T> edge : outputEdges) { nodesToRemove.add(edge.getEndNode()); } for (Node<T> node2 : nodesToRemove) { ascendingNode.removeEdge(node2); } nodes.remove(ascendingNode); rootNodes.remove(ascendingNode); } nodes.remove(node); rootNodes.remove(node); for (Node<T> remainedNode : nodes) { if (remainedNode.getInputEdges().isEmpty() && rootNodes.contains(remainedNode) == false) rootNodes.add(remainedNode); } return true; } Graph(); List<Node<T>> getDescendingNodes(Node<T> node); List<T> getNodesByDFSSearch(); List<Node<T>> nodesAffectedByRemoval(Node<T> node); boolean hasCycle(); Node<T> addNode(T object); void addEdge(T startObject, T endObject); Node<T> findRootNode(Node<T> startNode); boolean removeNode(T object); Node<T> findNode(T object); ArrayList<Node<T>> getNodes(); ArrayList<Node<T>> getRootNodes(); @Override String toString();  }
@Test public void testRemoveNode() throws NodeNotExistsExeption{ Graph<String> graph = new Graph<String>(); graph.addNode("n1"); graph.addNode("n2"); graph.addNode("n3"); graph.addNode("n4"); graph.addNode("n5"); graph.addNode("n6"); graph.addEdge("n2", "n3"); graph.addEdge("n3", "n4"); graph.addEdge("n1", "n5"); graph.addEdge("n2", "n6"); printGraph(graph, "testRemoveNode1"); graph.removeNode("n4"); assertNull(graph.findNode("n2")); assertNotNull(graph.findNode("n6")); printGraph(graph, "testRemoveNode2"); }