Java:基于摩尔斯电码创建树

时间:2015-12-04 21:51:12

标签: java object if-statement tree nodes

给出每个字母的字母和莫尔斯码值......

a._
b_...
c_._.
d_..
e.
f.._.

我尝试创建一棵树,通过扫描代码找到树中一个字母的位置,然后向左分支以获得一个点并向右分支以获得一个破折号。

我创建了一个包含典型numberNode leftnumberNode right变量以及morseCodeletter的节点类。这是我的功能。

aList是从文件读入的已创建节点的arraylist。 rootNode是树的根,没有设置lettermorsecode

    public static void createTree(ArrayList<numberNode> aList, numberNode rootNode)
{
    for (numberNode n : aList)  //for each numberNode in aList
    {
        int lengthOfCode = n.getmorseCode().length()-1;  //get the length of the morsecode
        numberNode currentNode = rootNode; //sets currentNode to the rootNode at first
        for (int i=0; i<lengthOfCode; i++) 
        {
            char c = n.getmorseCode().charAt(i); //for each char in morsecode until it gets to the end
            if (c == '.')
            {
                if (currentNode.getleft() = null) //if currentnode left is null
                {
                    numberNode newLeftNode = new numberNode(); //create new node
                    currentNode.setleft(newLeftNode); //set current node left to the new node
                    if (i == lengthOfCode)
                    {
                        currentNode.setleft(n); //if end of morse code, set the current node left's to n
                    }
                    else
                    {
                        currentNode = newLeftNode; //else change current node to the newly created leftnode
                    }

                }
                else //if current node left is not null
                {
                    if (i == lengthOfCode)
                    {
                        currentNode.setleft(n); //if at end of morse code
                    }
                    currentNode = currentNode.getleft(); //if not at end set current node to current node's left

                }

            }
            if (c == '_')
            {
                if (currentNode.right() =null)
                {
                    numberNode newRightNode = new numberNode();
                    currentNode.setleft(newRightNode);
                    if (i == lengthOfCode)
                    {
                        currentNode.setright(n);
                    }
                    else
                    {
                        currentNode = newRightNode;
                    }

                }
                else
                {
                    if (i == lengthOfCode)
                    {
                        currentNode.setright(n);
                    }
                    currentNode = currentNode.getright();

                }

            }
        }
    }
}

我有几个问题......

我的算法至少是正确的吗?

有没有其他方法可以做到这一点,而不是那么难看?

如果您需要查看我的更多代码,请不要犹豫。感谢您的时间,我真的很感激!

编辑:目前正在运行,但获取当前输出

    a
null
null

b
null
null

c
null
null

d
null
null

关于出了什么问题的任何想法?

编辑#2:添加了我的课程

    class numberNode
{
    String morseCode;
    String letter;
    numberNode left;
    numberNode right;
    int occupied;


        public numberNode()
        {
            this.occupied=0;
            left = null;
            right = null;
        }
        public void setmorseCode(String aCode)
        {
            morseCode = aCode;
        }

        public String getmorseCode()
        {
            return morseCode;
        }

        public void setletter(String aLetter)
        {
            letter = aLetter;
        }

        public String getletter()
        {
            return letter;
        }

        public void setleft(numberNode aNode)
        {
            left = aNode;
        }
        public numberNode getleft()
        {
            return left;
        }

        public void setright(numberNode aNode)
        {
            right = aNode;
        }
        public numberNode getright()
        {
            return right;
        }
    }

1 个答案:

答案 0 :(得分:1)

如果currentNode.getleft()返回null,那么尝试在其上调用equals将为您提供NPE。使用==:

测试null
if (currentNode.getleft() == null) ....