Ejemplo n.º 1
0
 private void preOrderTraverse(TrieNode node, ArrayList<String> list) {
   if (node.isEndOfKey()) {
     list.add(node.toString());
   }
   for (int i = 0; i < TrieNode.NUMCHILDREN; i++) {
     if (node.getChild((char) i) != null) {
       preOrderTraverse(node.getChild((char) i), list);
     }
   }
 }
Ejemplo n.º 2
0
  private TrieNode getPrefixNode(String word) {
    //   ADD YOUR CODE BELOW HERE

    TrieNode cur = root;
    int i = 0;
    while (i < word.length()) {
      if (cur.getChild(word.charAt(i)) == null)
        return cur; // only a strict prefix exists which is a path
      else cur = cur.getChild(word.charAt(i));
      i++;
    }
    return cur; //  word is already a rooted path in the trie i.e. a path starting at root

    //   return null  //  REPLACE THIS STUB
    //
    //   ADD YOUR CODE ABOVE HERE

  }