0

Given the alphabet and a morse code value for each letter...

a._
b_...
c_._.
d_..  //file to be read in
e.
f.._.

I'm attempting to create a tree, finding the position for a letter in the tree by scanning the code, and branching left for a dot and branching right for a dash.

I've created a node class with the typical numberNode left and numberNode right variables along with morseCode and letter. Here's my function.

aList is an arraylist of created nodes read in from a file. rootNode is the root of the tree, having no set letter or morsecode.

    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();

                }

            }
        }
    }
}

Couple questions I have...

Is my algorithm at least correct?

Is there an alternate way of doing this without it being so ugly?

If you need to see any more of my code please don't hesitate to ask. Thanks for your time, I truly appreciate it!

EDIT: Currently runs, but when I try to print out the output using...

       for (numberNode n : nodeArray)
    {
        System.out.println(n.getletter());
        System.out.println(n.getleft().getletter().toString()); //error here
        System.out.println(n.getright().getletter());
        System.out.println("");
    }

I get an error where indicated,Any ideas on what's going wrong?

2
  • could you be more precise about errors ? can you put some println inside your code to trace it, and verify your algorithm ? Commented Dec 4, 2015 at 23:50
  • I am getting a NPE, I have atleast in my mind, verified my algorithm. I've been running through it for the last couple hours and can't seem to find anything wrong with it. Was hoping another pair of eyes on it would help me out Commented Dec 4, 2015 at 23:58

1 Answer 1

1

First, when you try to print the values out, what did the compiler say was the error on System.out.println(n.getleft().getletter().toString()); //error here?

Is each letter suppose to be the root of it's own tree? If it is suppose to be on tree, I would have a reference pointer at the start that would contain references to every letter in the alphabet, while the nodes branching off each letter would have the particular sequence that represents the Morse code for that letter. But if I was free to do whatever, I would simply create an array of 26 characters that contain a MorseCode object that has two fields, one containing the Letter and the other containing the Morse Code that is associated with that letter.

It would be helpful to understand what the output is suppose to be for this program and clarify what is in the file you are reading into the arrayList. What does that data look like?

Sign up to request clarification or add additional context in comments.

4 Comments

It was a null pointer exception error there. Each letter is supposed to be on one tree, based on the . and _ in each letter's given morse code. The output doesn't have to be anything at the moment, just build the tree.
I'm just trying to print out the data so I can see the left's and right's of each node to see if the tree was built correctly
If it crashed and returned a Null Pointer, it means that there is no left node to retrieve when you ran the code. It makes me think that maybe your code is not creating the left node, or if the first letter was A, there isn't going to be a left node. Typically a tree should have leafs, whether they contain data or not. So for example, if you make a binary search tree, you start with the root and create the left and right leafs right away and set them to null. If the first letter is in fact A, try n.getRight.getLetter().toString() and see if it crashes.
The empty nodes are so you don't have to deal with null pointers. You check the node's value and if it is set to null, your iterator will not continue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.