public class MorseTree {
TreeNode<Character> root = null;
public MorseTree() {
Scanner fin;
try {
fin = new Scanner(new File("data.txt"));
while(fin.hasNext()){
char letter = fin.next().charAt(0);
String morse = fin.nextLine().trim();
//System.out.println(morse + " = " + letter); //prints out txt file
add(morse, letter);
}
} catch (Exception e) { //FileNotFound
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add(String morseStr, char letter) {
root = insertInSubtree(morseStr, letter, root);
}
private TreeNode<Character> insertInSubtree(String morseStr, char letter, TreeNode<Character> subTree) {
//base case 1 : subtree is null
if(subTree == null){
subTree = new TreeNode<Character>('~', null, null); //~ means not defined
}
if(morseStr.length() == 0){
subTree.data = letter;
}else if(morseStr.charAt(0) == '.'){ //go right
subTree.right = insertInSubtree(morseStr.substring(1), letter, subTree.right);
}else{ //morseString.charAt(0) == '-') // go left
subTree.left = insertInSubtree(morseStr.substring(1), letter, subTree.left);
}
//base case 2 : morseStr is of length 0
//recursive case 1: the first char in morseStr is a '.', so recursively traverse tree
//recursive case 2: the first char in the morseStr is a '-', so recurse accordingly
return subTree;
}
public Character translate(String morseStr) {
return findInSubtree(morseStr, root);
}
private Character findInSubtree(String morseStr, TreeNode<Character> subTree) { //simular to insertInSubtree
if(subTree == null){
return '~'; //~ means not defined
}else if(morseStr.length() == 0){
return subTree.data;
}else if(morseStr.charAt(0) == '.'){ //go right
return findInSubtree(morseStr.substring(1), subTree.right);
}else{ //morseString.charAt(0) == '-') // go left
return findInSubtree(morseStr.substring(1), subTree.left);
}
}
public String translateString(String tokens) {
String retVal = "";
//build a scanner here using tokens as input
Scanner text = new Scanner(tokens);
while(text.hasNext()){
String morseStr = text.next();
retVal += translate(morseStr);
}
return retVal;
}
public String toMorseCode(Character c) {
//walk the tree looking for the TreeNode with the char c in it
String retVal = "";
if(subTree != null){
retVal += inorderWalk(subTree.left);
retVal += subTree.data.toString()+",";
retVal += inorderWalk(subTree.right);
}
return retVal;
//preorder walk?
//inorder walk? *********
//postorder walk?
//when you've found the char c, report the path from the root to the node
//and build the morse code by adding a "." when you go right, "-" when you go left
}
public String toString() {
return inorderWalk(root);
}
private String inorderWalk(TreeNode<Character> subTree) {
String retVal = "";
if(subTree != null){
retVal += inorderWalk(subTree.left);
retVal += subTree.data.toString()+",";
retVal += inorderWalk(subTree.right);
}
return retVal;
}
public static void main(String[] args) {
MorseTree mt = new MorseTree(); //builds our tree using data from a file
System.out.println(mt.translate("....")); //h
System.out.println(mt.translate(".")); //e
System.out.println(mt.translate(".-..")); //l
System.out.println(mt.translate(".-..")); //l
System.out.println(mt.translate("---")); //0
System.out.println(mt.translateString("... --- ...")); //SOS
System.out.println(mt.translateString(".... --- .-.. .-")); //Hola
System.out.println(mt.toMorseCode('S')); //find where we are in the tree, remember path to root
}
private class TreeNode<TBA extends Comparable>{
TBA data;
TreeNode<TBA> left;
TreeNode<TBA> right;
public TreeNode(TBA d, TreeNode<TBA> l, TreeNode<TBA> r){
data = d;
left = l;
right = r;
}
}
}