Huffman树及编码C++实现
By qianghaohao(Johar)
Huffman树采用数组实现,编码时从叶子节点开始向上编码,所以采用deque支持前插的
容器来存放每个叶子的编码。
代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <deque>
using namespace std;
typedef struct {
int weight;
int parent;
int lchild;
int rchild;
bool flag;
} HaffmanTree;
// h:Haffman树 w:权向量
void CreateHaffmanTree(vector<HaffmanTree> &h, const vector<int>w) {
int n = w.size(); //叶子个数
int condi = 2 * n - 1; //Haff