Esempio n. 1
0
  /**
   * Helper function for contains that takes in a String s and a LetterNode letter and verifies that
   * the first letter of s matches the LetterNode letter. If this does not hold; returns false.
   *
   * @param s is the prefix being checked if it is in the dictionary
   * @param letter is compared to the first letter of s to see if s is in the dictionary
   */
  public boolean containsHelper(String s, LetterNode w) {
    // If w's letter is equal to the first letter of s
    if (w.toString().equals(s.substring(0, 1))) {

      // If s has one letter and the corresponding TreeNode
      // completesFullWord, then the word is in the Trie.
      if (s.length() == 1) {
        if (w.completesFullWord) {
          return true;
        } else {
          return false;
        }
      } else {
        LetterNode nextPossible = w.getNextLetter(s.substring(1, 2));

        // If child LetterNode corresponding to the next letter does not exist
        if (nextPossible == null) {
          return false;
        } else {
          return containsHelper(s.substring(1), nextPossible);
        }
      }
    }
    return false;
  }
Esempio n. 2
0
  /**
   * Helper function for containsPrefix that takes in a String s and a LetterNode letter and
   * verifies that the first letter of s matches the LetterNode letter. If this does not hold;
   * returns false.
   *
   * @param s is the prefix being checked if it is in the dictionary
   * @param letter is compared to the first letter of s to see if s is in the dictionary
   */
  private boolean containsPrefixHelper(String s, LetterNode letter) {
    // If w's letter is equal to the first letter of s
    if (letter.toString().equals(s.substring(0, 1))) {

      // If s has one letter, then the prefix is in the Trie.
      if (s.length() == 1) {

        return true;
      } else {
        LetterNode nextPossible = letter.getNextLetter(s.substring(1, 2));

        // If child LetterNode corresponding to the next letter does not exist
        if (nextPossible == null) {

          return false;
        } else {
          return containsPrefixHelper(s.substring(1), nextPossible);
        }
      }
    }
    return false;
  }