import java.util.TreeMap;public class MorseCode{ private static final char DOT = '.'; private static final char DASH = '-'; private static TreeMap codeMap; private static TreeNode decodeTree; public static void start() { codeMap = new TreeMap(); decodeTree = new TreeNode(new Character(' '), null, null); // put a space in the root of the decoding tree addSymbol('A', ".-"); addSymbol('B', "-..."); addSymbol('C', "-.-."); addSymbol('D', "-.."); addSymbol('E', "."); addSymbol('F', "..-."); addSymbol('G', "--."); addSymbol('H', "...."); addSymbol('I', ".."); addSymbol('J', ".---"); addSymbol('K', "-.-"); addSymbol('L', ".-.."); addSymbol('M', "--"); addSymbol('N', "-."); addSymbol('O', "---"); addSymbol('P', ".--."); addSymbol('Q', "--.-"); addSymbol('R', ".-."); addSymbol('S', "..."); addSymbol('T', "-"); addSymbol('U', "..-"); addSymbol('V', "...-"); addSymbol('W', ".--"); addSymbol('X', "-..-"); addSymbol('Y', "-.--"); addSymbol('Z', "--.."); addSymbol('0', "-----"); addSymbol('1', ".----"); addSymbol('2', "..---"); addSymbol('3', "...--"); addSymbol('4', "....-"); addSymbol('5', "....."); addSymbol('6', "-...."); addSymbol('7', "--..."); addSymbol('8', "---.."); addSymbol('9', "----."); addSymbol('.', ".-.-.-"); addSymbol(',', "--..--"); addSymbol('?', "..--.."); System.out.println(countNodes(decodeTree)+ " Nodes!"); } /** * Inserts a letter and its Morse code string into the encoding map * and calls treeInsert to insert them into the decoding tree. */ private static void addSymbol(char letter, String code) { codeMap.put(letter, code); treeInsert(letter, code); } /** * Inserts a letter and its Morse code string into the * decoding tree. Each dot-dash string corresponds to a path * in the tree from the root to a node: at a "dot" go left, at a "dash" go * right. The node at the end of the path holds the symbol * for that code string. */ private static void treeInsert(char letter, String code) { TreeNode node = decodeTree; int i, len = code.length(); char ch; for (i = 0; i < len; i++) { ch = code.charAt(i); if (ch == DOT) { if (node.getLeft() == null) node.setLeft(new TreeNode(new Character(' '), null, null)); node = node.getLeft(); } else // if (ch == DASH) { if (node.getRight() == null) node.setRight(new TreeNode(new Character(' '), null, null)); node = node.getRight(); } } node.setValue(new Character(letter)); } /** * Converts text into a Morse code message. Adds a space after a dot-dash * sequence for each letter. Other spaces in the text are transferred directly * into the encoded message. * Returns the encoded message. */ public static String encode(String text) { StringBuffer morse = new StringBuffer(400); int len = text.length(); int i = 0; char ch; for ( i = 0; i < len; i++ ) { ch = text.toUpperCase().charAt(i); morse.append( (String)codeMap.get(new Character(ch)) + " "); } return morse.toString(); } /** * Converts a Morse code message into a text string. Assumes that dot-dash * sequences for each letter are separated by one space. Additional spaces are * transferred directly into text. * Returns the plain text message. */ public static String decode(String morse) { StringBuffer text = new StringBuffer(100); TreeNode node = decodeTree; int i, len = morse.length(); char ch; for (i = 0; i < len; i++) { ch = morse.charAt(i); if (ch == '.') { node = node.getLeft(); } else if (ch == '-') // if (ch == DASH) { node = node.getRight(); } else if (ch == ' ') { text.append(node.getValue().toString() + " "); } } return text.toString(); } public static int countNodes(TreeNode root) { if (root == null) { return 0; } else { System.out.println(root.getValue()); return 1 + countNodes(root.getLeft()) + countNodes(root.getRight()); } } }