Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote