I am doing huffman coding and I have made the tree. Now I am required to traverse the tree to create bit strings associated with the characters. Then I will put the bit string and char into a map to use with encode/decode. I have been working on this for days and could really use some help. Thanks for any help you can offer!

map<char, string> build_encoding_map(freq_info*& huffman_root) 
{
    map<char, string> ret;
    string char_code;
    char symbol;

    if (huffman_root->left == NULL && huffman_root->right == NULL)
    {
        cout << "Your tree is empty." << endl;
    }
    else
    {
        while (huffman_root->is_leaf = false)
        {
            if (huffman_root->left != NULL)
            {
                huffman_root = huffman_root->left;
                char_code += ".";
                //add a bit to the bit string
            }
            else if (huffman_root->right != NULL)
            {
                huffman_root = huffman_root->right;
                char_code += "^";
        }
        symbol = huffman_root->symbol;
        ret[symbol] = char_code;
        huffman_root->is_leaf = false; //So that I don't have repeat characters in the map

        //ret.insert(std::make_pair(symbol, char_code));
        //Need to delete the leaf node  
    }
    //traverse the tree until is_leaf is true
    //add turns taken (left"." or right"^") to the string
    //insert the character in the found leaf and it's associated string to the map

    // implement me. I suggest doing this recursively with a secondary
    // function that takes a pass-by-reference string for the current
    // state of the bitstring. read the C++ documentation on 'string' to
    // see how to add and erase characters to/from the end of a string.
    //Build the encoding map


}

You really need to ask a specific question. "Help!" won't get you many replies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.