質問

I am implementing a function that takes in a tree and an encoded string. Example:

decode(*Huffmantree, "10010101010")

I want this function to return decoded string for the encoded string in the input relative to the Huffman tree input.

The code I have so far:

string decode(NodePtr root, string encoded_str)
{
    string temp = "";
    for (int i = 0 ; i < encoded_str.size() ; i++)
    {
        if (root->is_leaf() == true)
        {
            temp[i] = root->letter;
            //cout << root->letter;
        }
        if (root->left != NULL)
        {
            encoded_str.
        }
        if(root->right != NULL)
        {

        }
    }
    return temp;
}

I am having trouble implementing what happens when either left or right is not NULL, i.e. when I have to continue to the next node.

Any ideas?

edit:

string decode(NodePtr root, string encoded_str)
{
    string temp = "";
    int i;
    for( i = 0 ; i < encoded_str.size() ; i++)
    {

    if(root == NULL)
    {
        cout<<"error in string"<<endl;
        return temp;
        //cout<<root->letter;
    }
    temp[i] = root->letter;
    if(encoded_str[i] == '0')
    {
        root = root->left;
    }
    else
    {
        root = root->right;
    }
    }
//    for (int i = 0 ; i < temp.size(); i++)
//    {
//        cout<<temp[i];
//    }
//    cout<<endl;
    temp[i]='/0';
    return temp;
}
役に立ちましたか?

解決

Following may help:

string decode(NodePtr root, string encoded_str)
{
    string res = "";
    NodePtr node = root;
    for (int i = 0; i != encoded_str.size(); ++i)
    {
        if (encoded_str[i] == '0') { // left child
            node = node->left;
        } else { // rigth child
            assert(encoded_str[i] == '1');
            node = node->right;
        }
        if (node->is_leaf() == true)
        {
            res += node->letter;
            node = root;
        }
    }
    return res;
}

他のヒント

This would be one of the simplest code , basically you need to check whether encoded_str is valid or not .

UPDATE : Now , the code should work .

string decode(NodePtr root, string encoded_str)
{
    string temp = "";
    int i;
    for(i = 0 ; i < encoded_str.size() ; i++)
    {

        if(root == NULL ){
            cout<<""not possible , error in encoded_str";
            return temp;
        }


        if(encoded_str[i]=='0')
            root=  root->left ;
        else
            root=  root->right ;


        if(node->is_leaf()){
            temp+=root->letter;
            return temp;
        }
    }

}

When I run this

#include <iostream>
#include<queue>
#include<vector>
#include<iomanip>
#include<string>

using namespace std;


string decode(NodePtr root, string encoded_str)
{
    string temp = "";
    int i;
    for(i = 0 ; i < encoded_str.size() ; i++)
    {

        if(root == NULL ){
            cout<< "not possible , error in encoded_str" << endl;
            return temp;
        }


        if(encoded_str[i]=='0')
            root=  root->left ;
        else
            root=  root->right ;


        if(node->is_leaf()){
            temp+=root->letter;
            return temp;
        }
    }

}



int main()
{

    decode(*Huffmantree, "10010101010");


    system("pause");
    return 0;
}    

I get the some undefined errors, Also there was a quotation error on line 18 I changed.

7   IntelliSense: identifier "NodePtr" is undefined c:\Users\Evan\Documents\Visual Studio 2012\Projects\huffman\huffman\Source.cpp  10  15  huffman
8   IntelliSense: identifier "node" is undefined    c:\Users\Evan\Documents\Visual Studio 2012\Projects\huffman\huffman\Source.cpp  29  12  huffman
9   IntelliSense: identifier "Huffmantree" is undefined c:\Users\Evan\Documents\Visual Studio 2012\Projects\huffman\huffman\Source.cpp  42  10  huffman

Error 5 error C2447: '{' : missing function header (old-style formal list?) c:\users\evan\documents\visual studio 2012\projects\huffman\huffman\source.cpp 11 1 huffman Error 2 error C2146: syntax error : missing ')' before identifier 'root' c:\users\evan\documents\visual studio 2012\projects\huffman\huffman\source.cpp 10 1 huffman Error 4 error C2143: syntax error : missing ';' before '{' c:\users\evan\documents\visual studio 2012\projects\huffman\huffman\source.cpp 11 1 huffman Error 1 error C2065: 'NodePtr' : undeclared identifier c:\users\evan\documents\visual studio 2012\projects\huffman\huffman\source.cpp 10 1 huffman Error 6 error C2065: 'Huffmantree' : undeclared identifier c:\users\evan\documents\visual studio 2012\projects\huffman\huffman\source.cpp 42 1 huffman Error 3 error C2059: syntax error : ')' c:\users\evan\documents\visual studio 2012\projects\huffman\huffman\source.cpp 10 1 huffman

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top