/** * creates a deep copy of itself * * @return a deep copy of itself */ @Override public Object clone() { TrieNode result; Enumeration<Character> keys; Character key; TrieNode child; result = new TrieNode(getChar()); keys = m_Children.keys(); while (keys.hasMoreElements()) { key = keys.nextElement(); child = (TrieNode) m_Children.get(key).clone(); result.add(child); result.m_Children.put(key, child); } return result; }
/** * checks whether a suffix can be found in its children * * @param suffix the suffix to look for * @return true if suffix was found */ public boolean contains(String suffix) { boolean result; Character c; String newSuffix; TrieNode child; c = suffix.charAt(0); newSuffix = suffix.substring(1); child = m_Children.get(c); if (child == null) { result = false; } else if (newSuffix.length() == 0) { result = true; } else { result = child.contains(newSuffix); } return result; }
/** * returns the common prefix for all the nodes starting with the node for * the specified prefix. Can be null if initial prefix is not found. The * result includes this node, unless it's the root node or a STOP node. * Using the empty string means starting with this node. * * @param startPrefix the prefix of the node to start the search from * @return the result of the search, null if startPrefix cannot be found */ public String getCommonPrefix(String startPrefix) { String result; TrieNode startNode; if (startPrefix.length() == 0) { startNode = this; } else { startNode = find(startPrefix); } if (startNode == null) { result = null; } else { result = startPrefix + startNode.determineCommonPrefix(""); } return result; }
for (i = 0; i < node.getLevel(); i++) { indentation.append(" | "); if (node.getChar() == null) { result.append("<root>"); } else if (node.getChar() == TrieNode.STOP) { result.append("STOP"); } else { result.append("'" + node.getChar() + "'"); for (i = 0; i < node.getChildCount(); i++) { result.append(toString((TrieNode) node.getChildAt(i)));
for (i = 0; i < node.getLevel(); i++) { indentation.append(" | "); if (node.getChar() == null) { result.append("<root>"); } else if (node.getChar() == TrieNode.STOP) { result.append("STOP"); } else { result.append("'" + node.getChar() + "'"); for (i = 0; i < node.getChildCount(); i++) { result.append(toString((TrieNode) node.getChildAt(i)));
/** * Removes a suffix from the trie. * * @param suffix the suffix to remove * @return true if this trie changed as a result of the call */ public boolean remove(String suffix) { boolean result; Character c; String newSuffix; TrieNode child; c = suffix.charAt(0); newSuffix = suffix.substring(1); child = m_Children.get(c); if (child == null) { result = false; } else if (newSuffix.length() == 0) { remove(c); result = true; } else { result = child.remove(newSuffix); if (child.getChildCount() == 0) { remove(child.getChar()); } } return result; }
/** * adds the given string to its children (creates children if necessary) * * @param suffix the suffix to add to its children * @return true if the add operation changed the structure */ public boolean add(String suffix) { boolean result; Character c; String newSuffix; TrieNode child; result = false; c = suffix.charAt(0); newSuffix = suffix.substring(1); // find child and add if necessary child = m_Children.get(c); if (child == null) { result = true; child = add(c); } // propagate remaining suffix if (newSuffix.length() > 0) { result = child.add(newSuffix) || result; } return result; }
/** * adds the given string to its children (creates children if necessary) * * @param suffix the suffix to add to its children * @return true if the add operation changed the structure */ public boolean add(String suffix) { boolean result; Character c; String newSuffix; TrieNode child; result = false; c = suffix.charAt(0); newSuffix = suffix.substring(1); // find child and add if necessary child = m_Children.get(c); if (child == null) { result = true; child = add(c); } // propagate remaining suffix if (newSuffix.length() > 0) { result = child.add(newSuffix) || result; } return result; }
/** * returns the node with the given suffix * * @param suffix the suffix to look for * @return null if unsuccessful otherwise the corresponding node */ public TrieNode find(String suffix) { TrieNode result; Character c; String newSuffix; TrieNode child; c = suffix.charAt(0); newSuffix = suffix.substring(1); child = m_Children.get(c); if (child == null) { result = null; } else if (newSuffix.length() == 0) { result = child; } else { result = child.find(newSuffix); } return result; }
/** * Removes a suffix from the trie. * * @param suffix the suffix to remove * @return true if this trie changed as a result of the call */ public boolean remove(String suffix) { boolean result; Character c; String newSuffix; TrieNode child; c = suffix.charAt(0); newSuffix = suffix.substring(1); child = m_Children.get(c); if (child == null) { result = false; } else if (newSuffix.length() == 0) { remove(c); result = true; } else { result = child.remove(newSuffix); if (child.getChildCount() == 0) { remove(child.getChar()); } } return result; }
/** * checks whether a suffix can be found in its children * * @param suffix the suffix to look for * @return true if suffix was found */ public boolean contains(String suffix) { boolean result; Character c; String newSuffix; TrieNode child; c = suffix.charAt(0); newSuffix = suffix.substring(1); child = m_Children.get(c); if (child == null) { result = false; } else if (newSuffix.length() == 0) { result = true; } else { result = child.contains(newSuffix); } return result; }
/** * returns the common prefix for all the nodes starting with the node for * the specified prefix. Can be null if initial prefix is not found. The * result includes this node, unless it's the root node or a STOP node. * Using the empty string means starting with this node. * * @param startPrefix the prefix of the node to start the search from * @return the result of the search, null if startPrefix cannot be found */ public String getCommonPrefix(String startPrefix) { String result; TrieNode startNode; if (startPrefix.length() == 0) { startNode = this; } else { startNode = find(startPrefix); } if (startNode == null) { result = null; } else { result = startPrefix + startNode.determineCommonPrefix(""); } return result; }
/** * returns the node with the given suffix * * @param suffix the suffix to look for * @return null if unsuccessful otherwise the corresponding node */ public TrieNode find(String suffix) { TrieNode result; Character c; String newSuffix; TrieNode child; c = suffix.charAt(0); newSuffix = suffix.substring(1); child = m_Children.get(c); if (child == null) { result = null; } else if (newSuffix.length() == 0) { result = child; } else { result = child.find(newSuffix); } return result; }
/** * returns the full string up to the root * * @return the full string to the root */ public String getString() { char[] result; TrieNode node; result = new char[this.getLevel()]; node = this; while (node.getParent() != null) { if (node.isRoot()) { break; } else { result[node.getLevel() - 1] = node.getChar(); } node = (TrieNode) node.getParent(); } return new String(result); }
/** * returns the full string up to the root * * @return the full string to the root */ public String getString() { char[] result; TrieNode node; result = new char[this.getLevel()]; node = this; while (node.getParent() != null) { if (node.isRoot()) { break; } else { result[node.getLevel() - 1] = node.getChar(); } node = (TrieNode) node.getParent(); } return new String(result); }
/** * determines the common prefix of the nodes. * * @param currentPrefix the common prefix found so far * @return the result of the search */ protected String determineCommonPrefix(String currentPrefix) { String result; String newPrefix; if (!isRoot() && (getChar() != STOP)) { newPrefix = currentPrefix + getChar(); } else { newPrefix = currentPrefix; } if (m_Children.size() == 1) { result = ((TrieNode) getChildAt(0)).determineCommonPrefix(newPrefix); } else { result = newPrefix; } return result; }
/** * returns all stored strings that match the given prefix * * @param prefix the prefix that all strings must have * @return all strings that match the prefix */ public Vector<String> getWithPrefix(String prefix) { Vector<String> result; TrieNode node; TrieIterator iter; result = new Vector<String>(); if (containsPrefix(prefix)) { node = m_Root.find(prefix); iter = new TrieIterator(node); while (iter.hasNext()) { result.add(iter.next()); } } return result; }
/** * returns all stored strings that match the given prefix * * @param prefix the prefix that all strings must have * @return all strings that match the prefix */ public Vector<String> getWithPrefix(String prefix) { Vector<String> result; TrieNode node; TrieIterator iter; result = new Vector<String>(); if (containsPrefix(prefix)) { node = m_Root.find(prefix); iter = new TrieIterator(node); while (iter.hasNext()) { result.add(iter.next()); } } return result; }