import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class PhonewordLister { private PrefixTree tree = new PrefixTree(); private FileReader fr = null; private HeapPQ words; // this constructor should set "tree" to the prefix tree created from Knuth's word list public PhonewordLister ( ) throws IOException { this("sgb-words.txt"); } // this constructor sets "tree" to the prefix tree created from any given file public PhonewordLister(String file) throws IOException{ if (fr == null){ try {fr = new FileReader(file); } catch (IOException e) {System.out.println(e.getMessage());} } BufferedReader br = new BufferedReader(fr); String next = br.readLine(); int loop = 0; while (next != null){ tree.addString(next); next = br.readLine(); loop++; } } public PrefixTree getTree ( ) { return tree; } public static int scrabbleScore (String w) { // returns Scrabble score if w is made only of letters, returns 0 otherwise int [ ] value = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10}; int sum = 0; for (int i = 0; i < w.length( ); i++) if ('a' <= w.charAt(i) && w.charAt(i) <= 'z') sum += value[w.charAt(i) - 'a']; else if ('A' <= w.charAt(i) && w.charAt(i) <= 'Z') sum += value[w.charAt(i) - 'A']; else return 0; return sum;} public String [ ] list (String digits) { // This is the key method to be written by the student. // The input "digits" is to be a string of five characters, each a digit in the range 2-9 or a *. // If it is of this form, return an array of all phonewords that can be made from the input, // in order of Scrabble score with highest score first, treating a * as a wild-card. (Handling *'s is extra credit.) // If the input is invalid, return an array of length 0. PTNode current = tree.getRoot(); words = new HeapPQ(); listRecurse(current, digits); //create a new array of the right size for output String[] output = new String[words.getSize()]; //add words in the right order by removing from the heap priority queue for (int j = words.getSize() - 1; j >= 0; j--){ PTNode node = words.removeNext(); String next = ""; if (node != null) next = node.getElem(); output[j] = next;} return output; } private void listRecurse(PTNode current, String digits){ //if we've reached a leaf, add it to our list at the proper place if (digits.length()==0){ words.addElement(current, scrabbleScore(current.getElem())); } //if there is only one letter left, don't recurse anymore, just get the right leaf else if (digits.length()==1){ if (digits.charAt(0) == '2'){ if (current.getChild('a') != null) listRecurse(current.getChild('a'), ""); if (current.getChild('b') != null) listRecurse(current.getChild('b'), ""); if (current.getChild('c') != null) listRecurse(current.getChild('c'), "");} else if (digits.charAt(0) == '3'){ if (current.getChild('d') != null) listRecurse(current.getChild('d'), ""); if (current.getChild('e') != null) listRecurse(current.getChild('e'), ""); if (current.getChild('f') != null) listRecurse(current.getChild('f'), "");} else if (digits.charAt(0) == '4'){ if (current.getChild('g') != null) listRecurse(current.getChild('g'), ""); if (current.getChild('h') != null) listRecurse(current.getChild('h'), ""); if (current.getChild('i') != null) listRecurse(current.getChild('i'), "");} else if (digits.charAt(0) == '5'){ if (current.getChild('j') != null) listRecurse(current.getChild('j'), ""); if (current.getChild('k') != null) listRecurse(current.getChild('k'), ""); if (current.getChild('l') != null) listRecurse(current.getChild('l'), "");} else if (digits.charAt(0) == '6'){ if (current.getChild('m') != null) listRecurse(current.getChild('m'), ""); if (current.getChild('n') != null) listRecurse(current.getChild('n'), ""); if (current.getChild('o') != null) listRecurse(current.getChild('o'), "");} else if (digits.charAt(0) == '7'){ if (current.getChild('p') != null) listRecurse(current.getChild('p'), ""); if (current.getChild('q') != null) listRecurse(current.getChild('q'), ""); if (current.getChild('r') != null) listRecurse(current.getChild('r'), ""); if (current.getChild('s') != null) listRecurse(current.getChild('s'), "");} else if (digits.charAt(0) == '8'){ if (current.getChild('t') != null) listRecurse(current.getChild('t'), ""); if (current.getChild('u') != null) listRecurse(current.getChild('u'), ""); if (current.getChild('v') != null) listRecurse(current.getChild('v'), "");} else if (digits.charAt(0) == '9'){ if (current.getChild('w') != null) listRecurse(current.getChild('w'), ""); if (current.getChild('x') != null) listRecurse(current.getChild('x'), ""); if (current.getChild('y') != null) listRecurse(current.getChild('y'), ""); if (current.getChild('z') != null) listRecurse(current.getChild('z'), "");} else if (digits.charAt(0) == '*'){ for (char charIndex = 'a'; charIndex <= 'z'; charIndex++) if (current.getChild(charIndex) != null) listRecurse(current.getChild(charIndex), "");}} //else recurse down else { if (digits.charAt(0) == '2'){ if (current.getChild('a') != null) listRecurse(current.getChild('a'), digits.substring(1)); if (current.getChild('b') != null) listRecurse(current.getChild('b'), digits.substring(1)); if (current.getChild('c') != null) listRecurse(current.getChild('c'), digits.substring(1));} else if (digits.charAt(0) == '3'){ if (current.getChild('d') != null) listRecurse(current.getChild('d'), digits.substring(1)); if (current.getChild('e') != null) listRecurse(current.getChild('e'), digits.substring(1)); if (current.getChild('f') != null) listRecurse(current.getChild('f'), digits.substring(1));} else if (digits.charAt(0) == '4'){ if (current.getChild('g') != null) listRecurse(current.getChild('g'), digits.substring(1)); if (current.getChild('h') != null) listRecurse(current.getChild('h'), digits.substring(1)); if (current.getChild('i') != null) listRecurse(current.getChild('i'), digits.substring(1));} else if (digits.charAt(0) == '5'){ if (current.getChild('j') != null) listRecurse(current.getChild('j'), digits.substring(1)); if (current.getChild('k') != null) listRecurse(current.getChild('k'), digits.substring(1)); if (current.getChild('l') != null) listRecurse(current.getChild('l'), digits.substring(1));} else if (digits.charAt(0) == '6'){ if (current.getChild('m') != null) listRecurse(current.getChild('m'), digits.substring(1)); if (current.getChild('n') != null) listRecurse(current.getChild('n'), digits.substring(1)); if (current.getChild('o') != null) listRecurse(current.getChild('o'), digits.substring(1));} else if (digits.charAt(0) == '7'){ if (current.getChild('p') != null) listRecurse(current.getChild('p'), digits.substring(1)); if (current.getChild('q') != null) listRecurse(current.getChild('q'), digits.substring(1)); if (current.getChild('r') != null) listRecurse(current.getChild('r'), digits.substring(1)); if (current.getChild('s') != null) listRecurse(current.getChild('s'), digits.substring(1));} else if (digits.charAt(0) == '8'){ if (current.getChild('t') != null) listRecurse(current.getChild('t'), digits.substring(1)); if (current.getChild('u') != null) listRecurse(current.getChild('u'), digits.substring(1)); if (current.getChild('v') != null) listRecurse(current.getChild('v'), digits.substring(1));} else if (digits.charAt(0) == '9'){ if (current.getChild('w') != null) listRecurse(current.getChild('w'), digits.substring(1)); if (current.getChild('x') != null) listRecurse(current.getChild('x'), digits.substring(1)); if (current.getChild('y') != null) listRecurse(current.getChild('y'), digits.substring(1)); if (current.getChild('z') != null) listRecurse(current.getChild('z'), digits.substring(1));} else if (digits.charAt(0) == '*'){ for (char charIndex = 'a'; charIndex <= 'z'; charIndex++) if (current.getChild(charIndex) != null) listRecurse(current.getChild(charIndex), digits.substring(1));}} } }