/** * Returns the number of successor for a node. If the node could not be found the return value is * 0. * * @param aWord a word. * @return the number of successor for a node. */ public Integer getSuccessors(String aWord) { KeyValueNode<String, Integer> node = findWord(aWord); if (node != null) { return node.getValue(); } return 0; }
/** * Adds a word to the tree. Also increments the successor value for each node * * @param aWord a word. */ public void addWord(String aWord) { KeyValueNode<String, Integer> parent = root; for (int i = 0; i < aWord.length(); i++) { String subword = aWord.substring(0, i + 1); KeyValueNode<String, Integer> child = parent.getChild(subword); if (child != null) { if (!subword.equals(aWord)) { child.setValue(child.getValue() + 1); } } else { Integer value = 1; if (subword.equals(aWord)) { value = 0; } child = new KeyValueNode<String, Integer>(subword, value); parent.addChild(child); } parent = child; } }