c++ // decode_char(tnode, message, d){ // decode one character from the message.
ID: 3684173 • Letter: C
Question
c++
// decode_char(tnode, message, d){
// decode one character from the message.
// Pre: tnode is a node in a huffman tree
// message:: cstring, the whole message to decode
// d:: a reference to an int containing the current
// index in the message
// Post: d has been increased by the number of 0s and 1s used to
// encode the character
// Return: the decoded character
char decode_char(TreeNode *t, char message[], int *d){
// TODO: complete this function
return ’.’; // dot returned for no good reason
}
Explanation / Answer
Answer:
char decode(TreeNode root, char message[])
{
char result = "";
TreeNode node = root;
for (int i = 0; i != message.size(); ++i)
{
if (message[i] == '0') {
node = node->left;
} else {
assert(message[i] == '1');
node = node->right;
}
if (node->is_leaf() == true)
{
result += node->letter;
node = root;
}
}
return result;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.