资源简介
自适应霍夫曼编码的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
};
代码片段和文件信息
#include
#include
#include
#include
#define PSEUDO_EOF 3
using namespace std;
struct HuffmanTree{
unsigned char letter; // the unsigned character of each code
int count; // frequency of each unsigned character
int left; // the left child of each node
int right; // the right child of each node
int parent; // the parent of each node
public:
bool operator==(const HuffmanTree& node) const;
bool operator!=(const HuffmanTree& node) const;
};
bool HuffmanTree::operator==(const HuffmanTree& node) const
{
return node.count == count && node.letter == letter && node.left == left && node.right == right && node.parent == parent;
}
bool HuffmanTree::operator!=(const HuffmanTree& node) const
{
return node.count != count || node.letter != letter || node.left != left || node.right != right || node.parent != parent;
}
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
};
int AdaptiveTree::ALPH_SIZE = 1024;
unsigned char AdaptiveTree::NYT = 255;
unsigned char AdaptiveTree::none = 254;
string AdaptiveTree::decodingStr() const
{
return deStr;
}
unsigned char AdaptiveTree::binary2char(string bin)
{
int n = bin.length();
int tempchar=0i;
for (i=0; i {
tempchar += (bin[i] - ‘0‘) * (int)pow(2i);
}
return (unsigned char)(tempchar);
}
int Adaptive
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 11778 2009-05-22 17:11 adaptiveHuffmanCoding.cpp
----------- --------- ---------- ----- ----
11778 1
- 上一篇:TstCon.exe
- 下一篇:hello world ,stm32串口打印程序
评论
共有 条评论