public void visit(TernaryNodeVisitor<C> visitor) { visitor.visit(this); if (this.lessThanNode != null) lessThanNode.visit(visitor); if (this.equalsNode != null) equalsNode.visit(visitor); if (this.greaterThanNode != null) greaterThanNode.visit(visitor); }
/** * Find all of the matches in the sub trie indicated and add them onto the list. * * @param wordList * @param limit * @param first true if we want to only include the equals first */ public void accumulate(List<C> wordList, int limit, boolean first) { for (List<C> col : this.valueListMap.values()) { wordList.addAll(col); } if (!first && lessThanNode != null) lessThanNode.accumulate(wordList, limit, false); if (equalsNode != null) equalsNode.accumulate(wordList, limit, false); if (!first && greaterThanNode != null) greaterThanNode.accumulate(wordList, limit, false); }
/** * @param levelZeroSet * <p>Visit all of the level zero nodes and add their character value into the set. */ public void getStartingCharacterSet(Set<String> levelZeroSet) { if (this.level > 0) return; if (this.level == 0) { levelZeroSet.add(this.character); if (lessThanNode != null) lessThanNode.getStartingCharacterSet(levelZeroSet); if (greaterThanNode != null) greaterThanNode.getStartingCharacterSet(levelZeroSet); } }
public void collect(List<C> valueList) { // private void collect(Node x, String prefix, Queue<String> queue) { // if (x == null) return; // collect(x.left, prefix, queue); // if (x.val != null) queue.enqueue(prefix + x.c); // collect(x.mid, prefix + x.c, queue); // collect(x.right, prefix, queue); for (List<C> col : this.valueListMap.values()) { valueList.addAll(col); } if (lessThanNode != null) lessThanNode.collect(valueList); if (equalsNode != null) equalsNode.collect(valueList); if (greaterThanNode != null) greaterThanNode.collect(valueList); }
/** * @param prefix * @return the node theat matches the prefix given. */ public TernaryNode<C> matchPrefix(String prefix) { // private Node get(Node x, String key, int d) { // if (key == null || key.length() == 0) throw new RuntimeException("illegal key"); // if (x == null) return null; // char c = key.charAt(d); // if (c < x.c) return get(x.left, key, d); // else if (c > x.c) return get(x.right, key, d); // else if (d < key.length() - 1) return get(x.mid, key, d+1); // else return x; if (prefix.length() == 0) return this; String testCharacter = prefix.substring(this.level, this.level + 1); String thisCharacter = this.character; int comparison = testCharacter.compareTo(thisCharacter); if (comparison < 0) { if (lessThanNode != null) return lessThanNode.matchPrefix(prefix); else return null; } else if (comparison > 0) { if (greaterThanNode != null) return greaterThanNode.matchPrefix(prefix); else return null; } else if (level < prefix.length() - 1) { // equals if (equalsNode != null) return equalsNode.matchPrefix(prefix); else return null; } else { return this; } }
/** * Write the word into the index adding nodes when required. * * @param word * @param value */ public void index(String word, C value) { if (isCompressedLeafNode()) { // is the word contained within our string. // if (this.character.startsWith(word)) { // // int index = word.length()-1; // // List<C>matchList = this.valueListMap.get(index); // // if (matchList == null) { // matchList = new LinkedList<C>(); // this.valueListMap.put(index, matchList); // } // // matchList.add(value); // } // else { } String testCharacter = word.substring(this.level, this.level + 1); int comparison = testCharacter.compareTo(this.character); if (comparison < 0) { if (lessThanNode == null) lessThanNode = new TernaryNode<C>(this, this.level, testCharacter); lessThanNode.index(word, value); } else if (comparison > 0) { if (greaterThanNode == null) greaterThanNode = new TernaryNode<C>(this, this.level, testCharacter); greaterThanNode.index(word, value); } else if (level < word.length() - 1) { // equals if (equalsNode == null) { // if null we write the rest of the string into this node equalsNode = new TernaryNode<C>(this, this.level + 1, word.substring(this.level + 1)); } else { equalsNode.index(word, value); } } else { // terminates here List<C> valueList = this.valueListMap.get(0); if (valueList == null) { valueList = new LinkedList<C>(); this.valueListMap.put(0, valueList); } valueList.add(value); } }