/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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 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 the node in a string representation * * @return the node as string */ @Override public String toString() { return "" + getChar(); }
/** * returns the node in a string representation * * @return the node as string */ @Override public String toString() { return "" + getChar(); }