Decoding Morse Code

Answer To: http://forum.codecall.net/c-c/23032-recursion-binarytree-morsecode.html

This is a very nasty example, and I blame it on the time (3:19 AM). Try handing this in your teacher will laugh at you. You'll probably want to extend the table as well, I've only done a-z.

Example usage:

tyler@MacMonster:~/stdc/morse$ gcc morse.c ; ./a.out .... . .-.. .-.. ---
HELLO

And of course the source…

#include <stdio.h>
#include <stdlib.h>
 
/* A very bare-bones btree */
typedef struct bnode {
    char data;
    struct bnode* right;
    struct bnode* left;
} bnode_t;
 
bnode_t* b_newnode(const char t_data){
    bnode_t* temp;
 
    temp = (bnode_t*)calloc(1,sizeof(bnode_t));
    if(temp == NULL){
        return NULL;
    }
 
    temp->data    = (char)t_data;
    temp->left    = NULL;
    temp->right   = NULL;
 
    return temp;
}
 
bnode_t* b_insert(bnode_t* t_root, const char t_data, const char* t_path){
    bnode_t* temp;
 
    if(t_root == NULL){
        t_root = b_newnode(t_data);
        return t_root;
    }
 
    temp = t_root;
 
    while(*t_path != '\0'){
        if(*t_path == '.' && temp->left != NULL){
            temp = temp->left;
        }else if(*t_path == '-' && temp->right != NULL){
            temp = temp->right;
        }else{
            break;
        }
        t_path++;
    }
 
    if(*t_path == '.'){
        temp->left = b_newnode(t_data);
        temp = temp->left;
    }
 
    if(*t_path == '-'){
        temp->right = b_newnode(t_data);
        temp = temp->right;
    }
 
    return temp;
}
 
const char b_lookup(bnode_t* t_root, const char* t_path){
    bnode_t* temp;
 
    if(t_root == NULL){
        return '\0';
    }
 
    temp = t_root;
 
    while(*t_path != '\0'){
        if(*t_path == '.' && temp->left != NULL){
            temp = temp->left;
        }else if(*t_path == '-' && temp->right != NULL){
            temp = temp->right;
        }else{
            return '\0';
        }
        t_path++;
    }
 
    return temp->data;
}
 
int main(const int argc, const char* argv[] ){
    bnode_t *root = b_newnode('\0');
    int i;
    const char* map[] = {"E",".","I","..","S","...",
                         "H","....","V","...-","U","..-",
                         "Ü","..--","F","..-.","A",".-",
                         "W",".--","R",".-.","J",".---",
                         "P",".--.","Ä",".-.-","L",".-..",
                         "T","-","N","-.","M","--",
                         "O","---","G","--.","K","-.-",
                         "D","-..","Ö","---.","Q","--.-",
                         "Z","--..","Y","-.--","C","-.-.",
                         "X","-..-","B","-..."};
 
    for(i = 0; i < sizeof(map)/sizeof(map[0]); i+=2){
        b_insert(root,map[i][0],map[i+1]);
    }
 
    for(i = 1; i < argc; i++){    
        printf("%c",b_lookup(root,argv[i]));
    }
 
    printf("\n");
 
    return 0;
}
 
morse.txt · Last modified: 2010/01/09 15:43 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:Public Domain
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki