### Question:
TarjanLowestCommonAncestor { public V calculate(V start, V a, V b) { List<LcaRequestResponse<V>> list = new LinkedList<>(); list.add(new LcaRequestResponse<>(a, b)); return calculate(start, list).get(0); } TarjanLowestCommonAncestor(Graph<V, E> g); V calculate(V start, V a, V b); List<V> calculate(V start, List<LcaRequestResponse<V>> lrr);  }### Answer:
@Test public void testBinaryTree() { Graph<String, DefaultEdge> g = new DefaultDirectedGraph<>(DefaultEdge.class); g.addVertex("a"); g.addVertex("b"); g.addVertex("c"); g.addVertex("d"); g.addVertex("e"); g.addEdge("a", "b"); g.addEdge("b", "c"); g.addEdge("b", "d"); g.addEdge("d", "e"); Assert.assertEquals("b", new TarjanLowestCommonAncestor<>(g).calculate("a", "c", "e")); Assert.assertEquals("b", new TarjanLowestCommonAncestor<>(g).calculate("a", "b", "d")); Assert.assertEquals("d", new TarjanLowestCommonAncestor<>(g).calculate("a", "d", "e")); }
@Test public void testNonBinaryTree() { Graph<String, DefaultEdge> g = new DefaultDirectedGraph<>(DefaultEdge.class); g.addVertex("a"); g.addVertex("b"); g.addVertex("c"); g.addVertex("d"); g.addVertex("e"); g.addVertex("f"); g.addVertex("g"); g.addVertex("h"); g.addVertex("i"); g.addVertex("j"); g.addEdge("a", "b"); g.addEdge("b", "c"); g.addEdge("c", "d"); g.addEdge("d", "e"); g.addEdge("b", "f"); g.addEdge("b", "g"); g.addEdge("c", "h"); g.addEdge("c", "i"); g.addEdge("i", "j"); Assert.assertEquals("b", new TarjanLowestCommonAncestor<>(g).calculate("a", "b", "h")); Assert.assertEquals("b", new TarjanLowestCommonAncestor<>(g).calculate("a", "j", "f")); Assert.assertEquals("c", new TarjanLowestCommonAncestor<>(g).calculate("a", "j", "h")); LcaRequestResponse<String> bg = new LcaRequestResponse<>("b", "h"); LcaRequestResponse<String> ed = new LcaRequestResponse<>("j", "f"); LcaRequestResponse<String> fd = new LcaRequestResponse<>("j", "h"); List<LcaRequestResponse<String>> list = new LinkedList<>(); list.add(bg); list.add(ed); list.add(fd); List<String> result = new TarjanLowestCommonAncestor<>(g).calculate("a", list); Assert.assertEquals("b", bg.getLca()); Assert.assertEquals("b", ed.getLca()); Assert.assertEquals("c", fd.getLca()); Assert.assertEquals(Arrays.asList(new String[] { "b", "b", "c" }), result); Assert.assertEquals("b", new TarjanLowestCommonAncestor<>(g).calculate("b", "h", "b")); }
@Test public void testOneNode() { Graph<String, DefaultEdge> g = new DefaultDirectedGraph<>(DefaultEdge.class); g.addVertex("a"); Assert.assertEquals("a", new TarjanLowestCommonAncestor<>(g).calculate("a", "a", "a")); }