[英]Decoding Huffman Tree
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, ie when I have to continue to the next node. 我很难实现当left或right不为NULL时(即当我必须继续到下一个节点时)发生的情况。
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 . 这将是最简单的代码之一,基本上,您需要检查encoded_str
是否有效。
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. 我得到一些未定义的错误,而且在我更改的第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
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 ';' 错误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:语法错误:缺少';' 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 在'{'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错误6错误C2065:'Huffmantree':未声明的标识符c:\\ users \\ evan \\ documents \\ visual studio 2012 \\ projects \\ huffman \\ huffman \\ source.cpp 42 1霍夫曼错误3错误C2059:语法错误:')'c:\\ users \\ evan \\ documents \\ visual studio 2012 \\ projects \\ huffman \\ huffman \\ source.cpp 10 1霍夫曼
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.