Download presentation
Presentation is loading. Please wait.
Published byOsborne Johns Modified over 9 years ago
1
1 Huffman Codes Drozdek Chapter 11
2
2 Encoding Next we will add the capability to encode a message entered as normal text. The Huffman Tree that we use for decoding is not appropriate for encoding. We need to be able to look up a letter and get the string of 0’s and 1’s that are the code for that letter. Use an STL map for this. Key is character to be encoded. Value is string of 1's and 0's.
3
3 Huffman_Tree.h #pragma once #include #include "Char_Freq.h" class Huffman_Tree { public: Huffman_Tree(void); ~Huffman_Tree(void) {}; // Add a single node tree to the list. void Add(char c, double frequency); void Display_List(void); void Make_Decode_Tree(void); void Display_Decode_Tree(Char_Freq* cf, int indent) const; void Display_Code(Char_Freq* cf, std::string prefix) const; std::string Decode_Msg(std::string msg) const;
4
4 Huffman_Tree.h void Make_Encode_Map(void); std::string Encode_Char(char ch); private: std::list node_list; Char_Freq decode_tree_root; std::map encode_map; void Display_Code(Char_Freq* cf, std::string prefix) const; void Make_Encode_Map2(Char_Freq* cf, std::string prefix); }; NOT const
5
5 Huffman_Tree.cpp Add at end: void Huffman_Tree::Make_Encode_Map(void) { Make_Encode_Map2(&decode_tree_root, ""); }
6
6 Huffman_Tree.cpp void Huffman_Tree::Make_Encode_Map2(Char_Freq* cf, string prefix) { if (cf->ch != 0) { // At a leaf char ch = cf->Ch(); encode_map[ch] = prefix; } else { // At an interior node Make_Encode_Map2(cf->left, prefix + "0"); Make_Encode_Map2(cf->right, prefix + "1"); }
7
7 Using the Encode Tree string Huffman_Tree::Encode_Char(char ch) { string code = encode_map[ch]; return code; } Build Project
8
8 main.cpp Add code to main.cpp to permit the user to enter and encode messages. void do_encode(void) { string msg; cout << "Enter message to encode\n"; getline(cin, msg); for (size_t i = 0; i < msg.length(); ++i) { char next_char = tolower(msg[i]); string code = huffman_tree.Encode_Char(next_char); cout << code; } cout << endl << endl; } http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/ 2011_04_11_Huffman_Codes/do_encode.cpp.txt
9
9 main.cpp At end of main(), replacing do_decode(): huffman_tree.Make_Encode_Tree(); while (true) { do_encode(); do_decode(); } cin.get(); }
10
10 Program Running
11
11 Handle All Letters and Digits The program seems to be working. Let’s extend it to handle all letters and digits (and space.) Wikipedia has letter frequencies: http://en.wikipedia.org/wiki/Letter_frequency Copy from Downloads area: http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/ 2011_04_11_Huffman_Codes/build_huffman_tree.cpp.txt http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/ 2011_04_11_Huffman_Codes/build_huffman_tree.cpp.txt
12
12 In main.cpp void build_huffman_tree(void) { huffman_tree.add('a',0.0401); huffman_tree.add('b',0.0073); huffman_tree.add('c',0.0137); huffman_tree.add('d',0.0209); huffman_tree.add('e',0.0624); huffman_tree.add('f',0.0109); huffman_tree.add('g',0.0099); huffman_tree.add('h',0.0299); huffman_tree.add('i',0.0342); huffman_tree.add('j',0.0008); huffman_tree.add('k',0.0038); huffman_tree.add('l',0.0198); huffman_tree.add('m',0.0118); huffman_tree.add('n',0.0331); huffman_tree.add('o',0.0369); huffman_tree.add('p',0.0095); huffman_tree.add('q',0.0005); huffman_tree.add('r',0.0294); huffman_tree.add('s',0.0311);
13
13 In main.cpp huffman_tree.add('t',0.0445); huffman_tree.add('u',0.0135); huffman_tree.add('v',0.0048); huffman_tree.add('w',0.0116); huffman_tree.add('x',0.0007); huffman_tree.add('y',0.0097); huffman_tree.add('z',0.0004); huffman_tree.add('0',0.0442); huffman_tree.add('1',0.0442); huffman_tree.add('2',0.0442); huffman_tree.add('3',0.0442); huffman_tree.add('4',0.0442); huffman_tree.add('5',0.0442); huffman_tree.add('6',0.0442); huffman_tree.add('7',0.0442); huffman_tree.add('8',0.0442); huffman_tree.add('9',0.0442); huffman_tree.add(' ',0.0668); }
14
14 main() int main(void) { cout << "This is the Huffman code demo.\n"; build_huffman_tree(); huffman_tree.display_list(); huffman_tree.make_decode_tree(); huffman_tree.make_encode_tree(); int cmd_code = 0;
15
15 Program in Action Saved as Huffman_Codes_with_Associative_Map.zip
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.