2017-07-29 139 views
0

基本上在这个程序中创建了一个二叉搜索树,它完整的由一个字母amd 2 TREENODE指针(左和右)组成的结构体TREENODE连接到其他节点并模拟树。用户输入被保存到字符数组text。编码函数在数组中迭代并查找莫尔斯码转换,将其保存到字符数组morse。奇迹般有效。摩尔斯电码二叉搜索树中的分段错误

问题:在Decode函数中接收到分段错误。 GDB:![BST Segmentation Fault

这表示没有真正创建二叉搜索树。

如何解决此代码,使解码功能的作品? (我知道我可以只清点text但我想创建一个解码字符数组合法功能

头文件。

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(); 
} 
+2

如何解决这个问题?你可以使用调试器 – user463035818

+1

也许你应该看看'std :: map' /'std :: string'来代替。 –

回答

0

Decode中,每当您开始解码新的莫尔斯电码符号时,您需要添加node = root;。可以这样做:

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

您还应该检查您要访问的节点(例如, node->left)实际存在,以防输入无效。