Converting from Morse code is more difficult; however, there is in fact a straight-forward conversion.
Suppose we are hearing a sequence of dots and dashes. When listening to Morse code, you learn sequences of dots and dashes as representing specific characters. This requires a significant amount of practice. Suppose, however, you are listening or reading dots and dashes separated by spaces. Suppose you have a space, indicating that the next dot or dash is the start of a new character. Now, consider the following tree:
Suppose the first signal is a dot. This removes all possible characters in the right-hand sub-tree of the root. If the only signal is that dot, the letter was an 'e'. If, however, there is another signal, if it is a dot, we move down the left tree from 'e' and if it is a dash, we move down the right tree. We continue listening for either dots or dashes and continue down the tree. Now, one of three outcomes may occur:
For example, if you see .--. .- - . ..-., you start at the top of tree and
If this was the end of the transmission, this would code patel.
This is definitely not how a human would learn Morse code, and it would be idiotic to try to do so. However, this would be perfect for a computer. The question is, how do we store and traverse such a tree?
Consider the following array:
You will notice that we are walking across the array following manner:
and so on and so forth as shown in this image:
Now, we will ignore array entry 0. Note that, for example, 'a' appears in the array entry 5. In the tree, its left child is 'r' and its right child is 'w'. These two are in array entries 10 and 11. Similarly, 'k' is in the array entry 13. In the tree, its left child is 'c' and its right child is 'y'. These two are in array entries 26 and 27.
Consequently, the children of the array entry n are in array entries 2*n and 2*n + 1.
We can now define the following process for decoding Morse code:
char *decode_array[33]{"\0\0etianmsurwdkgohvf\0l\0pjbxcyzq\0\0"};
Albert Ndur-Osei suggested this project.