自适应霍夫曼编码的C++版本简单实现
class AdaptiveTree {
public:
AdaptiveTree(int rootNum);
AdaptiveTree(int rootNum, string str);
void swap(int first, int second); // swap two nodes of the tree
void initalCode(); // initializing the data
string char2code(unsigned char letter); // locate the character in the tree with its corresponding binary string and return the string
string char2binary(unsigned char letter); // translating the character to the 8-bit binary string
unsigned char binary2char(string bin); // translating the binary string: bin to the corresponding character
int spawn(unsigned char letter); // add a new character to the original tree
void updateTree(unsigned char newchar); // update the tree
int highestInBlock(int count); // return the highest node to be exchanged
void setString(string str); //
string decodingStr() const;
void encoding();
string decoding();
unsigned char code2char(string bincode);
static int size();
string binStr() const; // return the binary string of string: tempString
private:
void run();
int findchar(unsigned char letter ); // locate the letter in the tree
string tempString; //temp string to be encoded needed to be stored here
string deStr;// used for storing the decoding string
string bin; // used for storing the result of encoding process
/* Adaptive Tree data members */
HuffmanTree *tree;
int root;
/* Adaptive Tree constants */
static int ALPH_SIZE; // size of the alphabet
static unsigned char none; // not a unsigned character
static unsigned char NYT; // Not Yet transmitted code
};
1