title: Morse Code and Binary Tree
body: <p>I want to implement the generateTree() method but something got wrong.
  The idea is that, once generated from the Array of strings, I should be able, thanks the decode method, to translate from morse code to text. Thanks in advance for any help. Only left is also the printTree() method. </p>
  
  <pre><code>import java.io.*;
  import java.util.ArrayList;
  
  class BinaryTreeNode
  {
      private Object value;
      private BinaryTreeNode left;
      private BinaryTreeNode right;
  
      public BinaryTreeNode(Object o)
      {
          value = o;
          left = null;
          right = null;
      }
  
      public Object getValue() { return value; }
      public BinaryTreeNode getLeft() { return left; }
      public BinaryTreeNode getRight() { return right; }
  
      public void setValue(Object v) { value = v; }
      public void setLeft(BinaryTreeNode p) { left = p; }
      public void setRight(BinaryTreeNode p) { right = p; }}
  
  
  
  public class Morse{
      private static ArrayList&lt;Integer&gt; codesize=new  ArrayList&lt;Integer&gt;();
      private static BinaryTreeNode root;
  
      private static String code[] = {
              ".-", "-...", "-.-.", "-..", ".", "..-.",
              "--.", "....", "..", ".---", "-.-", ".-..",
              "--", "-.", "---", ".--.", "--.-", ".-.",
              "...", "-", "..-", "...-", ".--", "-..-",
              "-.--", "--.."};
  
      public static void main(String[] args) throws IOException
      {
          if (args.length &lt; 1)
          {
              System.out.println("Not enough arguments!");
              return;
          }
  
          // convert from morse code strings to letters.
          if (args[0].equals("-d"))
          {
              generateTree();
              printTree();
              BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
              String inputLine;
              while((inputLine=console.readLine()) != null) {
                  String[] codes = inputLine.split(" ");
                  for(int i = 0; i &lt; codes.length; i++) {
                      System.out.print(decode(codes[i]));
                  }
                  System.out.println();
                  break;
              }
              return;
          }
      }
  
      // generates the binary tree used for the decoding of morse code strings
      public static void generateTree(){
          //TODO
          BinaryTreeNode current = root;
          String signal = " ";
  
          for (int i = 0; i &lt; code[i].length(); i++) {
              signal = code[i];
                  if (signal.equals(".")) {
                      if (current.getLeft() != null) {
                          current = current.getLeft();
                      } else {
                          current.setLeft(new BinaryTreeNode(root));
                          current = current.getLeft();
                      }
                  } else {
                      if (current.getRight() != null) {
                          current = current.getRight();
                      } else {
                          current.setRight(new BinaryTreeNode(root));
                          current = current.getRight();
                      }
                  }
          }
          current.getValue();
      }
  
      //decoding of morse code strings
      public static String decode(String m){
  
          String signal = "";
          StringBuffer result = new StringBuffer("");
          BinaryTreeNode current = root;
  
          for (int i = 0; i &lt; m.length(); i++) {
              signal = m;
                  if (signal.equals(".")) {
                      if (current.getLeft() != null) {
                          current = current.getLeft();
                      } else {
                          current.setLeft(new BinaryTreeNode(root));
                          current = current.getLeft();
                      }
                  } else if (signal.equals("-")) {
                      if (current.getRight() != null) {
                          current = current.getRight();
                      } else {
                          current.setRight(new BinaryTreeNode(root));
                          current = current.getRight();
                      }
                  } else {
                      result = result.append(current.getValue());
                      current = root;
                  }
          }
          result = result.append(current.getValue());
          return result.toString();
          //return null;
      }
  
      //print the binary tree
      public static void printTree(){
          //TODO
      }
  }
  </code></pre>