|
ADS
Algorithms and Data Structures
|
The code of Huffman (reality check of section 14.4). More...
Files | |
| file | ADS/string/Huffman.h |
| The code of Huffman (reality check of section 14.4): a prefix code, no code being a prefix of another one, so that a text can be decoded, of least length for the frequencies of the characters. | |
Functions | |
| template<class C, class F> | |
| std::map< C, std::string > | ADS::huffman (const std::map< C, F > &freq) |
| the codes of Huffman of the characters with the given frequencies, at least one; a single character gets the code 0 | |
| template<class C> | |
| std::string | ADS::encode (const std::basic_string< C > &text, const std::map< C, std::string > &code) |
| the text encoded by the codes, the code of each character in turn | |
| template<class C> | |
| std::basic_string< C > | ADS::decode (const std::string &bits, const std::map< C, std::string > &code) |
| the bits decoded by the codes, a prefix code | |
The code of Huffman (reality check of section 14.4).
The definitions, the invariants and the costs of the area are in Theory of strings.
| std::basic_string< C > ADS::decode | ( | const std::string & | bits, |
| const std::map< C, std::string > & | code ) |
the bits decoded by the codes, a prefix code
In pseudocode (section 14.4), the bits read until they spell a code, which no other code extends:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{decode}( \mathit{bits} , \mathit{code} )\quad \triangleright\ \text{no code is a prefix of another} \cr 2 & \quad s \gets \varepsilon;\ t \gets \varepsilon \cr 3 & \quad \textbf{foreach}\ b \in \mathit{bits}\ \textbf{do} \cr 4 & \qquad \text{append } b \text{ to } s \cr 5 & \qquad \textbf{if}\ s \text{ is the code of a character } c \ \textbf{then}\ \text{append } c \text{ to } t;\ s \gets \varepsilon \cr 6 & \quad \textbf{return}\ t \end{array}} \]
| std::string ADS::encode | ( | const std::basic_string< C > & | text, |
| const std::map< C, std::string > & | code ) |
the text encoded by the codes, the code of each character in turn
| std::map< C, std::string > ADS::huffman | ( | const std::map< C, F > & | freq | ) |
the codes of Huffman of the characters with the given frequencies, at least one; a single character gets the code 0
In pseudocode (section 14.4), \( Q \) a min-heap of the nodes by frequency:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{huffman}( \mathit{freq} ) \cr 2 & \quad Q \gets \text{a min-heap of a leaf for each character } c \text{, of priority } \mathit{freq}[ c ] \cr 3 & \quad \textbf{while}\ Q \text{ holds two trees or more} \ \textbf{do}\quad \triangleright \ \text{the two lightest become siblings} \cr 4 & \qquad ( z_1 , f_1 ) \gets \mathrm{deleteMin}( Q );\ ( z_2 , f_2 ) \gets \mathrm{deleteMin}( Q ) \cr 5 & \qquad \mathrm{insert}( Q , \text{a new node over } z_1 \text{ and } z_2 , f_1 + f_2 ) \cr 6 & \quad \mathit{code}[ c ] \gets \text{the path from the root to the leaf of } c \text{, } 0 \text{ for left and } 1 \text{ for right} \cr 7 & \quad \textbf{return}\ \mathit{code} \end{array}} \]