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;
}
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 .
Following may help:
When I run this
I get the some undefined errors, Also there was a quotation error on line 18 I changed.
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