尝试通过二叉树中的递归返回字符串(连接)

时间:2013-11-14 05:40:26

标签: c++ recursion binary-search-tree

我不能,为了我的生活,找出这个问题! 我用字符串参数调用'encode()'方法。 当我遍历该字符串的字符时,     我用该字符串的字符调用'encode_char()'方法。 encode_char()方法遍历二叉树并附加“*”     每次左遍历和每次正确遍历的“ - ”。 如果我来到一个NULL节点,我跳过遍历,退出函数调用并减去     最后一个角色添加 例如。

return encode_char(node->left, let, codeString += "*"); // traverse & append
codeString = codeString.substr(0, codeString.size() - 1); // subtract last symbol
return encode_char(node->right, let, codeString += "-"); // traverse & append
codeString = codeString.substr(0, codeString.size() - 1); // subtract last symbol

目标:当我找到我正在搜索的节点时,我想返回创建的codeString并返回调用它的encode()方法。

class Morse_Code_Tree{

public:
Morse_Code_Tree() : root(NULL){}
void insert(char let, std::string codeString);
Morse_Node* getRoot(){return root;}
//std::string find(char let);
std::string decode(std::string message);
std::string encode(std::string message);
std::string encode_char(const Morse_Node* node, char let, std::string codeString);

protected:
Morse_Node* root;
};

// THIS FUNCTION IS THE PROBLEM
std::string Morse_Code_Tree::encode_char(const Morse_Node* node, char let, 
std::string codeString) {

if (node == NULL){
    // do nothing
}else if (node->letter == let){
    //cout << codeString << " ";
    return codeString;
}
else{
    return encode_char(node->left, let, codeString += "*");
    codeString = codeString.substr(0, codeString.size() - 1);
    return encode_char(node->right, let, codeString += "-");
    codeString = codeString.substr(0, codeString.size() - 1);
}
}

std::string Morse_Code_Tree::encode(std::string message){
std::string result = "";
for (size_t i = 0; i < message.length(); i++){
    std::string temp;
    char let = tolower(message[i]);
    result += encode_char(root, let, "");
}
return result;
}

void Morse_Code_Tree::insert(char let, std::string codeString){

// set root to empty
if (root == NULL){
    char letter = ' ';
    Morse_Node* blankNode = new Morse_Node(letter);
    root = blankNode;
}

Morse_Node* temp = root;
for (size_t i = 0; i < codeString.length(); i++){
    if (codeString[i] == '.'){
        if (i == codeString.length() - 1){
            Morse_Node* node = new Morse_Node(let);
            temp->left = node;
            break;
        }
        temp = temp->left;
    }
    else{
        if (i == codeString.length() - 1){
            Morse_Node* node = new Morse_Node(let);
            temp->right = node;
            break;
        }
        temp = temp->right;
    }
}
}

std::string Morse_Code_Tree::decode(std::string message){

std::string decodedMessage;
Morse_Node* temp = root;

for(size_t i = 0; i < message.length(); i++){
    char bit = message[i];
    if (bit == '*'){
        temp = temp->left;
    }
    else if (bit == '-'){
        temp = temp->right;
    }
    else if (bit == ' '){
        decodedMessage += temp->letter;
        temp = root;
    }
}
return decodedMessage;
}

1 个答案:

答案 0 :(得分:0)

您的函数将仅向下递归左侧树,但从第一次递归调用encode_char后return开始,不会递归到正确的树。

// THIS FUNCTION IS THE PROBLEM
std::string Morse_Code_Tree::encode_char(const Morse_Node* node, char let, std::string codeString)
{
    if (node == NULL)
    {
        /* changed to return empty string, vs random nothing */
        return std::string();
    }
    else if (node->letter == let)
    {
        //cout << codeString << " ";
        return codeString;
    }
    else
    {
        std::string rv;
        rv = encode_char(node->left, let, codeString += "*");
        if (!rv.empty()) {
            return rv; /* found something down the left side */
        }
        /* this code now reached, due to no return above, so the right tree is traversed */ 
        codeString = codeString.substr(0, codeString.size() - 1);
        rv = encode_char(node->right, let, codeString += "-");
        if (!rv.empty()) {
            return rv; /* found something in the right tree */
        }
        /* nothing found in right or left, return empty string */
        return rv;
        /* codeString = codeString.substr(0, codeString.size() - 1); */
    }
}