我已经对树进行了编码,现在我正在尝试遍历树并为每个字符创建一个位串并将这些值放入映射中。
到目前为止,这是我的代码:
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
}
return ret;
}