decode(*Huffmantree, "10010101010")
我希望此函数返回输入中相对于霍夫曼树输入的编码字符串的解码字符串。
我目前拥有的代码:
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;
}
我无法实现当 left 或 right 不为 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;
}
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;
}
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;
}
}
}
#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 van\documents isual studio 2012\projects\huffman\huffman\source.cpp 11 1 huffman 错误 2 错误 C2146:语法错误:标识符“root”之前缺少“)”c:\users van\documents isual studio 2012\projects\huffman\huffman\source.cpp 10 1 huffman 错误 4 错误 C2143:语法错误:缺少 ';'在“{”之前 c:\users van\documents isual studio 2012\projects\huffman\huffman\source.cpp 11 1 huffman 错误 1 错误 C2065:'NodePtr':未声明的标识符 c:\users van\documents isual studio 2012\projects\huffman\huffman\source.cpp 10 1 huffman 错误 6 错误 C2065:“Huffmantree”:未声明的标识符 c:\users van\documents isual studio 2012\projects\huffman\huffman\source.cpp 42 1 huffman 错误 3 错误 C2059:语法错误:')' c:\users van\documents isual studio 2012\projects\huffman\huffman\source.cpp 10 1 huffman