Esempio n. 1
0
  /**
   * Helper function for load that takes in a string strItem and a LetterNode w and constructs a
   * trie path for strItem starting from w.
   *
   * @param strItem is the word to be constructed in the Trie
   * @param letter is the letter from which strItem is to be constructed
   */
  private void loadHelper(String strItem, LetterNode letter) {

    if (strItem.equals("")) {
      return;
    } else {

      for (int x = 0; x < letter.myChildren.size(); x++) {

        // If w already has a child LetterNode equal to the first letter of strItem
        if (letter.myChildren.get(x).toString().equals(strItem.substring(0, 1))) {
          LetterNode nextLetter = letter.myChildren.get(x);

          // Mark as a full word if one letter remains in the word
          if (strItem.length() == 1) {
            nextLetter.completesFullWord = true;
          }

          loadHelper(strItem.substring(1), nextLetter);

          return;
        }
      }

      // If w does not have a child LetterNode corresponding to the next letter of the word
      LetterNode newLetter = new LetterNode(strItem.substring(0, 1), letter);
      letter.myChildren.add(newLetter);

      // Mark as a full word if one letter remains in the word
      if (strItem.length() == 1) {
        newLetter.completesFullWord = true;
      }
      loadHelper(strItem.substring(1), newLetter);
      return;
    }
  }
Esempio n. 2
0
  public void load(Scanner input) {
    while (input.hasNext()) {
      String nextStr = input.next();

      // Filter out empty strings
      if (nextStr != "") {

        /* Get child LetterNode in Trie corresponding to the first letter of nextStr; if such child does
           not exist, then create it
        */
        LetterNode firstLetter = myDictionary.getFirstLetter(nextStr.substring(0, 1));

        // If nextStr is a one-lettered word, mark it as a complete word
        if (nextStr.length() == 1) {
          firstLetter.completesFullWord = true;
        }

        // Create rest of word with firstLetter as the starting point of the path
        loadHelper(nextStr.substring(1), firstLetter);
      }
    }
  }