### Question:
TrieNode { public void insert(String needle) { TrieNode traverseNode = this, lastNode = null; int lastElementsFound = 0; int elementsFound = 0; int treeLength = 0; TrieEdge nextEdge = null; while (elementsFound == treeLength && (nextEdge = traverseNode.findOne(needle, elementsFound)) != null && !traverseNode.leaf()) { lastNode = traverseNode; traverseNode = nextEdge.target; lastElementsFound = elementsFound; elementsFound += shares(needle.substring(elementsFound), nextEdge.label).length(); treeLength += nextEdge.label.length(); } if (elementsFound < needle.length()) { if (elementsFound < treeLength) { String needleSub = needle.substring(elementsFound); TrieEdge newEdge = new TrieEdge(needleSub); TrieEdge earlyEdge = new TrieEdge(needle.substring(lastElementsFound, elementsFound)); earlyEdge.target.edges.add(newEdge); int i = 0; for (; i < lastNode.edges.size(); i++) { TrieEdge thisEdge = lastNode.edges.get(i); if (thisEdge.label.startsWith(earlyEdge.label)) { String teSub = thisEdge.label.substring(elementsFound - lastElementsFound); if (teSub.length() > 0) { TrieEdge latest = new TrieEdge(teSub); earlyEdge.target.edges.add(latest); lastNode.edges.remove(thisEdge); for (TrieEdge edge : thisEdge.target.edges) { latest.target.edges.add(edge); } } else { lastNode.edges.remove(thisEdge); } break; } } lastNode.edges.add(earlyEdge); } else if (elementsFound == treeLength) { TrieEdge trieEdge = new TrieEdge(needle.substring(elementsFound)); traverseNode.edges.add(trieEdge); } } else if (elementsFound == needle.length() && elementsFound < treeLength) { TrieEdge firstEdge = new TrieEdge(needle.substring(lastElementsFound, elementsFound)); TrieEdge edgeReplace = new TrieEdge(nextEdge.label.substring(firstEdge.label.length())); lastNode.edges.add(firstEdge); edgeReplace.target = nextEdge.target; firstEdge.target.edges.add(edgeReplace); String needleSub = needle.substring(firstEdge.label.length()); if (needleSub.length() > 0) { TrieEdge newEdge = new TrieEdge(needleSub); firstEdge.target.edges.add(newEdge); } lastNode.edges.remove(nextEdge); } }  TrieEdge getEdge(int i); String edgeLabel(int i); TrieNode getEdgeTarget(int i); boolean edgeTargetLeaf(int i); int countEdges(); boolean leaf(); static boolean prefix(String needle, String label, int elementsFound); boolean lookup(String needle); void insert(String needle);  }### Answer:
@Test public void testTreeInsertsProperlySearch() throws Exception { TrieNode trie = new TrieNode(); trie.insert("search"); trie.insert("searches"); trie.insert("searched"); trie.insert("searching"); assertEquals(1, trie.edges.size()); assertEquals("search", trie.edges.get(0).label); assertEquals(2, trie.edges.get(0).targetEdgesSize()); assertEquals("e", trie.edges.get(0).targetEdge(0).label); assertEquals(2, trie.edges.get(0).targetEdge(0).targetEdgesSize()); assertEquals("d", trie.edges.get(0).targetEdge(0).targetEdge(0).label); assertEquals("s", trie.edges.get(0).targetEdge(0).targetEdge(1).label); assertEquals("ing", trie.edges.get(0).targetEdge(1).label); }