摩尔斯电码和二叉树

时间:2017-04-17 18:01:48

标签: java binary-tree morse-code

我想实现generateTree()方法,但出了点问题。 这个想法是,一旦从字符串数组生成,我应该能够,感谢解码方法,从莫尔斯代码转换为文本。在此先感谢您的帮助。只剩下了printTree()方法。

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<Integer> codesize=new  ArrayList<Integer>();
    private static BinaryTreeNode root;

    private static String code[] = {
            ".-", "-...", "-.-.", "-..", ".", "..-.",
            "--.", "....", "..", ".---", "-.-", ".-..",
            "--", "-.", "---", ".--.", "--.-", ".-.",
            "...", "-", "..-", "...-", ".--", "-..-",
            "-.--", "--.."};

    public static void main(String[] args) throws IOException
    {
        if (args.length < 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 < 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 < 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 < 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
    }
}

0 个答案:

没有答案