/** * 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; } }
/** * 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; }
/* If Trie has child corresponding to letter, then return the child. * Else create the child and return it. * */ public LetterNode getFirstLetter(String letter) { for (int x = 0; x < myChildren.size(); x++) { LetterNode current = myChildren.get(x); if (current.toString().equals(letter)) { return current; } } LetterNode newLetter = new LetterNode(letter, null); myChildren.add(newLetter); return newLetter; }
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); } } }
/** * 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; }
/** * If the prefix is in the lexicon, returns true. * * @param s The word to search for. * @return True if the lexicon contains s. */ public boolean containsPrefix(String s) { // Check for empty string if (s == "") { return false; } // Indicates if the Trie has a child corresponding to the first letter of s. // By default hasFirstLetter's value is false. boolean hasFirstLetter = false; for (int x = 0; x < myDictionary.myChildren.size(); x++) { LetterNode current = myDictionary.myChildren.get(x); if (current.toString().equals(s.substring(0, 1))) { hasFirstLetter = true; } } return hasFirstLetter && containsPrefixHelper(s, myDictionary.getFirstLetter(s.substring(0, 1))); }