简体   繁体   中英

Segmentation Fault in Morse Code Binary Search Tree

Essentially in this program a binary search tree is created, full of TREENODE structs consisting of a letter amd 2 TREENODE pointers (left and right) to connect to other nodes and simulate the tree.

User input gets saved to character array text . Encode function iterates through array and couts the morse code translation, which it saves to character array morse . Works like a charm.

Problem : Receive segmentation fault in Decode function. GDB: ![BST分段错误

This indicates that no binary search tree had really been created.

How can I fix this code such that the decode function works? (I understand that I can just cout text but I'd like to create a legitimate function that decodes an array of characters.

Header file:

struct TREENODE {
 char letter;
 TREENODE *left;
 TREENODE *right;

 TREENODE(){ // Constructor
    letter = '*'; //To be replaced by a letter later.
    left = 0;
    right = 0;
 }
};  

struct MORSECODE{ //each morsecode object has 
    char letter; //English letters.
    char code[20]; //Dots + dashes
};   

class TELEGRAPH{ //The binary (tree)
    private:
        static MORSECODE table[40];
        static TREENODE * root;
        static void destroyTree(TREENODE *node);
    public:
        TELEGRAPH(){
           root = NULL;
        }
        static void buildTree();
        static void destroyTree();
        void Encode(char text[], char morse[]);
        void Decode(char morse[], char text[]);
};    

MORSECODE TELEGRAPH::table[40] = {
 {'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."},
 {'E', "."}, {'F', "..-."}, {'G', "--."}, {'H', "...."},
 {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."},
 {'M', "--"}, {'N', "-."}, {'O', "---"}, {'P', ".--."},
 {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"},
 {'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"},
 {'Y', "-.--"}, {'Z', "--.."},
 {'0', "-----"}, {'1', ".----"}, {'2', "..---"}, {'3', "...--"},
 {'4', "....-"}, {'5', "....."}, {'6', "-...."}, {'7', "--..."},
 {'8', "---.."}, {'9', "----."},
 {'.', ".-.-.-"}, {',', "--..--"}, {'?', "..--.."},
 {'\0', "END"}
};
TREENODE* TELEGRAPH::root = 0;

void TELEGRAPH::Decode(char morse[], char text[]){
    char *morsePtr;
    TREENODE *node;
    node = root;
    cout << "Decode called."  << endl;
    for (morsePtr = morse; *morsePtr; morsePtr++) {   
        if(*morsePtr != ' '){
            if(*morsePtr == '.'){
                node = node->left;
            }
            else if (*morsePtr == '-'){
                node = node->right;
            }
        }
        continue;
    }
    *text++ = node->letter; 
    return;
}

void TELEGRAPH::Encode(char text[], char morse[]){
    int i;
    char c, *t, *morsePtr;
    cout << "Encode called" << endl;
    cout << "\nSending >>> ";
    for (t = text; *t; t++){
        c = toupper(*t);
        if (c == ' ') {
            *morse++ = ' ';
            continue;
        }

        for (i = 0; table[i].letter; i++){
            if (table[i].letter == c) break;
        }    
        if (!table[i].letter){
             continue;
        }
        morsePtr = table[i].code; 
        while (*morsePtr){
            *morse++ = *morsePtr++;
        }
        *morse++ = ' ';
    }
}

void TELEGRAPH::buildTree(){
    TREENODE *node, *nextNode;
    char *morsePtr; //Points to the dots and dashes in the table.
    root = new TREENODE;
    if (!root){ 
        return;
    }
    root->letter = ' ';
    cout << "Alphabet in Morse:";
    for (int i = 0; table[i].letter; i++) {
        node = root;
        for (morsePtr = table[i].code; *morsePtr; morsePtr++){ //goes through the morse code for that letter/symbol.
            if(*morsePtr ==  '-'){
                cout << *morsePtr;
                nextNode = new TREENODE;
                node->right = nextNode;
                node = node->right;
            }
            else if(*morsePtr == '.'){
                cout << *morsePtr;
                nextNode = new TREENODE;
                node->left = nextNode; 
                node = node->left;
            }
        }
    }
}

main():

int main(){
     TELEGRAPH station;
     char text[80], morse[600];
     station.buildTree();
     cout << "\nEnter telegram (in English): ";
     cin.getline(text, 80);
     station.Encode(text, morse);
     cout << morse;
     cout << " >>> Received\n\n";
     station.Decode(morse, text);
     cout << "Message sent: " << text << endl;
     station.destroyTree();
}

In Decode , you need to add node = root; whenever you start to decode a new morse code symbol. This could be done this way:

for (morsePtr = morse; *morsePtr; ++morsePtr) {
    if (*morsePtr != ' ') {
        // ...
    } else {
        node = root;
    }
}

You should also check that the node you want to access (eg. node->left ) actually exists, in case your input is invalid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM