C++如何实现哈夫曼树编码解码

小编这次要给大家分享的是C++如何实现哈夫曼树编码解码,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

创新互联公司主营应城网站建设的网络公司,主营网站建设方案,成都app软件开发公司,应城h5微信小程序搭建,应城网站营销推广欢迎应城等地区企业咨询

代码:

#pragma once
#include
#include
using namespace std;
#define m 20
stack s;
/*哈夫曼树结点类HuffmanNode声明*/
template
class HuffmanNode {
private:
 HuffmanNode * left;
 HuffmanNode * right;
 T data;
 int weight;
public:
 HuffmanNode(){}
 HuffmanNode(const T& item, HuffmanNode * l, HuffmanNode * r):data(item),left(l),right(r){}
 HuffmanNode * getLeft() { return left; }
 void setLeft(HuffmanNode * left) { this->left = left; }
 HuffmanNode * getRight() { return right; }
 void setRight(HuffmanNode * right) { this->right=right; }
 T& getData() { return data; }
 void setData(const T& item) { data = item; }
 int getWeight() { return weight; }
 void setWeight(int weight) { this->weight = weight; }
};

/*哈夫曼树类HuffmanTree声明*/
template
class HuffmanTree {
private:
 HuffmanNode * root;
 T data[m];
 int weight[m];
 int count;
public:
 HuffmanTree(HuffmanNode * t=NULL):root(t){}
 void CreateHuffmanTree();//创建哈夫曼树
 HuffmanNode * getRoot() { return root; }
 void setRoot(HuffmanNode * t) { root = t; }
 void inputNodes();//输入各个结点的data和weight值
 HuffmanNode * deCode(HuffmanNode*t,T item);
 void showCode();
};

template
inline void HuffmanTree::CreateHuffmanTree()
{
 inputNodes();
 HuffmanNode ** H = new HuffmanNode*[count];
 HuffmanNode * p1 = NULL, *p2 = NULL, * p,* t;
 for (int i = 0; i < count; i++) {
 H[i] = new HuffmanNode();
 H[i]->setData(data[i]);
 H[i]->setWeight(weight[i]);
 H[i]->setLeft(NULL);
 H[i]->setRight(NULL);
 }

 for (int i = 0; i < count - 1; i++) {
 p = new HuffmanNode();
 p1 = H[i];
 p2 = H[i + 1];
 p->setLeft(p1);
 p->setRight(p2);
 p->setWeight(p1->getWeight() + p2->getWeight());
 //将新的结点插入结点数组中的合适位置,并将被合并的两个结点删除
 t = p;
 int j = i + 2;
 while ( j < count &&H[j]->getWeight() < t->getWeight()) {
  H[j - 1] = H[j];
  j++;
 }
 H[j - 1] = t;
 }
 root = H[count - 1];
}

template
inline void HuffmanTree::inputNodes()
{
 int i = 0;
 for (int j = 0; j < m; j++) {
 data[j] = NULL;
 weight[j] = 0;
 }
 while (cin >> data[i] >> weight[i]) {
 i++;
 }
 count = i;
}

template
inline HuffmanNode* HuffmanTree::deCode( HuffmanNode * t,T item)
{
 HuffmanNode * p, * q;
 if(t==NULL)
 return NULL;
 if (t->getData() == item)
 return t;
 if ((p = deCode(t->getLeft(), item))!=NULL) {
 s.push(0);
 return p;
 }
 else if((q = deCode(t->getRight(), item))!=NULL){
 s.push(1);
 return q ;
 }
}

template
inline void HuffmanTree::showCode()
{
 cout << root->getWeight() << endl;
 cout << (root->getLeft())->getRight()->getData() << endl;
 for (int i = 0; i < count; i++) {
 cout << data[i] << "——";
 deCode(root, data[i]);
 while (!s.empty()) {
  cout << s.top();
  s.pop();
 }
 cout << endl;
 }
}

测试程序:

#include
#include"HuffmanTree.h"
using namespace std;

int main() {
 HuffmanTree * tree = new HuffmanTree();
 tree->CreateHuffmanTree();
 tree->showCode();
 system("pause");
 return 0;
}

测试数据:

n 1
s 3
t 4
a 10
i 12
p 13
e 15
Ctrl+Z 回车结束输入
输入时权值由小到大排序…也可以在程序里加一个排序

结果:

C++如何实现哈夫曼树编码解码

看完这篇关于C++如何实现哈夫曼树编码解码的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。


新闻名称:C++如何实现哈夫曼树编码解码
本文URL:http://scjbc.cn/article/pgisgs.html

其他资讯