Пример #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
  }
Пример #2
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);
     }
   }
 }
Пример #3
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
  }
Пример #4
0
    public void insert(String word) {
      TrieNode node = root;
      char[] word_arr = word.toCharArray();
      for (char c : word_arr) {

        if (node.children[c - 'a'] == null) {
          node.children[c - 'a'] = new TrieNode();
        }
        node = node.children[c - 'a'];
      }
      node.item = word;
    }
Пример #5
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;
  }
Пример #6
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

  }
Пример #7
0
 public List<GraphNode> getListGraphNodeByName(String locationName) {
   TrieNode n = this.locations.searchNode(locationName);
   return n.getGraphNodes();
 }