解码霍夫曼树

时间:2014-02-18 12:35:51

标签: c++ binary-tree huffman-code

我正在实现一个接受树和编码字符串的函数。 例如:

decode(*Huffmantree, "10010101010")

我希望此函数返回输入中编码字符串的解码字符串,相对于Huffman树输入。

到目前为止我的代码:

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;
}

我无法实现当左或右不为NULL时发生的事情,即当我必须继续下一个节点时。

有什么想法吗?

编辑:

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;
}

3 个答案:

答案 0 :(得分:4)

以下可能会有所帮助:

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;
}

答案 1 :(得分:1)

这将是最简单的代码之一,基本上您需要检查encoded_str是否有效。

更新:现在,代码应该有效。

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;
        }
    }

}

答案 2 :(得分:0)

当我运行时

#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;
}    

我得到一些未定义的错误, 第18行也有引用错误我改变了。

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

错误5错误C2447:'{':缺少函数头(旧式正式列表?)c:\ users \ evan \ documents \ visual studio 2012 \ projects \ huffman \ huffman \ source.cpp 11 1 huffman 错误2错误C2146:语法错误:缺少')'在标识符'root'之前'c:\ users \ evan \ documents \ visual studio 2012 \ projects \ huffman \ huffman \ source.cpp 10 1 huffman 错误4错误C2143:语法错误:缺少';'在'{'c:\ users \ evan \ documents \ visual studio 2012 \ projects \ huffman \ huffman \ source.cpp 11 1 huffman之前 错误1错误C2065:'NodePtr':未声明的标识符c:\ users \ evan \ documents \ visual studio 2012 \ projects \ huffman \ huffman \ source.cpp 10 1 huffman 错误6错误C2065:'Huffmantree':未声明的标识符c:\ users \ evan \ documents \ visual studio 2012 \ projects \ huffman \ huffman \ source.cpp 42 1 huffman 错误3错误C2059:语法错误:')'c:\ users \ evan \ documents \ visual studio 2012 \ projects \ huffman \ huffman \ source.cpp 10 1 huffman

相关问题