Exemple #1
0
  public boolean contains(String key) {
    //   ADD YOUR CODE BELOW HERE

    TrieNode prefixNode = getPrefixNode(key);
    return (prefixNode != null && prefixNode.getDepth() == key.length() && prefixNode.isEndOfKey());

    //   ADD YOUR CODE ABOVE HERE
  }
Exemple #2
0
  public void insert(String key) {
    //  ADD YOUR CODE BELOW HERE

    TrieNode prefixNode = getPrefixNode(key);
    for (int curDepth = prefixNode.getDepth(); curDepth < key.length(); curDepth++) {
      prefixNode = prefixNode.createChild(key.charAt(curDepth));
    }
    prefixNode.setEndOfKey(true);

    //  ADD YOUR ABOVE HERE
  }
Exemple #3
0
  public ArrayList<String> getAllPrefixMatches(String prefix) {
    //  ADD YOUR CODE BELOW

    TrieNode node = getPrefixNode(prefix);
    ArrayList<String> stringList = new ArrayList<String>();
    if (node.getDepth() == prefix.length()) // make sure whole prefix is matched
    preOrderTraverse(node, stringList);

    //  ADD YOUR CODE ABOVE HERE

    return stringList;
  }